android development dip and pixel switching

Keywords: Android

Original Link: http://www.cnblogs.com/carmanloneliness/archive/2012/10/18/2729977.html

DisplayMetrics class - Structured general information describing a display, including its size, density, and character scale.

public float density; //screen pixel density value, density value indicates how many display points per inch, two different concepts from resolution.

Android has the following screens:

QVGA and WQVGA screen density=120;

HVGA screen density=160;

WVGA screen density=240;

Following is a detailed list of screen resolution information under different densities, taking the WVGA(density=240) of 480dip*800dip as an example:

The actual screen resolution is 240px*400px when density=120 (two points correspond to one resolution)
19px or 25dip for status bar and title bar
The horizontal screen is 400px or 800dip in width, 211px or 480dip in height of the work area
Screen width 240px or 480dip, work area height 381px or 775dip when vertical screen

Actual screen resolution at density=160 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 width, 295px or 480dip in height of the work area
Screen width 320px or 480dip, work area height 508px or 775dip when vertical screen

The actual screen resolution at density=240 is 480px*800px (one point for one resolution)
The status bar and title bar are 38px or 25dip tall
The horizontal screen is 800px or 800dip screen width, 442px or 480dip work area height
Screen width 480px or 480dip, work area height 762px or 775dip when vertical screen

Resource in apk's resource bundle that uses the hdpi tag when screen density=240
Resources using mdpi tags when screen density=160
When screen density=120, use resources labeled with ldpi.
Resources without any labels are common at all resolutions.
Suggestion: Try to use unit dip instead of px in the layout.

Device independent pixels. Different devices have different display effects, which are related to the hardware of the device. Generally, we recommend this for supporting WVGA, HVGA and QVGA, independent of pixels.

import android.content.Context;
import android.util.DisplayMetrics;


//The calculation formula pixels = dips * (density / 160)

public class DensityUtil {
   
    private static final String TAG = DensityUtil.class.getSimpleName();
   
    // The densityDpi of the current screen
    private static float dmDensityDpi = 0.0f;
    private static DisplayMetrics dm;
    private static float scale = 0.0f;

   
    public DensityUtil(Context context) {
        // Get the current screen
        dm = new DisplayMetrics();

        //Returns DispatchMetrics information for the current resource object.
        dm = context.getApplicationContext().getResources().getDisplayMetrics();
        // Set DensityDpi
        setDmDensityDpi(dm.densityDpi);
        // Density factor
        scale = getDmDensityDpi() / 160;//Equals scale=dm.density;
        Logger.i(TAG, toString());
    }

   
    public static float getDmDensityDpi() {
        return dmDensityDpi;
    }

   
    public static void setDmDensityDpi(float dmDensityDpi) {
        DensityUtil.dmDensityDpi = dmDensityDpi;
    }

   
    public static int dip2px(float dipValue) {

        return (int) (dipValue * scale + 0.5f);

    }

   
    public int px2dip(float pxValue) {
        return (int) (pxValue / scale + 0.5f);
    }

    @Override
    public String toString() {
        return " dmDensityDpi:" + dmDensityDpi;
    }
}

//Others:

//dip to pixel
 public static int DipToPixels(Context context,int dip) {
  final float SCALE = context.getResources().getDisplayMetrics().density;
  float valueDips =  dip;
  int valuePixels = (int)(valueDips * SCALE + 0.5f); 
  return valuePixels;

 }


 //Pixel to dip
 public static float PixelsToDip(Context context,int Pixels) {
  final float SCALE = context.getResources().getDisplayMetrics().density;
  float dips =Pixels / SCALE ;
  return dips;

 }

//Specify Picture Length and Width to Generate a New Picture

public static Bitmap decodeBitmap(Bitmap initialBitmap, int height, int weight) {
        int bmpHeight = initialBitmap.getHeight();
        int bmpWeight = initialBitmap.getWidth();
        float scale = Math.min(height / bmpHeight, weight / bmpWeight);
        Bitmap mutableBitmap = Bitmap.createScaledBitmap(initialBitmap, (int) (bmpWeight * scale), (int) (bmpHeight * scale), true);//Specify the width and length of the picture to generate a new one
        return mutableBitmap;
    }

//Save Bitmap as the specified JPG file   

public static void writePhotoJpg(Bitmap data, String pathName) {
        File file = new File(pathName);
        try {
            file.createNewFile();
            // BufferedOutputStream os = new BufferedOutputStream(
            // new FileOutputStream(file));

            FileOutputStream os = new FileOutputStream(file);
            data.compress(Bitmap.CompressFormat.JPEG, 100, os);
            os.flush();
            os.close();
            MyDebug.i("writePhotoJpg");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
//Save Bitmap as the specified PNG file   
    public static void writePhotoPng(Bitmap data, String pathName) {
        File file = new File(pathName);
        try {
            file.createNewFile();
            FileOutputStream os = new FileOutputStream(file);
            // BufferedOutputStream os = new BufferedOutputStream(
            // new FileOutputStream(file));
            data.compress(Bitmap.CompressFormat.PNG, 100, os);
            os.flush();
            os.close();
            MyDebug.i("writePhotoPng");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

  

Reprinted at: https://www.cnblogs.com/carmanloneliness/archive/2012/10/18/2729977.html

Posted by menriquez on Mon, 05 Aug 2019 14:18:33 -0700