Example usage for android.util DisplayMetrics DisplayMetrics

List of usage examples for android.util DisplayMetrics DisplayMetrics

Introduction

In this page you can find the example usage for android.util DisplayMetrics DisplayMetrics.

Prototype

public DisplayMetrics() 

Source Link

Usage

From source file:Main.java

public static Bitmap getSampleBitmap(Activity activity, String filepath) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from   w  ww.  jav a2  s. c  o m*/
    BitmapFactory.decodeFile(filepath, options);
    options.inJustDecodeBounds = false;
    int width = options.outWidth;
    int height = options.outHeight;

    DisplayMetrics dm = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    int scale = Math.min(width / dm.widthPixels, height / dm.heightPixels);
    options.inSampleSize = scale;
    Bitmap bitmap2 = BitmapFactory.decodeFile(filepath, options);
    return bitmap2;
}

From source file:Main.java

public static boolean shouldScaleDownBitmap(Context context, Bitmap bitmap) {
    if (context != null && bitmap != null && bitmap.getWidth() > 0 && bitmap.getHeight() > 0) {
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();
        DisplayMetrics metrics = new DisplayMetrics();
        display.getMetrics(metrics);//  ww w  . ja va 2s  .c  om
        int width = metrics.widthPixels;
        int height = metrics.heightPixels;
        return ((width != 0 && width / bitmap.getWidth() < 1)
                || (height != 0 && height / bitmap.getHeight() < 1));
    }
    return false;
}

From source file:Main.java

public static void shrinkToLeft(FragmentActivity activity, View view, AnimationListener listener) {

    DisplayMetrics displaymetrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int screenHeight = displaymetrics.heightPixels;
    int screenWidth = displaymetrics.widthPixels;

    AnimationSet animation = new AnimationSet(true);
    float pivotX = view.getWidth() / 2;
    float pivotY = view.getHeight() / 2;
    ScaleAnimation anim = new ScaleAnimation(1f, 0.8f, 1f, 0.8f, pivotX, pivotY);
    anim.setInterpolator(new LinearInterpolator());
    anim.setDuration(200);/*ww  w .ja  va  2 s .  c  o  m*/
    anim.setStartOffset(200);
    //anim.setFillAfter(true);
    animation.addAnimation(anim);

    TranslateAnimation animTrans = new TranslateAnimation(0.0f, (float) -(screenWidth - view.getWidth()), 0.0f,
            0.0f);
    anim.setInterpolator(new LinearInterpolator());
    animTrans.setDuration(400);
    //animTrans.setStartOffset(300);
    animation.addAnimation(animTrans);

    if (listener != null)
        animation.setAnimationListener(listener);

    view.startAnimation(animation);
}

From source file:Main.java

public static void enlargeToRight(FragmentActivity activity, View view, AnimationListener listener) {

    DisplayMetrics displaymetrics = new DisplayMetrics();
    activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
    int screenHeight = displaymetrics.heightPixels;
    int screenWidth = displaymetrics.widthPixels;

    AnimationSet animation = new AnimationSet(true);
    float pivotX = view.getWidth() / 2;
    float pivotY = view.getHeight() / 2;
    ScaleAnimation anim = new ScaleAnimation(0.8f, 1f, 0.8f, 1f, pivotX, pivotY);
    anim.setInterpolator(new LinearInterpolator());
    anim.setDuration(200);//  w  w  w. ja  v a  2 s .  co  m
    anim.setStartOffset(200);
    //anim.setFillAfter(true);
    animation.addAnimation(anim);

    TranslateAnimation animTrans = new TranslateAnimation(0.0f, (float) (screenWidth - view.getWidth()), 0.0f,
            0.0f);
    anim.setInterpolator(new LinearInterpolator());
    animTrans.setDuration(400);
    //animTrans.setStartOffset(300);
    animation.addAnimation(animTrans);

    if (listener != null)
        animation.setAnimationListener(listener);

    view.startAnimation(animation);
}

From source file:Main.java

/**
 * Method getDensity() used to get the Device's Density. With this function,
 * we can use for checking the device's density and supporting for other
 * devices.//w w  w .j  a  v a 2 s  . c  o m
 * 
 * @param context
 *            The Application Context.
 * @return Integer Type.
 */
public static int getDensity(Context context) {
    final WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    final DisplayMetrics metrics = new DisplayMetrics();
    wm.getDefaultDisplay().getMetrics(metrics);
    return metrics.densityDpi;
}

From source file:Main.java

public static DisplayMetrics getScreenSize(Context context) {
    if (displayMetrics == null) {
        try {//from  w  ww  . ja v a  2 s . c o m

            displayMetrics = new DisplayMetrics();
            displayMetrics.widthPixels = 1280;
            displayMetrics.heightPixels = 720;
            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            wm.getDefaultDisplay().getMetrics(displayMetrics);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage());
        }

    }
    return displayMetrics;
}

From source file:Main.java

/**
 * Returns DisplayMetrics.//from  w  ww  .  j  av a  2  s.c o m
 * 
 * @param context
 * @return
 */
public static DisplayMetrics getDisplayMetrics(Context context) {
    WindowManager window = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    DisplayMetrics outMetrics = new DisplayMetrics();
    window.getDefaultDisplay().getMetrics(outMetrics);

    return outMetrics;
}

From source file:Main.java

public static BitmapDrawable writeOnDrawable(Activity actv, Resources res, int drawableId, String text,
        int textSize) {

    Bitmap bm = BitmapFactory.decodeResource(res, drawableId).copy(Bitmap.Config.ARGB_8888, true);

    DisplayMetrics dm = new DisplayMetrics();
    actv.getWindowManager().getDefaultDisplay().getMetrics(dm);

    int pixelSize = (int) ((textSize * dm.scaledDensity));

    if (text.length() > 2) {
        pixelSize = (int) ((textSize * dm.scaledDensity) * (0.5 - (text.length() / 10)));
    }/*from ww  w  . j  a  v  a 2 s  .c o m*/

    Paint paint = new Paint();
    paint.setStyle(Style.FILL);
    paint.setColor(Color.WHITE);
    paint.setTextSize(pixelSize);
    paint.setTextAlign(Paint.Align.CENTER);

    // float adjust = paint.measureText(text);

    Canvas canvas = new Canvas(bm);
    int xPos = (int) ((bm.getWidth() / 2));
    int yPos = (int) ((bm.getHeight() / 2) - ((paint.descent() + paint.ascent()) / 2));

    canvas.drawText(text, xPos, yPos, paint);

    return new BitmapDrawable(res, bm);
}

From source file:Main.java

public static int getFullScreenSizeWidth(Context context) {
    int realWidth;
    int realHeight;

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();

    Point point = new Point();

    if (Build.VERSION.SDK_INT >= 17) {
        //new pleasant way to get real metrics
        DisplayMetrics realMetrics = new DisplayMetrics();
        display.getRealMetrics(realMetrics);
        realWidth = realMetrics.widthPixels;
        realHeight = realMetrics.heightPixels;

    } else if (Build.VERSION.SDK_INT >= 14) {
        //reflection for this weird in-between time
        try {/*  w  w w . j a v  a2s .c  o m*/
            Method mGetRawH = Display.class.getMethod("getRawHeight");
            Method mGetRawW = Display.class.getMethod("getRawWidth");
            realWidth = (Integer) mGetRawW.invoke(display);
            realHeight = (Integer) mGetRawH.invoke(display);
        } catch (Exception e) {
            //this may not be 100% accurate, but it's all we've got
            realWidth = display.getWidth();
            realHeight = display.getHeight();
            Log.e("Display Info", "Couldn't use reflection to get the real display metrics.");
        }

    } else {
        //This should be close, as lower API devices should not have window navigation bars
        realWidth = display.getWidth();
        realHeight = display.getHeight();
    }
    return realWidth;
}

From source file:Main.java

public static int getFullScreenSizeHeight(Context context) {
    int realWidth;
    int realHeight;

    WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    Display display = wm.getDefaultDisplay();

    Point point = new Point();

    if (Build.VERSION.SDK_INT >= 17) {
        //new pleasant way to get real metrics
        DisplayMetrics realMetrics = new DisplayMetrics();
        display.getRealMetrics(realMetrics);
        realWidth = realMetrics.widthPixels;
        realHeight = realMetrics.heightPixels;

    } else if (Build.VERSION.SDK_INT >= 14) {
        //reflection for this weird in-between time
        try {/*from   w ww  . ja  v  a 2  s  . c  o m*/
            Method mGetRawH = Display.class.getMethod("getRawHeight");
            Method mGetRawW = Display.class.getMethod("getRawWidth");
            realWidth = (Integer) mGetRawW.invoke(display);
            realHeight = (Integer) mGetRawH.invoke(display);
        } catch (Exception e) {
            //this may not be 100% accurate, but it's all we've got
            realWidth = display.getWidth();
            realHeight = display.getHeight();
            Log.e("Display Info", "Couldn't use reflection to get the real display metrics.");
        }

    } else {
        //This should be close, as lower API devices should not have window navigation bars
        realWidth = display.getWidth();
        realHeight = display.getHeight();
    }
    return realHeight;
}