adapter


When it comes to adaptation, I mainly tell about the two modules in Android that need adaptation, the first view adaptation, the second permission adaptation, today from two aspects to thoroughly solve the adaptation problem




View adapter





Density-independent pixels (DP) vs DPI

  • Density -independent pixel, called DP or DIP, is independent of the actual physical pixels on the terminal
  • Unit: DP, which can ensure the same effect on devices with different screen pixel densities. It is an Android length unit.
  • Scenario example: If both draw a line half the length of the screen, if px is used as the unit of measurement, set it to 240px on the 480×800 resolution phone. On a 320×480 phone it should be 160px, but the Settings are different. If you use dp as a unit, 160DP is displayed as half the length of the screen at both resolutions.
  • Px = density * dp
  • Px = dp * (dpi / 160) density = Dpi / 160












From this, we can see that screen adaptation has an inseparable relationship with the size and screen density of our phones. Major Android phone manufacturers are often inaccurate in terms of screen density, which may be the reason for buying something. Therefore, THE DP provided by Google is not very easy to use
But for the same aspect ratio of mobile phone adaptation is no problem




My recommended adaptation:

Headline fit solution (90% of problems solved) :
  • Px = density * dp
  • Px = dp * (dpi / 160) density = Dpi / 160
The principle is simple: Denstiy changes to a live variable, and each phone recalculates the value
Qualifier adaptation: This is the most accurate adaptation

package com.example.androidtest;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintWriter;

public class MakeXml {
    private final static String rootPath = "D:\\layoutroot\\values-{0}x{1}\\";

    private final static float dw = 320f;
    private final static float dh = 480f;

    private final static String WTemplate = "{1}px\n";
    private final static String HTemplate = "{1}px\n";

    public static void main(String[] args) {
        makeString(320, 480);
        makeString(480,800);
        makeString(480, 854);
        makeString(540, 960);
        makeString(600, 1024);
        makeString(720, 1184);
        makeString(720, 1196);
        makeString(720, 1280);
        makeString(768, 1024);
        makeString(800, 1280);
        makeString(1080, 1812);
        makeString(1080, 1920);
        makeString(1440, 2560);
    }

    public static void makeString(int w, int h) {

        StringBuffer sb = new StringBuffer();
        sb.append("\n");
        sb.append("");
        float cellw = w / dw;
        System.out.println("w= " + w + " , h= " + h + " , cellw= " + cellw + " , dw= " + dw);
        for (int i = 1; i < 320; i++) {
            sb.append(WTemplate.replace("{0}", i + "").replace("{1}",
                    change(cellw * i) + ""));
            System.out.println("i = " + i + " , cellw*i = " + cellw *i);
        }
        sb.append(WTemplate.replace("{0}"."320").replace("{1}", w + ""));
        sb.append("");

        StringBuffer sb2 = new StringBuffer();
        sb2.append("\n");
        sb2.append("");
        float cellh = h / dh;
        for (int i = 1; i < 480; i++) {
            sb2.append(HTemplate.replace("{0}", i + "").replace("{1}",
                    change(cellh * i) + ""));
        }
        sb2.append(HTemplate.replace("{0}"."480").replace("{1}", h + ""));
        sb2.append("");

        String path = rootPath.replace("{0}", h + "").replace("{1}", w + "");
        File rootFile = new File(path);
        if(! rootFile.exists()) { rootFile.mkdirs(); } File layxFile = new File(path +"lay_x.xml");
        File layyFile = new File(path + "lay_y.xml");
        try {
            PrintWriter pw = new PrintWriter(new FileOutputStream(layxFile));
            pw.print(sb.toString());
            pw.close();
            pw = new PrintWriter(new FileOutputStream(layyFile));
            pw.print(sb2.toString());
            pw.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    public static float change(float a) {
        int temp = (int) (a * 100);
        returntemp / 100f; }}Copy the code

Use the command line to execute the Java program and place the generated values in app\main\res




Independent proportional pixel (SP)
  • Scale-independent Pixel is sp or SIP
  • Unit: SP, font size unit
  • Android development uses this unit to set the text size, which can be scaled according to the font size preference;
  • It is recommended to use 12sp, 14sp, 18sp, and 22sp as font sizes. Odd numbers and decimals are not recommended because the font size below 12sp is too small


Sp and DP difference
  • Dp only depends on the pixel density of the screen;
  • Sp is similar to DP but the only difference is that Android allows users to customize the text size (small, normal, large, Large, etc.). 1SP = 1DP =0.00625 inches when the text size is “normal” and 1SP >1dp=0.00625 inches when the text size is “large” or “large”. Similar to what happens when we resize the font in Windows – the window size stays the same, but the text size changes


Adaptation of layout components
  • Specify dimension DP using density-independent pixels
  • Use relative or linear layouts, not absolute ones
  • Use wrap_content, match_parent, weights
  • Use attributes such as minWidth, minHeight, and lines
  • Use dimens
Liu Haibing
Google official bangs screen adaptation scheme
  • https://developer.android.com/reference/android/view/DisplayCutout
Each major manufacturer’s plan
  • Huawei: https://devcentertest.huawei.com/consumer/cn/devservice/doc/50114
  • OPPO:https://open.oppomobile.com/service/message/detail?id=61876
  • vivo: https://dev.vivo.com.cn/doc/document/info?id=103
Folding screen
Huawei: developer.huawei.com/consumer/cn…


Permissions adaptation

6.0 Dynamically Obtaining Permissions

lNormal Permissions
lDangerous Permission
Check the command
  • Adb shell PM list permissions -d -g
  • Apply for all
  • Baidu map
  • I have to write it again