https://github.com/feisher/ScreenAdapter
https://github.com/yatoooon/AndroidScreenAdaptation
https://github.com/Firedamp/Rudeness
来自 https://mp.weixin.qq.com/s/d9QCoBP6kV9VSWvVldVVwA
private static float sNonCompatDensity;
private static float sNonCompatScaleDensity;
private static void setCustomDensity(Activity activity,final Application application) {
final DisplayMetrics appDisplayMetrics = application.getResources().getDisplayMetrics();
if (sNonCompatDensity ==0) {
sNonCompatDensity = appDisplayMetrics.density;
sNonCompatScaleDensity = appDisplayMetrics.scaledDensity;
application.registerComponentCallbacks(new ComponentCallbacks() {
@Override
public void onConfigurationChanged(Configuration newConfig) {
sNonCompatScaleDensity =application.getResources().getDisplayMetrics().scaledDensity;
}
@Override
public void onLowMemory() {
}
});
}
//屏幕宽 的像素除以360 获得Density的值
final float targetDensity = (float)appDisplayMetrics.widthPixels /380;
final int targetDensityDpi = (int) (targetDensity *160);
final float targetScaledDensity = (int) (targetDensity * (sNonCompatScaleDensity /sNonCompatDensity));
appDisplayMetrics.density = targetDensity;
appDisplayMetrics.scaledDensity = targetScaledDensity;
appDisplayMetrics.densityDpi = targetDensityDpi;
//这里把scaledDensity的值 等于获得Density的值(会有bug 下面会解决)
final DisplayMetrics activityDisplayMetrics = application.getResources().getDisplayMetrics();
activityDisplayMetrics.density = targetDensity;
activityDisplayMetrics.scaledDensity = targetScaledDensity;
activityDisplayMetrics.densityDpi = targetDensityDpi;
}
网友评论