Resolution processing for Android programming related code segment collections
- 2020-11-03 22:36:10
- OfStack
An example of RESOLUTION processing for Android programming is presented in this paper. To share for your reference, the details are as follows:
1. Screen resolutions commonly referred to as 800x480, 960x540, etc. These resolutions are available in code. Mobile phone screen resolution calculation:
// Calculate your phone's screen resolution
private void computeDisplayMetrics() {
// The resolution of the mobile phone screen is heightxwidth
DisplayMetrics dm = new DisplayMetrics();
this.getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
// The current resolution of the mobile phone screen is width x height
}
2. Switch from dp to px according to the resolution of the phone:
public static int dip2px(Context context,float dpValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
3. Switch from px(pixels) to dp according to the resolution of the phone:
public static int px2dip(Context context,float pxValue) {
final float scale = context.getResources().getDisplayMetrics().density;
return (int) (pxValue / scale + 0.5f) - 15;
}
I hope this article has been helpful for Android programming.