Convert dimension To Pixel Strictly - Android Graphics

Android examples for Graphics:Pixel

Description

Convert dimension To Pixel Strictly

Demo Code


//package com.java2s;

import android.util.DisplayMetrics;

import android.util.TypedValue;

public class Main {
    private static float dimensionToPixelStrictly(int unit, float value,
            DisplayMetrics metrics) {/*from   w w w.  j  a  v  a  2 s.  c  o m*/
        switch (unit) {
        case TypedValue.COMPLEX_UNIT_PX:
            return value;
        case TypedValue.COMPLEX_UNIT_DIP:
            return value * metrics.density;
        case TypedValue.COMPLEX_UNIT_SP:
            return value * metrics.scaledDensity;
        case TypedValue.COMPLEX_UNIT_PT:
            return value * metrics.xdpi * (1.0f / 72);
        case TypedValue.COMPLEX_UNIT_IN:
            return value * metrics.xdpi;
        case TypedValue.COMPLEX_UNIT_MM:
            return value * metrics.xdpi * (1.0f / 25.4f);
        }
        return 0;
    }
}

Related Tutorials