一、ConstraintLayout 屏幕适配案例
ConstraintLayout 屏幕适配案例 :
<?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">
<kim.hsl.android_ui.PathMeasureView
android:id="@+id/pathMeasureView"
android:layout_width="0dip"
android:layout_height="0dip"
app:layout_constraintHeight_default="percent"
app:layout_constraintHeight_percent="0.5"
app:layout_constraintWidth_default="percent"
app:layout_constraintWidth_percent="0.5"
app:layout_constraintDimensionRatio="1:1"
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.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
布局样式如下 :
二、使用代码生成约束布局
public class BoundaryCaculate {
public static void main(String[] args) {
caculate_constraint();
}
// 给定左上值计算
public static void caculate_constraint() {
// 相对于父类 比例计算 的原始数据 : 屏幕 宽高 , 其比例肯定是相对于父控件进行计算
float width = 200, height = 260;
// 计算 垂直 水平方向 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_"
三、备份代码
package main;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import javax.swing.text.AbstractDocument.LeafElement;
/**
* 计算坐标 主要是给出中点坐标, 计算上下左右坐标
*
* @author octopus
*
*/
public class BoundaryCaculate {
public static void main(String[] args) {
caculate_constraint();
//caculate_top_left();
// caculate_stage_instrument();
// caculate_center();
// caculate_width_center();
// caculate_toolbar();
// caculate_mouse_position();
// caculate_center_point();
}
// 给定左上值计算
public static void caculate_constraint() {
// 相对于父类 比例计算 的原始数据 : 屏幕 宽高 , 其比例肯定是相对于父控件进行计算
float width = 720, height = 426;
// 计算 垂直 水平方向 bias 数据 , 子布局 , 如果是相对于父控件 , 就是 750, 1334
// 计算流程 :
// ① bias 宽度计算 : 计算出总的 bias 总长度 = width_inner - 控件长度 , 左侧值 / 总长度 = 水平方向的
// bias 值
// ② bias 高度计算 : 计算出总的 bias 总高度 = height_inner - 控件高度 , 顶部值 / 总高度 =
// 垂直方向的 bias 值
float width_inner = width, height_inner = height;
// 中心点坐标
float[][] left_top_data = {
{ 37, 39 },
{ 657, 39 },
{ 106, 304 },
{ 530, 304 },
} ;
// 图片坐标,0位置是宽,1位置是高
float[][] width_height_data = {
{ 150, 68 },
{ 53, 36 },
{ 150, 90 },
{ 150, 90 },
} ;
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");
}
}
private static void caculate_center_point() {
float points[][] = { { 118, 440 }, { 270, 435 }, { 422, 431 }, { 574, 426 }, { 726, 425 }, { 878, 420 },
{ 1030, 417 }, { 1182, 413 } };
// 计算出 7 个中心点值
for (int i = 0; i < points.length - 1; i++) {
System.out.println(i + " . x : " + (int) ((points[i][0] + points[i + 1][0]) / 2f));
}
for (int i = 0; i < points.length - 1; i++) {
System.out.println(i + " . y : " + (int) ((points[i][1] + points[i + 1][1]) / 2f));
}
}
private static void caculate_toolbar() {
float width = 1334;
float height = 750;
float height_toolbar = 93;
float width_percent = width / width;
width_percent = format_float(width_percent);
float height_percent = height_toolbar / height;
height_percent = format_float(height_percent);
float left = 0;
float top = 0;
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);
System.out.println("标题栏属性 : ");
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_marginLeftPercent=\"" + margin_parent_left * 100
+ "%\"\n" + "app:layout_marginTopPercent=\"" + margin_parent_top * 100 + "%\"\n");
System.out.println("单个元素属性");
width_percent = height_toolbar / width;
width_percent = format_float(width_percent);
System.out.println(
"android:layout_width=\"0dip\"\n" + "android:layout_height=\"0dip\"\n" + "app:layout_widthPercent=\""
+ width_percent * 100 + "%\"\n" + "app:layout_heightPercent=\"" + 100 + "%\"\n");
}
private static void caculate_width_center() {
// 吉他 : 累加坐标 : 12, 148, 283, 416, 549, 681, 814, 948
// 吉他 : 中心坐标 : 80.0f , 215.5f , 349.5f , 482.5f , 615.0f , 747.5f ,
// 881.0f
// 古筝 : 累加坐标 : 118, 270, 422, 574, 726, 878, 1030, 1182
// 古筝 : 中心坐标 : 194.0f , 346.0f , 498.0f , 650.0f , 802.0f , 954.0f ,
// 1106.0f
// 贝斯 : 累加坐标 : 1, 144, 277, 412, 545, 678, 810, 944
// 贝斯 : 中心坐标 : 72.5f , 210.5f , 344.5f , 478.5f , 611.5f , 744.0f ,
// 877.0f
// 萨克斯 : 累加坐标 : {118, 270, 422, 574, 726, 878, 1030, 1182} {349, 436,
// 522, 608, 696, 782, 876, 953}
// 萨克斯 : 中心坐标 : {194.0f , 346.0f , 498.0f , 650.0f , 802.0f , 954.0f ,
// 1106.0f} {392.5f , 479.0f , 565.0f , 652.0f , 739.0f , 829.0f ,
// 914.5f}
// 扬琴 : 累加坐标 : {154, 303, 452, 598, 745, 892, 1041, 1188} {293, 402,
// 510, 615, 720, 828, 934, 1041}
// 扬琴 : 中心坐标 : {228.5f , 377.5f , 525.0f , 671.5f , 818.5f , 966.5f ,
// 1114.5f} {347.5f , 456.0f , 562.5f , 667.5f , 774.0f , 881.0f ,
// 987.5f}
float[] panel_design_width_acc = { 12, 149, 283, 416, 550, 681, 814, 948 };
float[] panel_design_width_acc2 = { 293, 402, 510, 615, 720, 828, 934, 1041 };
for (int i = 0; i < 7; i++) {
System.out.print((panel_design_width_acc[i] + panel_design_width_acc[i + 1]) / 2.0f + "f , ");
}
/*
* System.out.println();
*
* for(int i = 0; i < 7; i ++){
* System.out.print((panel_design_width_acc2[i] +
* panel_design_width_acc2[i + 1]) / 2.0f + "f , "); }
*/
}
// 给定左上值计算
public static void caculate_top_left() {
// 给出中心点坐标,图片宽高,屏幕宽高,计算出该图片的位置
// 屏幕宽高
float width = 750, height = 1334;
// 中心点坐标
float[][] left_top_data = {
{ 150, 412 },
{ 1013, 25 }
};
// 图片坐标,0位置是宽,1位置是高
float[][] width_height_data = {
{ 445, 519 },
{ 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");
}
}
/**
* 计算合奏界面中 每个 乐器 的位置 给定舞台中心的上方点, 乐器图片只能 平行 或 高于 该点
*/
public static void caculate_stage_instrument() {
System.out.println("\n\n计算舞台乐器图片信息 : \n\n");
// 给出中心点坐标,图片宽高,屏幕宽高,计算出该图片的位置
// 屏幕宽高
float width = 814, height = 374;
// 中心点坐标
float[][] stage_top_center = { { 0, 0 }, { 258, 135 }, { 440, 135 }
};
// 图片坐标,0位置是宽,1位置是高
float[][] width_height_data = { { 814, 80 }, { 118, 118 }, { 118, 118 }, };
for (int i = 0; i < stage_top_center.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 = stage_top_center[i][0] - width_height_data[i][0] / 2.0f;
float top = stage_top_center[i][1] - width_height_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);
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_marginLeftPercent=\"" + margin_parent_left * 100
+ "%\"\n" + "app:layout_marginTopPercent=\"" + margin_parent_top * 100 + "%\"\n"
+ "android:scaleType=\"fitXY\"\n" + "android:src=\"@mipmap/actual_\"\n");
}
}
// 给定边界计算中心值
public static void caculate_center() {
// 给出中心点坐标,图片宽高,屏幕宽高,计算出该图片的位置
// 屏幕宽高
float width = 1334, height = 750;
// 中心点坐标
float[][] center_data = {
// 72.5f , 210.5f , 344.5f , 478.5f , 611.5f , 744.0f , 877.0f
{ 80.5f, 262.5f }, { 216.0f, 262.5f }, { 349.5f, 262.5f }, { 483.0f, 262.5f },
{ 615.5f, 262.5f }, { 747.5f, 262.5f }, { 881.0f, 262.5f }
// { 507, 687 }
};
// 图片坐标,0位置是宽,1位置是高
float[][] width_height_data = { { 200, 364 }, { 200, 364 }, { 200, 364 }, { 200, 364 },
{ 200, 364 }, { 200, 364 }, { 200, 364 }
// { 131, 131 }
};
for (int i = 0; i < center_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 = center_data[i][0] - width_height_data[i][0] / 2.0f;
float top = center_data[i][1] - width_height_data[i][1] / 2.0f;
float right = center_data[i][0] + width_height_data[i][0] / 2.0f;
float bottom = center_data[i][1] + width_height_data[i][1] / 2.0f;
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 margin_parent_right = right / width;
margin_parent_right = 1 - margin_parent_right;
margin_parent_right = format_float(margin_parent_right);
float margin_parent_bottom = bottom / height;
margin_parent_bottom = 1 - margin_parent_bottom;
margin_parent_bottom = format_float(margin_parent_bottom);
System.out.println("第" + i + "个点 : \n" + left + " , " + top + " , " + right + " , " + bottom);
System.out.println(margin_parent_left + " , " + margin_parent_top + " , " + margin_parent_right + " , "
+ margin_parent_bottom);
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_marginLeftPercent=\"" + margin_parent_left * 100
+ "%\"\n" + "app:layout_marginTopPercent=\"" + margin_parent_top * 100 + "%\"\n"
+ "app:layout_marginRightPercent=\"" + margin_parent_right * 100 + "%\"\n"
+ "app:layout_marginBottomPercent=\"" + margin_parent_bottom * 100 + "%\"\n"
+ "android:scaleType=\"fitXY\"\n" + "android:src=\"@mipmap/actual_\"\n");
}
}
public static float format_float(float num) {
BigDecimal bigDecimal = new BigDecimal(num);
float result = bigDecimal.setScale(5, BigDecimal.ROUND_HALF_UP).floatValue();
// if(result <= 0) result = 0;
// if(result >= 1) result = 1;
return result;
}
public static void caculate_mouse_position() {
float DESIGN_HOLE_CENTER[][][] = {
{ { 330, 212 }, { 330, 352 }, { 330, 490 }, { 330, 629 }, { 136, 212 }, { 136, 352 }, { 136, 490 },
{ 136, 629 } },
{ { 764, 212 }, { 764, 352 }, { 764, 490 }, { 764, 629 }, { 570, 212 }, { 570, 352 }, { 570, 490 },
{ 570, 629 } },
{ { 1208, 212 }, { 1208, 352 }, { 1208, 490 }, { 1208, 629 }, { 1014, 212 }, { 1014, 352 },
{ 1014, 490 }, { 1014, 629 } } };
float DESIGN_HEIGHT = 750;
float DESIGN_WIDTH = 1334;
float DESIGN_MOUSE_HEIGHT_MINUS = 155;
float DESIGN_MOUSE_WIDTH_MINUS = 81;
float MOUSE_DESIGN_HEIGHT = 155;
float MOUSE_DESIGN_WIDTH = 163;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 8; j++) {
float width_percent = MOUSE_DESIGN_WIDTH / DESIGN_WIDTH;
width_percent = format_float(width_percent);
float height_percent = MOUSE_DESIGN_HEIGHT / DESIGN_HEIGHT;
height_percent = format_float(height_percent);
float left = DESIGN_HOLE_CENTER[i][j][0] - DESIGN_MOUSE_WIDTH_MINUS;
float top = DESIGN_HOLE_CENTER[i][j][1] - DESIGN_MOUSE_HEIGHT_MINUS;
float margin_parent_left = left / DESIGN_WIDTH;
margin_parent_left = format_float(margin_parent_left);
float margin_parent_top = top / DESIGN_HEIGHT;
margin_parent_top = format_float(margin_parent_top);
/*
* System.out.println( "<ImageView\n" +
* "android:id=\"@+id/game_1_hammer_" + i + "_" + j + "\"\n" +
* "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_marginLeftPercent=\"" +
* margin_parent_left * 100 + "%\"\n" +
* "app:layout_marginTopPercent=\"" + margin_parent_top * 100 +
* "%\"\n" + "android:scaleType=\"fitXY\"\n" +
* "android:visibility=\"gone\"\n" +
* "android:src=\"@mipmap/game_1_hammer\"/>\n\n" );
*/
System.out.print("R.id." + "game_1_hammer_" + i + "_" + j + ", ");
}
System.out.println();
}
}
}