下面列出了android.util.DisplayMetrics#DENSITY_DEFAULT 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private int getLauncherIconDensity(int requiredSize) {
// Densities typically defined by an app.
int[] densityBuckets = new int[] {
DisplayMetrics.DENSITY_LOW,
DisplayMetrics.DENSITY_MEDIUM,
DisplayMetrics.DENSITY_TV,
DisplayMetrics.DENSITY_HIGH,
DisplayMetrics.DENSITY_XHIGH,
DisplayMetrics.DENSITY_XXHIGH,
DisplayMetrics.DENSITY_XXXHIGH
};
int density = DisplayMetrics.DENSITY_XXXHIGH;
for (int i = densityBuckets.length - 1; i >= 0; i--) {
float expectedSize = ICON_SIZE_DEFINED_IN_APP_DP * densityBuckets[i]
/ DisplayMetrics.DENSITY_DEFAULT;
if (expectedSize >= requiredSize) {
density = densityBuckets[i];
}
}
return density;
}
private static int getScreenType(Context con) {
if (mDeviceType == -1) {
WindowManager wm = (WindowManager) con.getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics outMetrics = new DisplayMetrics();
wm.getDefaultDisplay().getMetrics(outMetrics);
int shortSize = Math.min(outMetrics.heightPixels, outMetrics.widthPixels);
int shortSizeDp = shortSize * DisplayMetrics.DENSITY_DEFAULT / outMetrics.densityDpi;
if (shortSizeDp < 600) {
// 0-599dp: "phone" UI with a separate status & navigation bar
mDeviceType = DEVICE_PHONE;
} else if (shortSizeDp < 720) {
// 600-719dp: "phone" UI with modifications for larger screens
mDeviceType = DEVICE_HYBRID;
} else {
// 720dp: "tablet" UI with a single combined status & navigation bar
mDeviceType = DEVICE_TABLET;
}
}
return mDeviceType;
}
/**
* Set the density at which this drawable will be rendered.
*
* @param density The density scale for this drawable.
*
* @see android.graphics.Bitmap#setDensity(int)
* @see android.graphics.Bitmap#getDensity()
*/
public void setTargetDensity(int density) {
if (mTargetDensity != density) {
mTargetDensity = density == 0 ? DisplayMetrics.DENSITY_DEFAULT : density;
if (mBitmap != null) {
computeBitmapSize();
}
invalidateSelf();
}
}
public static boolean isBlacklisted(Context context, String requestedRes, String requestedDpi) {
boolean blacklisted = false;
SharedPreferences prefMain = getPrefMain(context);
int defaultHeight = prefMain.getInt("height", 0);
int defaultWidth = prefMain.getInt("width", 0);
int defaultDpi = getSystemProperty("ro.sf.lcd_density", prefMain.getInt("density", 0));
int height, width, density;
if("reset".equals(requestedRes)) {
height = defaultHeight;
width = defaultWidth;
} else {
Scanner scanner = new Scanner(requestedRes);
scanner.useDelimiter("x");
width = scanner.nextInt();
height = scanner.nextInt();
scanner.close();
}
if("reset".equals(requestedDpi))
density = defaultDpi;
else
density = Integer.parseInt(requestedDpi);
// Blacklist DPI values that result in a smallest width that's too low or too high
int smallestWidth = (DisplayMetrics.DENSITY_DEFAULT * Math.min(height, width)) / density;
if(smallestWidth < 320 || smallestWidth > 1280)
blacklisted = true;
// On Android 10, blacklist resolutions that are larger than the device's native resolution
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q && (height > defaultHeight || width > defaultWidth))
blacklisted = true;
return blacklisted;
}
public static float pxToDp(Context context, float px) {
return px / ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}
public static float convertDpToPixel(float dp, Context context) {
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
return px;
}
public static int convertDpToPixel(Context context, float dp) {
return (int) (dp * (context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT));
}
public static float convertDpToPixel(float dp, Context context) {
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
return px;
}
private int pxToDp(int px) {
Resources resources = getContext().getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
return (int) (px / ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT));
}
static float dpiFromPx(int size, DisplayMetrics metrics){
float densityRatio = (float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT;
return (size / densityRatio);
}
public static int dpToPx(Context context, int dp) {
if (dp <= 0) return dp;
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
return (int) (dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT));
}
/**
* Create or retrieve from cache an icon bitmap for the icon row
*
* @param icon icon row
* @param density display density: {@link android.util.DisplayMetrics#density}
* @param iconCache icon cache
* @return icon bitmap
*/
public static Bitmap createIcon(IconRow icon, float density, IconCache iconCache) {
Bitmap iconImage = null;
if (icon != null) {
if (iconCache != null) {
iconImage = iconCache.get(icon.getId());
}
if (iconImage == null) {
BitmapFactory.Options options = icon.getDataBounds();
int dataWidth = options.outWidth;
int dataHeight = options.outHeight;
double styleWidth = dataWidth;
double styleHeight = dataHeight;
double widthDensity = DisplayMetrics.DENSITY_DEFAULT;
double heightDensity = DisplayMetrics.DENSITY_DEFAULT;
if (icon.getWidth() != null) {
styleWidth = icon.getWidth();
double widthRatio = dataWidth / styleWidth;
widthDensity *= widthRatio;
if (icon.getHeight() == null) {
heightDensity = widthDensity;
}
}
if (icon.getHeight() != null) {
styleHeight = icon.getHeight();
double heightRatio = dataHeight / styleHeight;
heightDensity *= heightRatio;
if (icon.getWidth() == null) {
widthDensity = heightDensity;
}
}
options = new BitmapFactory.Options();
options.inDensity = (int) (Math.min(widthDensity, heightDensity) + 0.5f);
options.inTargetDensity = (int) (DisplayMetrics.DENSITY_DEFAULT * density + 0.5f);
iconImage = icon.getDataBitmap(options);
if (widthDensity != heightDensity) {
int width = (int) (styleWidth * density + 0.5f);
int height = (int) (styleHeight * density + 0.5f);
if (width != iconImage.getWidth() || height != iconImage.getHeight()) {
Bitmap scaledBitmap = Bitmap.createScaledBitmap(iconImage, width, height, false);
iconImage.recycle();
iconImage = scaledBitmap;
}
}
if (iconCache != null) {
iconCache.put(icon.getId(), iconImage);
}
}
}
return iconImage;
}
private static float getPixelScaleFactor(Context context) {
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
return (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT);
}
public static int dpToPix(int dp, DisplayMetrics metrics) {
return (dp * metrics.densityDpi) / DisplayMetrics.DENSITY_DEFAULT;
}
public static int convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
return (int)px;
}
public static float convertDpToPixel(float dp, Context context){
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
float px = dp * ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
return px;
}
/**
* This method converts dp unit to equivalent pixels, depending on device density.
*
* @param context Context to get resources and device specific display metrics
* @param dp A value in dp (density independent pixels) unit. Which we need to convert into pixels
* @return A float value to represent px equivalent to dp depending on device density
*/
public static float convertDpToPixel(Context context, float dp) {
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
return dp * ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}
/**
* This method converts device specific pixels to density independent pixels.
*
* @param context Context to get resources and device specific display metrics
* @param px A value in px (pixels) unit. Which we need to convert into db
* @return A float value to represent dp equivalent to px value
*/
public static float convertPixelsToDp(Context context, float px) {
return px / (dpi(context) / DisplayMetrics.DENSITY_DEFAULT);
}
/**
* Set the stroke width
*
* @param strokeWidth stroke width
*/
public void setStrokeWidth(float strokeWidth) {
this.strokeWidth = strokeWidth * (displayMetrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}
/**
* This method converts device specific pixels to density independent pixels.
*
* @param px
* A value in px (pixels) unit. Which we need to convert into db
* @param context
* Context to get resources and device specific display metrics
*
* @return A float value to represent dp equivalent to px value
*/
static float convertPixelsToDp(float px, Context context) {
Resources resources = context.getResources();
DisplayMetrics metrics = resources.getDisplayMetrics();
return px / ((float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}