【Android 屏幕适配】屏幕适配通用解决方案 ⑦ ( PercentRelativeLayout 百分比布局方案 | 该布局已废弃本方案仅做参考 )


参考文档 :


约束布局 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>

猜你喜欢

转载自blog.csdn.net/han1202012/article/details/126429488