天天看點

【Android 螢幕适配】螢幕适配通用解決方案 ⑦ ( PercentRelativeLayout 百分比布局方案 | 該布局已廢棄本方案僅做參考 )

文章目錄

  • ​​一、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_"      
<?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>