Example usage for android.content.res Resources getDisplayMetrics

List of usage examples for android.content.res Resources getDisplayMetrics

Introduction

In this page you can find the example usage for android.content.res Resources getDisplayMetrics.

Prototype

public DisplayMetrics getDisplayMetrics() 

Source Link

Document

Return the current display metrics that are in effect for this resource object.

Usage

From source file:com.android.gallery3d.data.UriImage.java

public Bitmap drawTextToBitmap(Context gContext, String gText, Bitmap bitmap) {
    Resources resources = gContext.getResources();
    float scale = resources.getDisplayMetrics().density;

    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if (bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }// w w w  .  ja  va  2  s .co  m
    // resource bitmaps are imutable, 
    // so we need to convert it to mutable one
    bitmap = bitmap.copy(bitmapConfig, true);

    Canvas canvas = new Canvas(bitmap);
    // new antialised Paint
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // text color - #3D3D3D
    paint.setColor(Color.rgb(61, 61, 61));
    // text size in pixels
    paint.setTextSize((int) (25 * scale));
    // text shadow
    paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

    // draw text to the Canvas center
    Rect bounds = new Rect();
    paint.setTextAlign(Align.CENTER);

    paint.getTextBounds(gText, 0, gText.length(), bounds);
    int x = (bitmap.getWidth() - bounds.width()) / 2;
    int y = (bitmap.getHeight() + bounds.height()) / 2;

    canvas.drawText(gText, x * scale, y * scale, paint);

    return bitmap;
}

From source file:cf.obsessiveorange.rhcareerfairlayout.ui.fragments.VPParentFragment.java

/**
 * Private helper method to animate toolbar and related views to a position.
 * @param toY New Y position.//from   ww  w .j  a v a  2  s  .c o  m
 * @param completionHandlers any handlers to be fired after completion of the animation
 */
private void animateToolbar(final float toY, final Runnable... completionHandlers) {
    float layoutTranslationY = mInterceptionLayout.getTranslationY();
    if (layoutTranslationY != toY) {
        // get translation
        final float translationY = -layoutTranslationY + toY;

        final View toolbarView = getActivity().findViewById(R.id.toolbar);
        final SearchBox searchView = ((MainActivity) getActivity()).getSearch();

        // Need to offset for the -6dp margin on search bar
        Resources r = getResources();
        float offset = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 6, r.getDisplayMetrics());

        // Start animating all the items
        mInterceptionLayout.animate().y(toY).setDuration(200);
        toolbarView.animate().y(toY).setDuration(200);
        searchView.animate().y(toY - offset).setDuration(200);

        // Relayout child view at animation completion
        final FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mInterceptionLayout.getLayoutParams();
        new Handler().postDelayed(new Runnable() {
            public void run() {
                lp.height = (int) (Math.max(0, -translationY) + getScreenHeight());
                mInterceptionLayout.requestLayout();
            }
        }, translationY < 0 ? 0 : 200);

        // Start calling all handlers, giving time for re-layout to occur.
        new Handler().postDelayed(new Runnable() {
            public void run() {
                for (Runnable completionHandler : completionHandlers) {
                    completionHandler.run();
                }
            }
        }, 250);

    }
}

From source file:com.amaze.filemanager.ui.icons.IconHolder.java

public IconHolder(Context context, boolean useThumbs, boolean grid) {
    super();//from  www  .j av a2  s  .c om
    this.mContext = context;
    this.mUseThumbs = useThumbs;
    this.mRequests = new HashMap<ImageView, String>();
    this.mIcons = new HashMap<String, Bitmap>();
    this.mAppIcons = new LinkedHashMap<String, Bitmap>(MAX_CACHE, .75F, true) {
        private static final long serialVersionUID = 1L;

        @Override
        protected boolean removeEldestEntry(Entry<String, Bitmap> eldest) {
            return size() > MAX_CACHE;
        }
    };
    this.mAlbums = new HashMap<String, Long>();
    this.grid = grid;
    Resources res = mContext.getResources();
    int dp = 50;
    if (grid) {
        dp = 150;
    }
    px = (int) (dp * (res.getDisplayMetrics().densityDpi / 160));

}

From source file:chrisrenke.drawerarrowdrawable.DrawerArrowDrawable.java

public DrawerArrowDrawable(Resources resources, boolean rounded) {
    this.rounded = rounded;
    float density = resources.getDisplayMetrics().density;
    float strokeWidthPixel = STROKE_WIDTH_DP * density;
    halfStrokeWidthPixel = strokeWidthPixel / 2;

    linePaint = new Paint(SUBPIXEL_TEXT_FLAG | ANTI_ALIAS_FLAG);
    linePaint.setStrokeCap(rounded ? ROUND : BUTT);
    linePaint.setColor(BLACK);//from www .  jav  a  2 s  .c  om
    linePaint.setStyle(STROKE);
    linePaint.setStrokeWidth(strokeWidthPixel);

    int dimen = (int) (DIMEN_DP * density);
    bounds = new Rect(0, 0, dimen, dimen);

    Path first, second;
    JoinedPath joinedA, joinedB;

    // Top
    first = new Path();
    first.moveTo(5.042f, 20f);
    first.rCubicTo(8.125f, -16.317f, 39.753f, -27.851f, 55.49f, -2.765f);
    second = new Path();
    second.moveTo(60.531f, 17.235f);
    second.rCubicTo(11.301f, 18.015f, -3.699f, 46.083f, -23.725f, 43.456f);
    scalePath(first, density);
    scalePath(second, density);
    joinedA = new JoinedPath(first, second);

    first = new Path();
    first.moveTo(64.959f, 20f);
    first.rCubicTo(4.457f, 16.75f, 1.512f, 37.982f, -22.557f, 42.699f);
    second = new Path();
    second.moveTo(42.402f, 62.699f);
    second.cubicTo(18.333f, 67.418f, 8.807f, 45.646f, 8.807f, 32.823f);
    scalePath(first, density);
    scalePath(second, density);
    joinedB = new JoinedPath(first, second);
    topLine = new BridgingLine(joinedA, joinedB);

    // Middle
    first = new Path();
    first.moveTo(5.042f, 35f);
    first.cubicTo(5.042f, 20.333f, 18.625f, 6.791f, 35f, 6.791f);
    second = new Path();
    second.moveTo(35f, 6.791f);
    second.rCubicTo(16.083f, 0f, 26.853f, 16.702f, 26.853f, 28.209f);
    scalePath(first, density);
    scalePath(second, density);
    joinedA = new JoinedPath(first, second);

    first = new Path();
    first.moveTo(64.959f, 35f);
    first.rCubicTo(0f, 10.926f, -8.709f, 26.416f, -29.958f, 26.416f);
    second = new Path();
    second.moveTo(35f, 61.416f);
    second.rCubicTo(-7.5f, 0f, -23.946f, -8.211f, -23.946f, -26.416f);
    scalePath(first, density);
    scalePath(second, density);
    joinedB = new JoinedPath(first, second);
    middleLine = new BridgingLine(joinedA, joinedB);

    // Bottom
    first = new Path();
    first.moveTo(5.042f, 50f);
    first.cubicTo(2.5f, 43.312f, 0.013f, 26.546f, 9.475f, 17.346f);
    second = new Path();
    second.moveTo(9.475f, 17.346f);
    second.rCubicTo(9.462f, -9.2f, 24.188f, -10.353f, 27.326f, -8.245f);
    scalePath(first, density);
    scalePath(second, density);
    joinedA = new JoinedPath(first, second);

    first = new Path();
    first.moveTo(64.959f, 50f);
    first.rCubicTo(-7.021f, 10.08f, -20.584f, 19.699f, -37.361f, 12.74f);
    second = new Path();
    second.moveTo(27.598f, 62.699f);
    second.rCubicTo(-15.723f, -6.521f, -18.8f, -23.543f, -18.8f, -25.642f);
    scalePath(first, density);
    scalePath(second, density);
    joinedB = new JoinedPath(first, second);
    bottomLine = new BridgingLine(joinedA, joinedB);
}

From source file:com.xueyu.view.DrawerArrowDrawable.java

@SuppressLint("ResourceAsColor")
public DrawerArrowDrawable(Resources resources, boolean rounded) {
    this.rounded = rounded;
    float density = resources.getDisplayMetrics().density;
    float strokeWidthPixel = STROKE_WIDTH_DP * density;
    halfStrokeWidthPixel = strokeWidthPixel / 2;

    linePaint = new Paint(SUBPIXEL_TEXT_FLAG | ANTI_ALIAS_FLAG);
    linePaint.setStrokeCap(rounded ? ROUND : BUTT);
    linePaint.setColor(R.color.white);/*ww w .  j a  v  a 2 s . c  om*/
    linePaint.setStyle(STROKE);
    linePaint.setStrokeWidth(strokeWidthPixel);

    int dimen = (int) (DIMEN_DP * density);
    bounds = new Rect(0, 0, dimen, dimen);

    Path first, second;
    JoinedPath joinedA, joinedB;

    // Top
    first = new Path();
    first.moveTo(5.042f, 20f);
    first.rCubicTo(8.125f, -16.317f, 39.753f, -27.851f, 55.49f, -2.765f);
    second = new Path();
    second.moveTo(60.531f, 17.235f);
    second.rCubicTo(11.301f, 18.015f, -3.699f, 46.083f, -23.725f, 43.456f);
    scalePath(first, density);
    scalePath(second, density);
    joinedA = new JoinedPath(first, second);

    first = new Path();
    first.moveTo(64.959f, 20f);
    first.rCubicTo(4.457f, 16.75f, 1.512f, 37.982f, -22.557f, 42.699f);
    second = new Path();
    second.moveTo(42.402f, 62.699f);
    second.cubicTo(18.333f, 67.418f, 8.807f, 45.646f, 8.807f, 32.823f);
    scalePath(first, density);
    scalePath(second, density);
    joinedB = new JoinedPath(first, second);
    topLine = new BridgingLine(joinedA, joinedB);

    // Middle
    first = new Path();
    first.moveTo(5.042f, 35f);
    first.cubicTo(5.042f, 20.333f, 18.625f, 6.791f, 35f, 6.791f);
    second = new Path();
    second.moveTo(35f, 6.791f);
    second.rCubicTo(16.083f, 0f, 26.853f, 16.702f, 26.853f, 28.209f);
    scalePath(first, density);
    scalePath(second, density);
    joinedA = new JoinedPath(first, second);

    first = new Path();
    first.moveTo(64.959f, 35f);
    first.rCubicTo(0f, 10.926f, -8.709f, 26.416f, -29.958f, 26.416f);
    second = new Path();
    second.moveTo(35f, 61.416f);
    second.rCubicTo(-7.5f, 0f, -23.946f, -8.211f, -23.946f, -26.416f);
    scalePath(first, density);
    scalePath(second, density);
    joinedB = new JoinedPath(first, second);
    middleLine = new BridgingLine(joinedA, joinedB);

    // Bottom
    first = new Path();
    first.moveTo(5.042f, 50f);
    first.cubicTo(2.5f, 43.312f, 0.013f, 26.546f, 9.475f, 17.346f);
    second = new Path();
    second.moveTo(9.475f, 17.346f);
    second.rCubicTo(9.462f, -9.2f, 24.188f, -10.353f, 27.326f, -8.245f);
    scalePath(first, density);
    scalePath(second, density);
    joinedA = new JoinedPath(first, second);

    first = new Path();
    first.moveTo(64.959f, 50f);
    first.rCubicTo(-7.021f, 10.08f, -20.584f, 19.699f, -37.361f, 12.74f);
    second = new Path();
    second.moveTo(27.598f, 62.699f);
    second.rCubicTo(-15.723f, -6.521f, -18.8f, -23.543f, -18.8f, -25.642f);
    scalePath(first, density);
    scalePath(second, density);
    joinedB = new JoinedPath(first, second);
    bottomLine = new BridgingLine(joinedA, joinedB);
}

From source file:com.actionbarsherlock.internal.view.menu.MenuPopupHelper.java

public MenuPopupHelper(Context context, MenuBuilder menu, View anchorView, boolean overflowOnly) {
    mContext = context;//from w ww . j a va2 s .  com
    mInflater = LayoutInflater.from(context);
    mMenu = menu;
    mOverflowOnly = overflowOnly;

    final Resources res = context.getResources();
    mPopupMaxWidth = Math.max(res.getDisplayMetrics().widthPixels / 2,
            res.getDimensionPixelSize(R.dimen.abs__config_prefDialogWidth));

    mAnchorView = anchorView;

    menu.addMenuPresenter(this);
}

From source file:com.example.nouri.playground.drawable.ArrowDrawable.java

public ArrowDrawable(Resources resources, boolean rounded) {
    this.rounded = rounded;
    float density = resources.getDisplayMetrics().density;
    float strokeWidthPixel = STROKE_WIDTH_DP * density;
    halfStrokeWidthPixel = strokeWidthPixel / 2;

    linePaint = new Paint(SUBPIXEL_TEXT_FLAG | ANTI_ALIAS_FLAG);
    linePaint.setStrokeCap(rounded ? ROUND : BUTT);
    linePaint.setColor(BLACK);/*from ww  w .j a v a 2  s . com*/
    linePaint.setStyle(STROKE);
    linePaint.setStrokeWidth(strokeWidthPixel);

    int dimen = (int) (DIMEN_DP * density);
    bounds = new Rect(0, 0, dimen, dimen);

    Path first, second;
    JoinedPath joinedA, joinedB;

    // Top
    first = new Path();
    first.moveTo(5.042f, 20f);
    first.rCubicTo(8.125f, -16.317f, 39.753f, -27.851f, 55.49f, -2.765f);
    second = new Path();
    second.moveTo(60.531f, 17.235f);
    second.rCubicTo(11.301f, 18.015f, -3.699f, 46.083f, -23.725f, 43.456f);
    scalePath(first, density);
    scalePath(second, density);
    joinedA = new JoinedPath(first, second);

    first = new Path();
    first.moveTo(64.959f, 20f);
    first.rCubicTo(4.457f, 16.75f, 1.512f, 37.982f, -22.557f, 42.699f);
    second = new Path();
    second.moveTo(42.402f, 62.699f);
    second.cubicTo(18.333f, 67.418f, 8.807f, 45.646f, 8.807f, 32.823f);
    scalePath(first, density);
    scalePath(second, density);
    joinedB = new JoinedPath(first, second);
    topLine = new BridgingLine(joinedA, joinedB);

    // Middle
    first = new Path();
    first.moveTo(5.042f, 35f);
    first.cubicTo(5.042f, 20.333f, 18.625f, 6.791f, 35f, 6.791f);
    second = new Path();
    second.moveTo(35f, 6.791f);
    second.rCubicTo(16.083f, 0f, 26.853f, 16.702f, 26.853f, 28.209f);
    scalePath(first, density);
    scalePath(second, density);
    joinedA = new JoinedPath(first, second);

    first = new Path();
    first.moveTo(64.959f, 35f);
    first.rCubicTo(0f, 10.926f, -8.709f, 26.416f, -29.958f, 26.416f);
    second = new Path();
    second.moveTo(35f, 61.416f);
    second.rCubicTo(-7.5f, 0f, -23.946f, -8.211f, -23.946f, -26.416f);
    scalePath(first, density);
    scalePath(second, density);
    joinedB = new JoinedPath(first, second);
    middleLine = new BridgingLine(joinedA, joinedB);

    // Bottom
    first = new Path();
    first.moveTo(5.042f, 50f);
    first.cubicTo(2.5f, 43.312f, 0.013f, 26.546f, 9.475f, 17.346f);
    second = new Path();
    second.moveTo(9.475f, 17.346f);
    second.rCubicTo(9.462f, -9.2f, 24.188f, -10.353f, 27.326f, -8.245f);
    scalePath(first, density);
    scalePath(second, density);
    joinedA = new JoinedPath(first, second);

    first = new Path();
    first.moveTo(64.959f, 50f);
    first.rCubicTo(-7.021f, 10.08f, -20.584f, 19.699f, -37.361f, 12.74f);
    second = new Path();
    second.moveTo(27.598f, 62.699f);
    second.rCubicTo(-15.723f, -6.521f, -18.8f, -23.543f, -18.8f, -25.642f);
    scalePath(first, density);
    scalePath(second, density);
    joinedB = new JoinedPath(first, second);
    bottomLine = new BridgingLine(joinedA, joinedB);
}

From source file:com.silentcircle.contacts.list.ShortcutIntentBuilder.java

/**
 * Generates a phone number shortcut icon. Adds an overlay describing the type of the phone
 * number, and if there is a photo also adds the call action icon.
 *///w w  w.  ja  v  a2 s  .c o  m
private Bitmap generatePhoneNumberIcon(Drawable photo, int phoneType, String phoneLabel, int actionResId) {
    final Resources r = mContext.getResources();
    final float density = r.getDisplayMetrics().density;

    Bitmap phoneIcon = ((BitmapDrawable) r.getDrawableForDensity(actionResId, mIconDensity)).getBitmap();

    Bitmap icon = generateQuickContactIcon(photo);
    Canvas canvas = new Canvas(icon);

    // Copy in the photo
    Paint photoPaint = new Paint();
    photoPaint.setDither(true);
    photoPaint.setFilterBitmap(true);
    Rect dst = new Rect(0, 0, mIconSize, mIconSize);

    // Create an overlay for the phone number type
    CharSequence overlay = Phone.getTypeLabel(r, phoneType, phoneLabel);

    if (overlay != null) {
        TextPaint textPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
        textPaint.setTextSize(r.getDimension(R.dimen.shortcut_overlay_text_size));
        textPaint.setColor(ContextCompat.getColor(mContext, R.color.textColorIconOverlay));
        textPaint.setShadowLayer(4f, 0, 2f,
                ContextCompat.getColor(mContext, R.color.textColorIconOverlayShadow));

        final FontMetricsInt fmi = textPaint.getFontMetricsInt();

        // First fill in a darker background around the text to be drawn
        final Paint workPaint = new Paint();
        workPaint.setColor(mOverlayTextBackgroundColor);
        workPaint.setStyle(Paint.Style.FILL);
        final int textPadding = r.getDimensionPixelOffset(R.dimen.shortcut_overlay_text_background_padding);
        final int textBandHeight = (fmi.descent - fmi.ascent) + textPadding * 2;
        dst.set(0, mIconSize - textBandHeight, mIconSize, mIconSize);
        canvas.drawRect(dst, workPaint);

        overlay = TextUtils.ellipsize(overlay, textPaint, mIconSize, TruncateAt.END);
        final float textWidth = textPaint.measureText(overlay, 0, overlay.length());
        canvas.drawText(overlay, 0, overlay.length(), (mIconSize - textWidth) / 2,
                mIconSize - fmi.descent - textPadding, textPaint);
    }

    // Draw the phone action icon as an overlay
    Rect src = new Rect(0, 0, phoneIcon.getWidth(), phoneIcon.getHeight());
    int iconWidth = icon.getWidth();
    dst.set(iconWidth - ((int) (20 * density)), -1, iconWidth, ((int) (19 * density)));
    canvas.drawBitmap(phoneIcon, src, dst, photoPaint);

    canvas.setBitmap(null);

    return icon;
}

From source file:com.gome.haoyuangong.views.MyViewPageIndicator.java

private int dipToPixels(int dip) {
    Resources r = getResources();
    float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip, r.getDisplayMetrics());
    return (int) px;
}

From source file:com.amaze.carbonfilemanager.ui.icons.IconHolder.java

public IconHolder(Context context, boolean useThumbs, boolean grid) {
    super();/*from   w  w w  .  j  a v a  2s  .  c  om*/
    this.mContext = context;
    this.mUseThumbs = useThumbs;
    this.mRequests = new HashMap<>();
    this.mIcons = new HashMap<>();
    this.mAppIcons = new LinkedHashMap<String, Bitmap>(MAX_CACHE, .75F, true) {
        private static final long serialVersionUID = 1L;

        @Override
        protected boolean removeEldestEntry(Entry<String, Bitmap> eldest) {
            return size() > MAX_CACHE;
        }
    };
    Resources res = mContext.getResources();
    int dp = 50;
    if (grid) {
        dp = 150;
    }
    px = (int) (dp * (res.getDisplayMetrics().densityDpi / 160));

}