参考文档 :
约束布局 bias 计算公式参考 【约束布局】ConstraintLayout 偏移 ( Bias ) 计算方式详解 ( 缝隙比例 | 计算公式 | 图解 | 测量图 + 公式 ) 方案 ;
约束布局 百分比 屏幕适配案例参考 【约束布局】ConstraintLayout 屏幕适配案例 ( 使用代码生成约束布局控件属性 ) 博客 ;
一、将设计稿尺寸自动转为约束布局百分比标签属性
美工给出的设计稿尺寸 720 × 1280 720 \times 1280 720×1280 像素 ;
在 caculate_constraint 方法中 , width 和 height 的高度就是设计稿的 宽度 720 和 高度 1280 ;
// 相对于父类 比例计算 的原始数据 : 屏幕 宽高 , 其比例肯定是相对于父控件进行计算
float width = 1280, height = 720;
width_inner 和 height_inner 是用于计算组件在约束布局中的位置的 , 一般情况下这两个值就是布局的宽高 , 也就是 宽度 720 和 高度 1280 ;
但是 , 假如有特殊需求 , 比如组件框定在某个组件的范围内 , 则设置不同的值 ;
// 计算 垂直 水平方向 bias 数据 , 子布局 , 如果是相对于父控件 , 就是 750, 1334
// 计算流程 :
// ① bias 宽度计算 : 计算出总的 bias 总长度 = width_inner - 控件长度 , 左侧值 / 总长度 = 水平方向的
// bias 值
// ② bias 高度计算 : 计算出总的 bias 总高度 = height_inner - 控件高度 , 顶部值 / 总高度 =
// 垂直方向的 bias 值
float width_inner = width, height_inner = height;
float[][] left_top_data 数组存放的是组件 左上角顶点位置 , float[][] width_height_data 数组存放的是宽高位置 ;
有了上述 4 4 4 组数据之后 , 就可以自动生成约束布局百分比标签属性 ;
使用如下代码生成 约束布局 标签属性 :
public class BoundaryCaculate {
public static void main(String[] args) {
caculate_constraint();
}
// 给定左上值计算
public static void caculate_constraint() {
// 相对于父类 比例计算 的原始数据 : 屏幕 宽高 , 其比例肯定是相对于父控件进行计算
float width = 1280, height = 720;
// 计算 垂直 水平方向 bias 数据 , 子布局 , 如果是相对于父控件 , 就是 750, 1334
// 计算流程 :
// ① bias 宽度计算 : 计算出总的 bias 总长度 = width_inner - 控件长度 , 左侧值 / 总长度 = 水平方向的
// bias 值
// ② bias 高度计算 : 计算出总的 bias 总高度 = height_inner - 控件高度 , 顶部值 / 总高度 =
// 垂直方向的 bias 值
float width_inner = width, height_inner = height;
// 左上位置
float[][] left_top_data = {
{
0, 0 },
{
0, 186 },
{
400, 0 },
{
600, 0 },
{
800, 0 },
{
1000, 0 },
{
2, 260 },
{
200, 260 },
{
400, 260 },
{
600, 260 },
{
800, 260 },
{
1000, 260 }
} ;
// 图片坐标,0位置是宽,1位置是高
float[][] width_height_data = {
{
200, 200 },
{
200, 78 },
{
200, 260 },
{
200, 260 },
{
200, 260 },
{
200, 260 },
{
200, 260 },
{
200, 260 },
{
200, 260 },
{
200, 260 },
{
200, 260 },
{
200, 260 }
} ;
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 horizontal_bias = 0.5f;
if (width_inner - width_height_data[i][0] != 0) {
horizontal_bias = left / (width_inner - width_height_data[i][0]);
horizontal_bias = format_float(horizontal_bias);
}
float vertical_bias = 0.5f;
if ((height_inner - width_height_data[i][1]) != 0) {
vertical_bias = top / (height_inner - width_height_data[i][1]);
vertical_bias = format_float(vertical_bias);
}
float margin_parent_top = top / height;
margin_parent_top = format_float(margin_parent_top);
System.out.println("第" + i + "个点 : left : " + left + " , left : " + top + " , width : "
+ width_height_data[i][0] + " , height : " + width_height_data[i][1] + "\n");
System.out.println("android:layout_width=\"0dip\"\n" + "android:layout_height=\"0dip\"\n\n" +
"app:layout_constraintHeight_default=\"percent\"\n" + "app:layout_constraintHeight_percent=\""
+ height_percent + "\"\n\n" +
"app:layout_constraintWidth_default=\"percent\"\n" + "app:layout_constraintWidth_percent=\"" + width_percent
+ "\"\n\n\n" +
"app:layout_constraintDimensionRatio=\"" + (int) width_height_data[i][0] + ":"
+ (int) width_height_data[i][1] + "\"\n\n" +
"app:layout_constraintLeft_toLeftOf=\"parent\"\n" + "app:layout_constraintRight_toRightOf=\"parent\"\n"
+ "app:layout_constraintHorizontal_bias=\"" + horizontal_bias + "\"\n\n" +
"app:layout_constraintTop_toTopOf=\"parent\"\n" + "app:layout_constraintBottom_toBottomOf=\"parent\"\n"
+ "app:layout_constraintVertical_bias=\"" + vertical_bias + "\"\n\n" +
"android:scaleType=\"fitXY\"\n" + "android:src=\"@mipmap/actual_\"\n");
}
}
}
二、将输出结果设置到组件标签中
输出结果样式 :
第0个点 : left : 0.0 , left : 0.0 , width : 200.0 , height : 200.0
android:layout_width="0dip"
android:layout_height="0dip"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.76923"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="1.0"
app:layout_constraintDimensionRatio="200:200"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.0"
android:scaleType="fitXY"
android:src="@mipmap/actual_"
约束布局组件样式 : 这里以 ImageView 为例 ;
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.76923"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="1.0"
app:layout_constraintDimensionRatio="200:200"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.0"
android:scaleType="fitXY"
android:src="@mipmap/ic_launcher" />
</androidx.constraintlayout.widget.ConstraintLayout>