Example usage for android.graphics Canvas setDensity

List of usage examples for android.graphics Canvas setDensity

Introduction

In this page you can find the example usage for android.graphics Canvas setDensity.

Prototype

public void setDensity(int density) 

Source Link

Document

Specifies the density for this Canvas' backing bitmap.

Usage

From source file:Main.java

/**
 * Copy the red channel to a new Bitmap's alpha channel.  The old
 * one will be recycled.//from   w  w w  . j ava2 s  .  c  om
 */
static Bitmap redToAlpha(Bitmap src, boolean recycle) {
    Bitmap dest = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);

    float[] matrix = new float[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0 };

    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(new ColorMatrix(matrix)));

    Canvas canvas = new Canvas(dest);
    canvas.setDensity(Bitmap.DENSITY_NONE);
    canvas.drawBitmap(src, 0, 0, paint);

    if (recycle)
        src.recycle();
    return dest;
}

From source file:com.nadmm.airports.utils.UiUtils.java

public static Drawable combineDrawables(Context context, Drawable d1, Drawable d2, int paddingDp) {
    // Assumes both d1 & d2 are same size and square shaped
    int w = d1.getIntrinsicWidth();
    int h = d1.getIntrinsicHeight();
    int paddingPx = convertDpToPx(context, paddingDp);
    Bitmap result = Bitmap.createBitmap(w + (d2 != null ? w + paddingPx : 0), h, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(result);
    canvas.setDensity(Bitmap.DENSITY_NONE);
    d1.setBounds(0, 0, w - 1, h - 1);/*from w w  w.  java 2  s.c om*/
    d1.draw(canvas);
    if (d2 != null) {
        canvas.translate(w + paddingPx, 0);
        d2.setBounds(0, 0, w - 1, h - 1);
        d2.draw(canvas);
    }

    return new BitmapDrawable(context.getResources(), result);
}

From source file:com.nadmm.airports.utils.UiUtils.java

public static Drawable getRotatedDrawable(Context context, int resid, float rotation) {
    String key = String.format(Locale.US, "%d:%d", resid, (int) rotation);
    Drawable d = getDrawableFromCache(key);
    if (d == null) {
        Resources res = context.getResources();
        Bitmap bmp = BitmapFactory.decodeResource(res, resid);
        Bitmap rotated = Bitmap.createBitmap(bmp.getWidth(), bmp.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(rotated);
        canvas.setDensity(Bitmap.DENSITY_NONE);
        canvas.rotate(rotation, bmp.getWidth() / 2, bmp.getHeight() / 2);
        canvas.drawBitmap(bmp, 0, 0, sPaint);
        d = new BitmapDrawable(res, rotated);
        putDrawableIntoCache(key, d);/*from w  ww  .  j  av  a 2 s.c  o m*/
    }
    return d;
}