public class Density {
private static float appDensity;
private static float appScaledDensity;
private static DisplayMetrics appDisplayMetrics;
private static int barHeight;
public static void setDensity(@NonNull final Activity application){
//获取application的DisplayMetrics
appDisplayMetrics=application.getResources().getDisplayMetrics();
//获取状态栏高度
barHeight=AppUtils.getStateBar2(application);
if(appDensity==0){
//初始化的时候赋值
appDensity=appDisplayMetrics.density;
appScaledDensity=appDisplayMetrics.scaledDensity;
//添加字体变化的监听
application.registerComponentCallbacks(new ComponentCallbacks(){
@Override
public void onConfigurationChanged(Configuration newConfig){
//字体改变后,将appScaledDensity重新赋值
if(newConfig!=null&&newConfig.fontScale>0){
appScaledDensity=application.getResources().getDisplayMetrics().scaledDensity;
}
}
@Override
public void onLowMemory(){
}
});
}
}
//此方法在BaseActivity中做初始化(如果不封装BaseActivity的话,直接用下面那个方法就好了)
public static void setDefault(Activity activity){
setAppOrientation(activity,AppUtils.HEIGHT);
}
//此方法用于在某一个Activity里面更改适配的方向
public static void setOrientation(Activity activity, String orientation){
setAppOrientation(activity,orientation);
}
/**
* targetDensity
* targetScaledDensity
* targetDensityDpi
* 这三个参数是统一修改过后的值
* <p>
* orientation:方向值,传入width或height
*/
private static void setAppOrientation(@Nullable Activity activity, String orientation){
float targetDensity;
if(orientation.equals("height")){
targetDensity=(appDisplayMetrics.heightPixels-barHeight)/1280f;
}else{
targetDensity=appDisplayMetrics.widthPixels/800f;
}
float targetScaledDensity=targetDensity*(appScaledDensity/appDensity);
int targetDensityDpi=(int)(160*targetDensity);
/**
*
* 最后在这里将修改过后的值赋给系统参数
*
* 只修改Activity的density值
*/
DisplayMetrics activityDisplayMetrics=activity.getResources().getDisplayMetrics();
activityDisplayMetrics.density=targetDensity;//相对密度 dp和px的比例
activityDisplayMetrics.scaledDensity=targetScaledDensity;
activityDisplayMetrics.densityDpi=targetDensityDpi;
}
}
状态栏,dp和px转换
public class AppUtils {
public final static String WIDTH = "1080";
public final static String HEIGHT = "1920";
/**
* px转dp
*
* @param context The context
* @param px the pixel value
* @return value in dp
*/
public static int pxToDp(Context context, float px) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
return (int) ((px / displayMetrics.density) + 0.5f);
}
/**
* dp转px
*
* @param context
* @param dp
* @return
*/
public static int dpToPx(Context context, float dp) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
return (int) ((dp * displayMetrics.density) + 0.5f);
}
public static int getStateBar3(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 getStateBar2(Context context) {
Class c = null;
try {
c = Class.forName("com.android.internal.R$dimen");
Object obj = c.newInstance();
Field field = c.getField("status_bar_height");
int x = Integer.parseInt(field.get(obj).toString());
int statusBarHeight = context.getResources().getDimensionPixelSize(x);
return statusBarHeight;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
/**
* 获取状态栏高度
*
* @param context
* @return
*/
public 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;
}
}
界面设置
super.onCreate(savedInstanceState);
//无title
requestWindowFeature(Window.FEATURE_NO_TITLE);
//全屏
getWindow().setFlags(WindowManager.LayoutParams. FLAG_FULLSCREEN ,
WindowManager.LayoutParams. FLAG_FULLSCREEN);
setContentView(R.layout.activity_sw);
网友评论