今日头条的屏幕适配类原文链接
public class DensityUtils {
private static float sNonCompatDensity;
private static float sNonCompatScaleDensity;
private static final int WIDTH_DP = 360;
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) {
if (newConfig != null && newConfig.fontScale > 0) {
sNonCompatScaleDensity = application.getResources().getDisplayMetrics().scaledDensity;
}
}
@Override
public void onLowMemory() {
}
});
}
//假设设计图的宽度是360dp,则适配后的 density = 设备真实宽(单位px) / 360
final float targetDensity = appDisplayMetrics.widthPixels / WIDTH_DP;
final float targetScaleDensity = targetDensity * (sNonCompatScaleDensity / sNonCompatDensity);
final int targetDensityDpi = (int) (160 * targetDensity);
appDisplayMetrics.density = appDisplayMetrics.scaledDensity = targetDensity;
appDisplayMetrics.scaledDensity = targetScaleDensity;
appDisplayMetrics.densityDpi = targetDensityDpi;
final DisplayMetrics activityDispalyMetrics = activity.getResources().getDisplayMetrics();
activityDispalyMetrics.densityDpi = targetDensityDpi;
activityDispalyMetrics.scaledDensity = targetScaleDensity;
activityDispalyMetrics.density = activityDispalyMetrics.scaledDensity = targetDensity;
}
}