【Android 屏幕适配】屏幕适配通用解决方案 ⑦ ( PercentRelativeLayout 百分比布局方案 | 该布局已废弃本方案仅做参考 )
2023-06-13 09:18:06 时间
文章目录
参考文档 :
约束布局 bias 计算公式参考 【约束布局】ConstraintLayout 偏移 ( Bias ) 计算方式详解 ( 缝隙比例 | 计算公式 | 图解 | 测量图 + 公式 ) 方案 ;
约束布局 百分比 屏幕适配案例参考 【约束布局】ConstraintLayout 屏幕适配案例 ( 使用代码生成约束布局控件属性 ) 博客 ;
约束布局百分比布局完整方案参考 【Android 屏幕适配】屏幕适配通用解决方案 ⑥ ( 约束布局 ConstraintLayout 百分比布局方案 | 将设计稿尺寸自动转为约束布局百分比标签属性 | 将输出结果设置到组件标签中 ) 博客 ;
一、PercentRelativeLayout 百分比布局方案
使用如下程序 , 输入
- PercentRelativeLayout 布局的 宽度 , 高度
// 给出中心点坐标,图片宽高,屏幕宽高,计算出该图片的位置
// 屏幕宽高
float width = 1334, height = 614;
- 左上角顶点的坐标
float[][] left_top_data
- 子组件的宽度和高度
float[][] width_height_data
直接可以输出 PercentRelativeLayout 布局中的子组件的标签属性 ;
完整代码如下 :
public class BoundaryCaculate {
public static void main(String[] args) {
caculate_top_left();
}
// 给定左上值计算
public static void caculate_top_left() {
// 给出中心点坐标,图片宽高,屏幕宽高,计算出该图片的位置
// 屏幕宽高
float width = 1334, height = 614;
// 左上角顶点坐标
float[][] left_top_data = {
{ 0, 24 },
{ 1013, 25 }
};
// 图片坐标,0位置是宽,1位置是高
float[][] width_height_data = {
{ 1200, 520 },
{ 106, 50 }
};
for (int i = 0; i < left_top_data.length; i++) {
float width_percent = width_height_data[i][0] / width;
width_percent = format_float(width_percent);
float height_percent = width_height_data[i][1] / height;
height_percent = format_float(height_percent);
float left = left_top_data[i][0];
float top = left_top_data[i][1];
float margin_parent_left = left / width;
margin_parent_left = format_float(margin_parent_left);
float margin_parent_top = top / height;
margin_parent_top = format_float(margin_parent_top);
float aspectRatio = width_height_data[i][0] / width_height_data[i][1];
aspectRatio = format_float(aspectRatio);
System.out.println("第" + i + "个点 : \n" + left + " , " + top);
System.out.println(margin_parent_left + " , " + margin_parent_top);
System.out.println("android:layout_width=\"0dip\"\n" + "android:layout_height=\"0dip\"\n"
+ "app:layout_widthPercent=\"" + width_percent * 100 + "%\"\n" + "app:layout_heightPercent=\""
+ height_percent * 100 + "%\"\n"
+ "app:layout_aspectRatio=\"" + aspectRatio * 100 + "%\"\n"
+ "app:layout_marginLeftPercent=\"" + margin_parent_left * 100
+ "%\"\n" + "app:layout_marginTopPercent=\"" + margin_parent_top * 100 + "%\"\n"
+ "android:scaleType=\"fitXY\"\n" + "android:src=\"@mipmap/actual_\"\n");
}
}
}
二、将输出结果设置到组件标签中
输出结果样式 :
第0个点 :
0.0 , 24.0
0.0 , 0.03909
android:layout_width="0dip"
android:layout_height="0dip"
app:layout_widthPercent="89.955%"
app:layout_heightPercent="84.691%"
app:layout_aspectRatio="230.769%"
app:layout_marginLeftPercent="0.0%"
app:layout_marginTopPercent="3.909%"
android:scaleType="fitXY"
android:src="@mipmap/actual_"
第1个点 :
1013.0 , 25.0
0.75937 , 0.04072
android:layout_width="0dip"
android:layout_height="0dip"
app:layout_widthPercent="7.946%"
app:layout_heightPercent="8.143001%"
app:layout_aspectRatio="211.99998%"
app:layout_marginLeftPercent="75.937004%"
app:layout_marginTopPercent="4.072%"
android:scaleType="fitXY"
android:src="@mipmap/actual_"
约束布局组件样式 : 这里以 ImageView 为例 ;
<?xml version="1.0" encoding="utf-8"?>
<android.support.percent.PercentRelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="0dip"
android:layout_height="0dip"
app:layout_widthPercent="89.955%"
app:layout_heightPercent="84.691%"
app:layout_aspectRatio="230.769%"
app:layout_marginLeftPercent="0.0%"
app:layout_marginTopPercent="3.909%"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher" />
</android.support.percent.PercentRelativeLayout>
相关文章
- android系统开机画面_Android开机画面
- android中适配器的作用,适配器模式 在Android中的简单理解「建议收藏」
- android 读取本地数据库db文件(Android sqlite)
- android进阶之了解Android系统与开机过程
- android退出app代码,Android应用退出代码各种方式
- gear s3刷android wear,【干货】三星Gear S3/Gear S3 classic 智能手表刷机教程「建议收藏」
- android定时器取消,Android定时器崩溃取消
- 【Android布局】在程序中设置android gravity 和 android layout Gravity属性
- iphone4装android,iPhone4可安装Android实现双系统启动.pdf
- Android angle_android 界面悬停
- Android触摸事件_android设置按钮点击事件
- Android保存图片到相册(适配android 10以下及以上)
- Android SDK Tools_android.intent.category.DEFAULT
- Android OpenCV 4.6 透视变换
- 【Android 组件化】使用 Gradle 实现组件化 ( 组件模式与集成模式切换 )
- 【Android 插件化】VirtualAppEx 编译运行 ( VirtualAppEx 简介 | 配置 VirtualAppEx 编译环境 | 编译运行 VirtualAppEx 代码 )
- 【错误记录】Android NDK 编译报错 ( no known conversion from ‘unsigned char *‘ to ‘const char *‘ )
- 【ijkplayer】编译 Android 版本的 ijkplayer ③ ( 执行 compile-ffmpeg.sh clean 命令 | 下载并配置 android-ndk-r10e )
- 【Android 屏幕适配】屏幕适配通用解决方案 ① ( 定义 dimens.xml 方案 | 使用 ScreenMatch 插件生成不同屏幕分辨率的 dimens.xml 配置 )
- 安装包立减1M--微信Android资源混淆打包工具
- Android测试入门-1
- Android横竖屏切换View设置不同尺寸或等比例缩放的XML解决方案详解手机开发
- [图]Android辅助套件迎来Camera Switches:用面部表情控制手机
- android自定义Android菜单背景的代码
- android中处理各种触摸事件的方法浅谈
- 安装时加入外部数据库示例(android外部数据库)