Java Code Examples for android.util.DisplayMetrics#DENSITY_DEFAULT

The following examples show how to use android.util.DisplayMetrics#DENSITY_DEFAULT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InvariantDeviceProfile.java    From Trebuchet with GNU General Public License v3.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: Utils.java    From GravityBox with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
Source File: U.java    From SecondScreen with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 4
Source File: RoundedBitmapDrawable.java    From adt-leanback-support with Apache License 2.0 5 votes vote down vote up
/**
 * 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();
    }
}
 
Example 5
Source File: ViewExpanderCollapser.java    From 1Rramp-Android with MIT License 4 votes vote down vote up
private static float getPixelScaleFactor(Context context) {
  DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
  return (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT);
}
 
Example 6
Source File: MessageAdapter.java    From ChatView with MIT License 4 votes vote down vote up
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;
}
 
Example 7
Source File: OAuthManagerDialogFragment.java    From react-native-oauth with MIT License 4 votes vote down vote up
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;
}
 
Example 8
Source File: Utils.java    From 4pdaClient-plus with Apache License 2.0 4 votes vote down vote up
public static int dpToPix(int dp, DisplayMetrics metrics) {
    return (dp * metrics.densityDpi) / DisplayMetrics.DENSITY_DEFAULT;
}
 
Example 9
Source File: Utils.java    From ExpansionPanel with Apache License 2.0 4 votes vote down vote up
public static float pxToDp(Context context, float px) {
    return px / ((float) context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}
 
Example 10
Source File: IconCache.java    From geopackage-android with MIT License 4 votes vote down vote up
/**
 * 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;
}
 
Example 11
Source File: UiUtils.java    From react-native-navigation with MIT License 4 votes vote down vote up
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));
}
 
Example 12
Source File: Utilities.java    From LaunchEnr with GNU General Public License v3.0 4 votes vote down vote up
static float dpiFromPx(int size, DisplayMetrics metrics){
    float densityRatio = (float) metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT;
    return (size / densityRatio);
}
 
Example 13
Source File: SwipeRevealLayout.java    From SwipeRevealLayout with MIT License 4 votes vote down vote up
private int pxToDp(int px) {
    Resources resources = getContext().getResources();
    DisplayMetrics metrics = resources.getDisplayMetrics();
    return (int) (px / ((float)metrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT));
}
 
Example 14
Source File: MainActivity.java    From WatermarkCreator with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 15
Source File: PublicFunctions.java    From MagicalExoPlayer with MIT License 4 votes vote down vote up
public static int convertDpToPixel(Context context, float dp) {
    return (int) (dp * (context.getResources().getDisplayMetrics().densityDpi / DisplayMetrics.DENSITY_DEFAULT));
}
 
Example 16
Source File: SimilarLoadingView.java    From similarLoadingView with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 17
Source File: Utils.java    From prayer-times-android with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}
 
Example 18
Source File: Utilities.java    From call_manage with MIT License 2 votes vote down vote up
/**
 * 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);
}
 
Example 19
Source File: ObservationShapeStyle.java    From mage-android with Apache License 2.0 2 votes vote down vote up
/**
 * Set the stroke width
 *
 * @param strokeWidth stroke width
 */
public void setStrokeWidth(float strokeWidth) {
    this.strokeWidth = strokeWidth * (displayMetrics.densityDpi / DisplayMetrics.DENSITY_DEFAULT);
}
 
Example 20
Source File: DimensionUtils.java    From FABsMenu with Apache License 2.0 2 votes vote down vote up
/**
 * 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);
}