码迷,mamicode.com
首页 > 其他好文 > 详细

Density

时间:2016-04-19 06:22:50      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:

package com.k1.frame.utils;

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

public class Density {

    public static final float DEFAULT_SCALE = 2;

    private static float scale = 1.0f;
    private static float fontScale = 1.0f;
    private static int statusBarHeight = 40;
    private static int screenWidth = 720;
    private static int screenHeight = 1280;

    public static void init (Context context) {
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        scale = dm.density;
        fontScale = dm.scaledDensity;
        screenWidth = dm.widthPixels;
        screenHeight = dm.heightPixels;
        statusBarHeight = getStatusBarHeight(context);
    }

    private static int getStatusBarHeight(Context context) {
        int result = 0;
        int resourceId = context.getResources().getIdentifier(
                "status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
    
    public static int statusBarHeight() {
        return statusBarHeight;
    }

    public static int screenWidth() {
        return screenWidth;
    }
    
    public static int screenHeight() {
        return screenHeight;
    }

    public static int dip2px(float dpValue) {
        return (int) (dpValue * scale + 0.5f);
    }

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

    public static int px2sp(float pxValue) {
        return (int) (pxValue / fontScale + 0.5f);
    }

    public static int sp2px(float spValue) {
        return (int) (spValue * fontScale + 0.5f);
    }
}

 

Density

标签:

原文地址:http://www.cnblogs.com/g-sheng/p/5406520.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!