工具类:
package com.android.camera.util;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class DensityUtil {
public static boolean overrideDensityIfNeed(DisplayMetrics metrics , Context context) {
if (metrics == null) {
return false;
}
Configuration configuration = context.getResources().getConfiguration();
if (metrics.density != getDefaultDensity() || configuration.fontScale != 1.0f) {
return true;
}
return false;
}
private static boolean overrideDensityIfNeed(DisplayMetrics metrics , Resources resources) {
if (metrics == null) {
return false;
}
Configuration configuration = resources.getConfiguration();
if (metrics.density != getDefaultDensity() || configuration.fontScale != 1.0f) {
return true;
}
return false;
}
public static float getDefaultDensity() {
return getDefaultDensityDpi() * 1f / DisplayMetrics.DENSITY_MEDIUM;
}
public static int getDefaultDensityDpi() {
if (getSystemProperties("qemu.sf.lcd_density").isEmpty()) {
return Integer.parseInt(getSystemPropertiesFromPlatform("ro.sf.lcd_density", String.valueOf(DisplayMetrics.DENSITY_MEDIUM)));
} else {
return Integer.parseInt(getSystemPropertiesFromPlatform("qemu.sf.lcd_density", String.valueOf(DisplayMetrics.DENSITY_MEDIUM)));
}
}
/**
* Get the value for the given key and default key if the default value have no exist.
*
* @return an empty string if the key isn't found
* @throws IllegalArgumentException if the key exceeds 32 characters
*/
public static String getSystemPropertiesFromPlatform(String key, String mDefault) {
String result = mDefault;
try {
Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class);
result = (String) get.invoke(c, key);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
/**
* Get the value for the given key.
*
* @return an empty string if the key isn't found
* @throws IllegalArgumentException if the key exceeds 32 characters
*/
public static String getSystemProperties(String key) {
String result = "";
try {
Class<?> c = Class.forName("android.os.SystemProperties");
Method get = c.getMethod("get", String.class);
result = (String) get.invoke(c, key);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return result;
}
public static Resources getResources(Context context,Resources resources) {
DisplayMetrics appDisplayMetrics = resources.getDisplayMetrics();
if(overrideDensityIfNeed(appDisplayMetrics,resources)) {
Configuration configuration = resources.getConfiguration();
configuration.densityDpi = getDefaultDensityDpi();
configuration.fontScale = 1.0f;
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}
return resources;
}
}
在basicactivity重写getResource方法
@Override
public Resources getResources() {
Resources resources = super.getResources();
return DensityUtil.getResources(this, resources);
}