Android 密度转换 java文件

package com.vinvo.android.games.limitlessDiamonds.utils;

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

public class DensityManager {
/**
* SoundManger的实例对象
*/
static private DensityManager densityInstance;
// 当前屏幕的density因子
private static float dmDensityDpi = 0.0f;
private static DisplayMetrics dm;
private static float scale = 0.0f;
private static final float CURR_SCALE = 1.5f;

private DensityManager(){
}

/**
* 获得SoundMangaer的实例
*
* @return SoundMangaer的实例
*/
static synchronized public DensityManager getInstance() {
if (densityInstance == null)
densityInstance = new DensityManager();
return densityInstance;
}
/**
* 初始化
* @param context
*/
public static void initDensity(Context context) {
dm = new DisplayMetrics();
dm = context.getApplicationContext().getResources().getDisplayMetrics();
dmDensityDpi=dm.densityDpi;
scale = dmDensityDpi / 160;
}

/**
* 根据当前屏幕的密度转换像素值
* @param pxValue 需要转换的像素值
* @return 转换后的像素值
*/
public static int transPxValue(int pxValue){
int dipValue =  (int) (pxValue / CURR_SCALE + 0.5f);
int result = (int) (dipValue * scale + 0.5f);
return result;
}
/**
* 清理实例
*/
public static void clearInstance(){
if (densityInstance != null){
densityInstance = null;
}
}
}

你可能感兴趣的:(java,android)