Example usage for android.graphics Canvas Canvas

List of usage examples for android.graphics Canvas Canvas

Introduction

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

Prototype

@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
public Canvas(long nativeCanvas) 

Source Link

Usage

From source file:Main.java

/**Method to get round shaped bitmap. Mostly used for profile pictures
 *
 *     @param scaleBitmapImage/*from  w  ww.  ja va 2  s  . co  m*/
 *                      The source bitmap which has to converted to round shape
 *     @param context
 *                      The context
 *     @param targetWidthPixels
 *                      The width required for the target or returned  bitmap
 *     @param targetHeightPixels
 *                      The height required for the target or returned  bitmap
 *     @return
 *            round shaped bitmap
 */
public static Bitmap getRoundedShape(Bitmap scaleBitmapImage, Context context, int targetWidthPixels,
        int targetHeightPixels) {

    Bitmap targetBitmap = Bitmap.createBitmap(targetWidthPixels, targetHeightPixels, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(((float) targetWidthPixels - 1) / 2, ((float) targetHeightPixels - 1) / 2,
            (Math.min(((float) targetWidthPixels), ((float) targetHeightPixels)) / 2), Path.Direction.CCW);

    canvas.clipPath(path);
    Bitmap sourceBitmap = scaleBitmapImage;
    canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()),
            new Rect(0, 0, targetWidthPixels, targetHeightPixels), null);
    return targetBitmap;
}

From source file:Main.java

public static Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) {

    Matrix m = new Matrix();
    m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
    float targetX, targetY;
    if (orientationDegree == 90) {
        targetX = bm.getHeight();// w ww .  j a  va 2  s.  com
        targetY = 0;
    } else {
        targetX = bm.getHeight();
        targetY = bm.getWidth();
    }

    final float[] values = new float[9];
    m.getValues(values);

    float x1 = values[Matrix.MTRANS_X];
    float y1 = values[Matrix.MTRANS_Y];

    m.postTranslate(targetX - x1, targetY - y1);

    Bitmap bm1 = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(), Bitmap.Config.ARGB_8888);
    Paint paint = new Paint();
    Canvas canvas = new Canvas(bm1);
    canvas.drawBitmap(bm, m, paint);

    return bm1;
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable, int size) {
    int width = size;
    int height = size;
    int ratio = size / drawable.getIntrinsicHeight();
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, width, height);
    canvas.scale(ratio, ratio);/*from w w w.  j  ava 2 s .  c o m*/
    drawable.draw(canvas);
    return bitmap;
}

From source file:Main.java

public static Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) {

    Matrix m = new Matrix();
    m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
    float targetX, targetY;
    if (orientationDegree == 90) {
        targetX = bm.getHeight();// w ww .ja  va  2 s .  co  m
        targetY = 0;
    } else {
        targetX = bm.getHeight();
        targetY = bm.getWidth();
    }

    final float[] values = new float[9];
    m.getValues(values);

    float x1 = values[Matrix.MTRANS_X];
    float y1 = values[Matrix.MTRANS_Y];

    m.postTranslate(targetX - x1, targetY - y1);

    Bitmap temp = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(), Bitmap.Config.ARGB_8888);

    Paint paint = new Paint();
    Canvas canvas = new Canvas(temp);
    canvas.drawBitmap(bm, m, paint);

    return temp;
}

From source file:Main.java

public static Bitmap forceConfig565(Bitmap original) {
    Bitmap convertedBitmap = original;/*from w ww . j  a v a 2 s.c  o m*/
    if (original.getConfig() != Bitmap.Config.RGB_565) {
        convertedBitmap = Bitmap.createBitmap(original.getWidth(), original.getHeight(), Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(convertedBitmap);
        Paint paint = new Paint();
        paint.setColor(Color.BLACK);
        canvas.drawBitmap(original, 0, 0, paint);

        if (convertedBitmap != original) {
            original.recycle();
        }
    }

    return convertedBitmap;
}

From source file:Main.java

/** All dimensions are in pixels */
private static Bitmap createColorSwatchBitmap(int widthPixels, int heightPixels, int borderWidthPixels,
        int fillColor, int borderColor) {
    final Bitmap bitmap = Bitmap.createBitmap(widthPixels, heightPixels, Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmap);
    if (borderWidthPixels > 0) {
        canvas.drawColor(borderColor);//from  w ww .ja  va2 s .c  om
        canvas.clipRect(borderWidthPixels, borderWidthPixels, widthPixels - borderWidthPixels,
                heightPixels - borderWidthPixels);
    }
    canvas.drawColor(fillColor);
    return bitmap;
}

From source file:Main.java

public static Bitmap vectorToBitmap(Context context, Drawable vector) {
    final Bitmap bitmap = Bitmap.createBitmap(vector.getIntrinsicWidth(), vector.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);/*from w  w w .  j  a  v  a  2s .c o m*/
    final Canvas canvas = new Canvas(bitmap);
    vector.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    vector.draw(canvas);
    return bitmap;
}

From source file:Main.java

public static Bitmap createReflectImages(Bitmap bImap) {
    if (null == bImap) {
        return null;
    }//w  w  w . ja  v a  2s  . co m

    final int reflectionGap = 4;
    int width = bImap.getWidth();
    int height = bImap.getHeight();

    Bitmap bitmapWithReflection = null;
    try {
        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);
        Bitmap reflectionImage = Bitmap.createBitmap(bImap, 0, height / 2, width, height / 2, matrix, false);
        bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmapWithReflection);
        canvas.drawBitmap(bImap, 0, 0, null);
        Paint deafaultPaint = new Paint();
        canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
        canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(0, bImap.getHeight(), 0,
                bitmapWithReflection.getHeight() + reflectionGap, 0x80ffffff, 0x00ffffff, TileMode.CLAMP);
        paint.setShader(shader);
        paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
        canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);
    } catch (OutOfMemoryError e) {
    }

    return bitmapWithReflection;
}

From source file:Main.java

public static Bitmap getUnreadMarker(int width, int radius, int color) {
    if (width <= 0) {
        return null;
    }/*w  w  w.  j a  va  2s .co  m*/
    Bitmap dest = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(dest);
    Paint p = new Paint();
    p.setColor(color);
    p.setAntiAlias(true);
    canvas.drawCircle(width / 2, width / 2, radius, p);
    return dest;
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable == null)
        drawable = new ColorDrawable(Color.TRANSPARENT);

    Bitmap bitmap;/*from  w  w w  .java 2s. c o m*/

    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        if (bitmapDrawable.getBitmap() != null)
            return bitmapDrawable.getBitmap();
    }

    if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0)
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
    else
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}