Screen adaptation base icon
drawable selector, shape label, frame animation list, density value, resolution density ratio
Drawable LDPI low resolution directory 120 240320 0.75 formula density value divided by 160
Drawable MDPI medium resolution directory 160 320480 1
Drawable hdpi high resolution directory 240 480800 1.5
Drawable xhdpi ultra high resolution directory 320 7201280 2
Drawable xxhpi ultra high resolution directory 480 1080 * 1920 3
Drawable xhdpi ultra high resolution directory
On a mobile phone with a screen density of 120, 1dp=0.75px
On a mobile phone with a screen density of 160, 1dp=1px
On a mobile phone with a screen density of 240, 1dp=1.5px
On a mobile phone with a screen density of 320, 1dp=2px
On a mobile phone with a screen density of 480, 1dp=3px
On a mobile phone with a screen density of 640, 1dp=4px
The width is 100px 160 100dp
The width is 100px 240 66.7dp
The width is 100px 320 50dp
The width is 100px 480 33.3dp
The width is 100px 640 25dp
Adapt to mainstream mobile phone screens in the market (Traffic Research Institute, global Compass)
Today's headlines fit
General implementation principle:
px = dp * density
Total screen width of current equipment (in pixels) / total width of design drawing (in dp) = density
mPx: total screen width of current device (change)
mDp: total width of design drawing (unchanged)
mDensity: mPx/mDp coefficient ratio (change)
1. Assuming that the total screen width of the current equipment is 1080px and the total width of the current design drawing is 375dp, the mDensity calculated from the above is 2.88
There is a 50dp50dp control on the screen. When converted to PX, it is 50dp2.88=144px
144px/1080px=0.133, that is, the actual proportion of this control in the screen is 0.133
2. Assuming that the total screen width of the current equipment is 1440 px and the total width of the current design drawing is 375dp, the mDensity calculated from the above is 3.84
There is a 50dp50dp control on the screen, which is converted to PX: 50dp 3.84= 192px
192px/1080px=0.133, that is, the actual proportion of this control in the screen is 0.133
It can be seen that the actual proportion of controls is the same with the change of screen size
Step 1:
1. Use steps:
implementation 'me.jessyan:autosize:1.2.1'
2. Declare the dimensions in the design drawing (if the sub unit is completely used for the project, you can directly fill in the design drawing dimensions to be filled in the AndroidManifest in pixels instead of converting pixels to dp)
<manifest> <application> <meta-data android:name="design_width_in_dp" android:value="360"/> <meta-data android:name="design_height_in_dp" android:value="640"/> </application> </manifest>
3. Sub units can be set in myapplication (remember to register the list file) (only one of the three popular units pt, in and mm can be selected as the sub unit, and the unit selected will be used for layout in the layout file).
AutoSizeConfig.getInstance().getUnitsManager() .setSupportDP(false) .setSupportSP(false) .setSupportSubunits(Subunits.MM);
Sub units are used to avoid the adverse effects of modifying {@ link DisplayMetrics#density} on other system controls or third-party library controls using dp layout
Sub units are used to avoid the adverse effects of modifying {@ link DisplayMetrics#density} on other system controls or third-party library controls using dp layout
public enum Subunits { /** * Do not use sub units */ NONE, /** * Unit pt * * @see android.util.TypedValue#COMPLEX_UNIT_PT */ PT, /** * Unit in * * @see android.util.TypedValue#COMPLEX_UNIT_IN */ IN, /** * Unit mm * * @see android.util.TypedValue#COMPLEX_UNIT_MM */ MM
4.
/**
isBaseOnWidth()
Whether to perform proportional adaptation according to the width (in order to ensure normal adaptation on screens with different aspect ratios, only one of the width and height can be selected as the benchmark for adaptation)
@return {@code true} is fit according to width and {@ code false} is fit according to height
*/
/**
getSizeInDp()
Returns the design dimension on the design drawing, in dp
{@ link #getSizeInDp} must be used with {@ link #isBaseOnWidth()} according to the following rules:
If {@ link #isBaseOnWidth()} returns {@ code true}, {@link #getSizeInDp}, the total width of the design drawing should be returned
If {@ link #isBaseOnWidth()} returns {@ code false}, {@link #getSizeInDp}, the total height of the design drawing should be returned
If you do not need to customize the design size on the design drawing, you want to continue to use the design size filled in the AndroidManifest,
{@ link #getSizeInDp} returns {@ code 0}
@return the design dimension on the design drawing, in dp
Activity(BaseActivity)
public class CustomAdaptActivity extends AppCompatActivity implements CustomAdapt { @Override public boolean isBaseOnWidth() { return false; } @Override public float getSizeInDp() { return 667; } }
Fragment
public class CustomAdaptFragment extends Fragment implements CustomAdapt { @Override public boolean isBaseOnWidth() { return false; } @Override public float getSizeInDp() { return 667; } }
XML
<TextView android:layout_width="200mm" android:layout_height="100mm" android:background="@color/colorAccent" />
Full demo
<!-- this Demo It mainly shows the usage of sub units, If only sub units are used (pt,in,mm) You can directly fill in the size of the design drawing in pixels, No need to convert pixels to dp--> <meta-data android:name="design_width_in_dp" android:value="1080"/> <meta-data android:name="design_height_in_dp" android:value="1920"/>
BaseApplication
public class BaseApplication extends Application { @Override public void onCreate() { super.onCreate(); //Please complete the custom configuration of the company when the App starts configUnits(); } /** * Note!!! The real-time preview during layout is a very important link in the development stage. In many cases, the default preview device provided by Android Studio can not fully display our design drawings * Therefore, we need to create our own simulation equipment. The following link is for everyone's benefits. According to the operation in the link, the preview effect can be completely consistent with the design drawing! * @see <a href="https://github.com/JessYanCoding/AndroidAutoSize/blob/master/README-zh.md#preview">dp,pt,in,mm These four units of analog device creation methods</a> * <p> * v0.9.0 In the future, Android autosize will be upgraded to the extreme. Now it supports 5 units (dp, sp, pt, in, mm) * {@link UnitsManager} Users can freely configure the type of company they want to use * dp and sp are common units. As the main unit of AndroidAutoSize, they are supported by AndroidAutoSize by default * pt,in,mm These three units are relatively rare. You can only select one of them as the sub unit of AndroidAutoSize, which is supported by AndroidAutoSize together with dp and sp * Sub units are used to avoid the adverse effects of modifying {@ link DisplayMetrics#density} on other system controls or third-party library controls using dp layout * The unit you choose is the layout unit in the layout file * <p> * Two primary units and one secondary unit can be closed and reopened at any time using the method of {@ link UnitsManager} * If you want to completely avoid the adverse effects of modifying {@ link DisplayMetrics#density} on other system controls or third-party library controls using dp layout * Please call {@ link UnitsManager#setSupportDP} and {@ link UnitsManager#setSupportSP} and set them to {@ code false} * Stop supporting two primary companies (if you enable sp, it will have little impact on other third-party library controls, or you can not close the support for sp) * And call {@ link UnitsManager#setSupportSubunits} to select one of the three unpopular units as the sub unit * The effect of the three units is the same. Choose according to your preferences. For example, I like mm. Translated into Chinese means sister * Then, only this sub unit is used for layout in the layout file, so that the adverse effects caused by modifying {@ link DisplayMetrics#density} can be completely avoided * Because dp and sp are very common in other system controls or third-party library controls, but three unpopular units are very rare */ private void configUnits() { //AndroidAutoSize enables support for dp by default, and calls unitsmanager.setsupport dp (false); you can turn off support for dp //The reason why the primary company dp and the secondary company can be opened at the same time is that they are compatible with the pages that have been laid out with dp in the old project //Let developers gradually switch their old projects from dp to sub unit, that is, the new page is laid out with sub unit, and then take time to gradually change the layout unit of the old page from dp to sub unit //Finally, after changing all DPS to sub companies, use unitsmanager.setsupport dp (false); Turn off the support of dp to completely isolate the adverse effects caused by modifying the density //If the sub unit is completely used in the project, you can directly fill in the design drawing size to be filled in the AndroidManifest in pixels, without converting pixels to dp AutoSizeConfig.getInstance().getUnitsManager() .setSupportDP(false) //When the user wants to transition the old item from the primary company to the sub company, or from the sub company to the primary company //Because when using the primary unit, it is recommended to fill in the dp size of the design drawing in the AndroidManifest, such as 360 * 640 //The sub unit has a feature that it can directly fill in the px size of the design drawing in the Android manifest, such as 1080 * 1920 //However, only one set of design drawing dimensions can be filled in the Android manifest, and the design drawing dimensions of the main company have been filled in //Therefore, when there are both sub companies and primary companies in the project, and the design size of the sub company is different from that of the primary company, it can be configured through the UnitsManager#setDesignSize() method //If the design size of the secondary company is the same as that of the primary company, there is no need to call UnitsManager#setDesignSize(), and the frame will automatically use the design size filled in the AndroidManifest // .setDesignSize(2160, 3840) //AndroidAutoSize enables SP support by default, and calls UnitsManager.setSupportSP(false); You can turn off support for sp //If you turn off support for sp, you should fill in the font size in sub units during layout //If you enable SP, it will have little impact on other third-party library controls, or you can not turn off the support for sp. here I will continue to enable sp. please consider whether your project needs to turn off the support for sp // .setSupportSP(false) //AndroidAutoSize does not support sub units by default. Call UnitsManager#setSupportSubunits() to select a desired sub unit and enable support for sub units //You can only choose one of the three unpopular units pt, in and mm as the sub unit. The adaptation effects of the three units are actually the same. You can use whichever unit looks good to you //The unit you choose will be used for layout in the layout file. I choose mm as the unit for layout, because mm translates into Chinese, which means sister //If you don't have a sister in your life, we'll let the sister be the most important thing in the project! .setSupportSubunits(Subunits.MM); }
BaseActivity
public class BaseActivity extends AppCompatActivity implements CustomAdapt { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_custom_adapt); } /** * Whether to conduct proportional adaptation according to the width (in order to ensure normal adaptation on screens with different aspect ratios, only one of the width and height can be selected as the benchmark for adaptation) * * @return {@code true} To fit according to width, {@ code false} is to fit according to height */ @Override public boolean isBaseOnWidth() { return false; } /** * The design drawing of iPhone is used here. The size of the design drawing of iPhone is 750px * 1334px, because this page uses sub units for layout * Therefore, you can directly return the size of the design drawing in pixels * <p> * Returns the design dimension on the design drawing * {@link #getSizeInDp} It must be used in conjunction with {@ link #isBaseOnWidth()}. The rules are as follows: * If {@ link #isBaseOnWidth()} returns {@ code true}, {@link #getSizeInDp}, the total width of the design drawing should be returned * If {@ link #isBaseOnWidth()} returns {@ code false}, {@link #getSizeInDp}, the total height of the design drawing should be returned * If you do not need to customize the design size on the design drawing and want to continue to use the design size filled in AndroidManifest, {@ link #getSizeInDp} returns {@ code 0} * * @return Design dimensions on design drawings */ @Override public float getSizeInDp() { return 1334; }