33
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
/*
|
||||
* Copyright (c) 2015 The CCP project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a Beijing Speedtong Information Technology Co.,Ltd license
|
||||
* that can be found in the LICENSE file in the root of the web site.
|
||||
*
|
||||
* http://www.yuntongxun.com
|
||||
*
|
||||
* An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/package cn.shangyu.gdxzExpert.utils;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
import android.content.res.Configuration;
|
||||
import android.content.res.Resources;
|
||||
import android.os.Build;
|
||||
import android.util.DisplayMetrics;
|
||||
import android.view.Display;
|
||||
import android.view.WindowManager;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import cn.shangyu.gdxzExpert.application.BaseApplication;
|
||||
|
||||
/**
|
||||
* Created by Jorstin on 2015/3/18.
|
||||
*/
|
||||
public class DensityUtil {
|
||||
|
||||
// 根据屏幕密度转换
|
||||
private static float mPixels = 0.0F;
|
||||
private static float density = -1.0F;
|
||||
|
||||
/**
|
||||
*
|
||||
* @param context
|
||||
* @param pixels
|
||||
* @return
|
||||
*/
|
||||
public static int getDisplayMetrics(Context context, float pixels) {
|
||||
if (mPixels == 0.0F)
|
||||
mPixels = context.getResources().getDisplayMetrics().density;
|
||||
return (int) (0.5F + pixels * mPixels);
|
||||
}
|
||||
|
||||
|
||||
public static int getImageWeidth(Context context , float pixels) {
|
||||
LogUtil.e("screen width " + context.getResources().getDisplayMetrics().widthPixels);
|
||||
return context.getResources().getDisplayMetrics().widthPixels - 66 - getDisplayMetrics(context, pixels);
|
||||
}
|
||||
|
||||
/**
|
||||
* 像素转化dip
|
||||
* @param context
|
||||
* @param pxValue
|
||||
* @return
|
||||
*/
|
||||
public static int px2dip(Context context, float pxValue){
|
||||
|
||||
final float scale = context.getResources().getDisplayMetrics().density;
|
||||
|
||||
return (int)(pxValue / scale + 0.5f);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* dip转化像素
|
||||
* @param dipValue
|
||||
* @return
|
||||
*/
|
||||
public static int dip2px(float dipValue){
|
||||
final float scale = Resources.getSystem().getDisplayMetrics().density;
|
||||
return (int)(dipValue * scale + 0.5f);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* @param context
|
||||
* @param height
|
||||
* @return
|
||||
*/
|
||||
public static int getMetricsDensity(Context context , float height) {
|
||||
DisplayMetrics localDisplayMetrics = new DisplayMetrics();
|
||||
((WindowManager) context.getSystemService(Context.WINDOW_SERVICE))
|
||||
.getDefaultDisplay().getMetrics(localDisplayMetrics);
|
||||
return Math.round(height * localDisplayMetrics.densityDpi / 160.0F);
|
||||
}
|
||||
|
||||
|
||||
public static int fromDPToPix(Context context, int dp) {
|
||||
return Math.round(getDensity(context) * dp);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param context
|
||||
* @return
|
||||
*/
|
||||
public static float getDensity(Context context) {
|
||||
if (context == null)
|
||||
context = BaseApplication.getInstance().getApplicationContext();
|
||||
if (density < 0.0F)
|
||||
density = context.getResources().getDisplayMetrics().density;
|
||||
return density;
|
||||
}
|
||||
|
||||
public static int round(Context context, int paramInt) {
|
||||
return Math.round(paramInt / getDensity(context));
|
||||
}
|
||||
|
||||
/**
|
||||
* 保持字体大小不随系统设置变化(用在界面加载之前)
|
||||
* 要重写Activity的attachBaseContext()
|
||||
*/
|
||||
public static Context attachBaseContext(Context context, float fontScale) {
|
||||
Configuration config = context.getResources().getConfiguration();
|
||||
//正确写法
|
||||
config.fontScale = fontScale;
|
||||
return context.createConfigurationContext(config);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保持字体大小不随系统设置变化(用在界面加载之前)
|
||||
* 要重写Activity的getResources()
|
||||
*/
|
||||
public static Resources getResources(Context context, Resources resources, float fontScale) {
|
||||
Configuration config = resources.getConfiguration();
|
||||
if(config.fontScale != fontScale) {
|
||||
config.fontScale = fontScale;
|
||||
return context.createConfigurationContext(config).getResources();
|
||||
} else {
|
||||
return resources;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存字体大小,后通知界面重建,它会触发attachBaseContext,来改变字号
|
||||
*/
|
||||
public static void recreate(Activity activity) {
|
||||
activity.recreate();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 系统设置"显示大小"时原有UI样式保持不变:
|
||||
* <p>
|
||||
* 1、当调节手机系统"显示大小"【调大】的时候,相应的dpi会变大【dp = (dpi/160) * px】,此时dp就会变大,所以相应的UI布局就会变大。
|
||||
* 2、当调节手机系统"分辨率"【调小】的时候,相应的dpi会变小【比如由480-->320】。如果此时使用技术手段使dpi保持不变,那么相同的dp就会占用更多的px,所以UI布局就会变大。
|
||||
*
|
||||
* @param context
|
||||
*/
|
||||
public static void setDefaultDisplay(Context context) {
|
||||
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
|
||||
Configuration origConfig = context.getResources().getConfiguration();
|
||||
//获取手机出厂时默认的densityDpi【注释1】
|
||||
origConfig.densityDpi = getDefaultDisplayDensity();
|
||||
|
||||
context.getResources().updateConfiguration(origConfig, context.getResources().getDisplayMetrics());
|
||||
}
|
||||
}
|
||||
|
||||
public static int getDefaultDisplayDensity() {
|
||||
try {
|
||||
Class clazz = Class.forName("android.view.WindowManagerGlobal");
|
||||
Method method = clazz.getMethod("getWindowManagerService");
|
||||
method.setAccessible(true);
|
||||
Object iwm = method.invoke(clazz);
|
||||
Method getInitialDisplayDensity = iwm.getClass().getMethod("getInitialDisplayDensity", int.class);
|
||||
getInitialDisplayDensity.setAccessible(true);
|
||||
Object densityDpi = getInitialDisplayDensity.invoke(iwm, Display.DEFAULT_DISPLAY);
|
||||
return (int) densityDpi;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user