First, let's look at their basic concepts:
px: The pixels on the screen
dp: An abstract unit based on density. If a screen is 160dpi, 1dp=1px
dip: Equivalent to dp
sp: Similar to dp, but it also scales according to user font size preferences (it is recommended that sp be used as a unit of text, and dip be used for others)
In view of the relationship between dip and px, the following summaries are made:
1. PX (pixels) pixels:
A pixel is usually regarded as the smallest complete sampling of an image, which is used more often, especially in web development. Pages basically use pixels as a unit.
2. dip or dp (device independent pixels):
Device Independent Pixels - This is related to device hardware. Usually we support multiple resolutions on mobile phones, such as WVGA and HVGA.
Both QVGA and dip are used as units of length.
Let's look at the relationship between screen type, density and resolution.
QVGA screen density=120. QVGA(240*320)
HVGA screen density=160) HVGA(320*480)
WVGA Screen density=240 * WVGA(480*800)
WQVGA screen density=120.WQVGA(240*400)
Note: The density value represents how many display points per inch, and the resolution is two concepts.
Screen resolution information is different under different densities. Take WVGA(density=240) of 480 dip * 800 dip as an example.
1. When density=120
Conversion: Conversion coefficient = 120/240
The actual screen resolution is 240px*400px (two points correspond to one resolution)
The status bar and title bar are 19 PX or 25 dip in height
The horizontal screen is 400px or 800dip in screen width, 211px or 480dip in working area height.
Screen width 240 PX or 480 dip, working area height 381 PX or 775 dip for vertical screen
2. When density=160
Conversion: Conversion coefficient = 160/240
The actual screen resolution is 320px*533px (three points correspond to two resolutions)
The status bar and title bar are 25px or 25dip tall
The horizontal screen is 533px or 800dip in screen width and 295px or 480dip in working area height.
Screen width 320px or 480dip, working area height 508px or 775dip for vertical screen
3. When density=240
Conversion: Conversion coefficient = 240/240
The actual screen resolution is 480px*800px (a point for a resolution)
The status bar and title bar are 38 PX or 25 dip tall
The horizontal screen is 800px or 800dip in screen width, 442px or 480dip in working area height.
The screen width is 480 PX or 480 dip, and the working area is 762 PX or 775 dip.
We usually define multiple adapter resource folders (values-XXX,drawable-XXX, etc.) in our projects.
drawable-ldpi: Mobile devices with 120 screen densities
Draable-mdpi: Mobile phone device with screen density of 160 (this is baseline, others are based on this, on this device, 1dp = 1px)
Draable-hdpi: Mobile devices with a screen density of 240
drawable-xhdpi: Mobile device with screen density of 320
drawable-xxhdpi: a mobile device with a screen density of 480
(Values is the same, of course, one more thing to note: Values and values-hdpi have the same effect, drawable and drawable-hdpi have the same effect, so generally we will store the same value in both folders, if both have better fit)
In the apk resource bundle
When screen density=240, use hdpi tag resources
When screen density=160, use mdpi tag resources
When screen density=120, use ldpi tagged resources
When screen density=320, use xhdpi tag resources
When screen density=480, use xxhdpi tag resources
Resources without any labels are shared at all resolutions
So use unit dip as much as possible and use less px in layout.
dp and px conversion formula:
pixs =dips * (densityDpi/160).
dips=(pixs*160)/densityDpi
But we also need an offset when we translate it into code: 0.5f
private static final float scale = mContext.getResources().getDisplayMetrics().density; private static final float scaledDensity = mContext.mContext.getResources().getDisplayMetrics().scaledDensity; /** * dp Turn to px * @param dipValue * @return */ public static int dip2px(float dipValue) { return (int) (dipValue * scale + 0.5f); } /** * px Turn to dp * @param pxValue * @return */ public static int px2dip(float pxValue) { return (int) (pxValue / scale + 0.5f); } /** * sp Turn to px * @param spValue * @param type * @return */ public static float sp2px(float spValue, int type) { switch (type) { case CHINESE: return spValue * scaledDensity; case NUMBER_OR_CHARACTER: return spValue * scaledDensity * 10.0f / 18.0f; default: return spValue * scaledDensity; } }
We see that scale here is a global variable defined in this class DisplayMetrics. In fact, this value is the current cell phone density/160. Scale Density is used to transform between px and sp, which is similar to scale. Another point is that there will be an offset value processing in the transformation.
The above is reproduced from: https://blog.csdn.net/jiangwei0910410003/article/details/40509571
======================================= partition line====================================
Android Screen Adaptation Projects can be adapted directly with dp and sp without changing a single line of code.
It's not clear whether compatibility and performance issues will take time to test.
At present, the basic effect of different resolution and different dpi of the simulator tested will not be much worse.
Rotating the screen layout will zoom in and out of view according to the size of the horizontal version.
package com.dzw.demo.utils; import android.app.Activity; import android.app.Application; import android.content.Context; import android.graphics.Point; import android.os.Bundle; import android.view.WindowManager; import static android.content.Context.WINDOW_SERVICE; public class ScreenAdaptation { private Application.ActivityLifecycleCallbacks activityLifecycleCallbacks; private Application mApplication; private float mWidth = 720; private float mHeight = 1280; public ScreenAdaptation(Application application, float width, int height) { mApplication = application; mWidth = width; mHeight = height; activityLifecycleCallbacks = new Application.ActivityLifecycleCallbacks() { @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { //Open Activity to execute resetDensity(activity, mWidth, mHeight); } @Override public void onActivityStarted(Activity activity) { } @Override public void onActivityResumed(Activity activity) { } @Override public void onActivityPaused(Activity activity) { } @Override public void onActivityStopped(Activity activity) { } @Override public void onActivitySaveInstanceState(Activity activity, Bundle outState) { } @Override public void onActivityDestroyed(Activity activity) { } }; } /** * register */ public void register() { resetDensity(mApplication, mWidth, mHeight); mApplication.registerActivityLifecycleCallbacks(activityLifecycleCallbacks); } /** * Cancellation */ public void unregister() { //set as default mApplication.getResources().getDisplayMetrics().setToDefaults(); mApplication.unregisterActivityLifecycleCallbacks(activityLifecycleCallbacks); } /** * dp Adapt getResources().getDisplayMetrics().density * <p> * sp Adapt getResources().getDisplayMetrics().scaledDensity * <p> * pt Adapt getResources().getDisplayMetrics().xdpi * * @paramcontext * @paramwidthui Width of design drawings * @paramheightui Height of design drawings */ private static void resetDensity(Context context, float width, float height) { Point point = new Point(); //Get the number of screens ((WindowManager) context.getSystemService(WINDOW_SERVICE)).getDefaultDisplay().getSize(point); //dp adapts getResources().getDisplayMetrics().density context.getResources().getDisplayMetrics().density = point.x / width * 2f; context.getResources().getDisplayMetrics().density = point.y / height * 2f; //sp adapts getResources().getDisplayMetrics().scaledDensity context.getResources().getDisplayMetrics().scaledDensity = point.x / width * 2f; context.getResources().getDisplayMetrics().scaledDensity = point.y / height * 2f; } }
Call in onCreate of Application:
new ScreenAdaptation(this,720,1280).register();
The above is reproduced from: https://www.jianshu.com/p/97b107308923