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

public static Bitmap getReflectionImageWithOrigin(Bitmap bitmap) {
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);/*from ww  w .  j  a  v a  2s  . c om*/

    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);

    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

From source file:Main.java

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);/*from  www . j  a va2 s  .  c o m*/

    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);

    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, Shader.TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

From source file:Main.java

private static Bitmap drawable2Bitmap(Drawable drawable, int... defaultWH) {
    if (drawable == null)
        return null;
    if (drawable instanceof BitmapDrawable)
        return ((BitmapDrawable) drawable).getBitmap();
    try {/*from w  w  w.  j a v  a2 s .  co  m*/
        Bitmap bitmap;
        if (drawable instanceof ColorDrawable)
            bitmap = Bitmap.createBitmap(defaultWH[0], defaultWH[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;
    } catch (OutOfMemoryError e) {
        return null;
    }
}

From source file:Main.java

public static Bitmap getPlusBitmap(Context context, String s) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);/*from ww  w.ja va 2 s  .  c  o  m*/
    Bitmap bitmap = BitmapFactory.decodeStream(getCommunityPicInputStream(s));
    Bitmap bitmap1;
    if (bitmap != null) {
        Bitmap bitmap2 = BitmapFactory.decodeResource(context.getResources(), 0x7f02003f);
        bitmap1 = Bitmap.createBitmap(bitmap.getWidth() + bitmap2.getWidth() / 2,
                bitmap.getHeight() + bitmap2.getHeight() / 2, android.graphics.Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap1);
        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawBitmap(bitmap, 0.0F, 0.0F, paint);
        bitmap.recycle();
        canvas.drawBitmap(bitmap2, bitmap.getWidth() - bitmap2.getWidth() / 2,
                bitmap.getHeight() - bitmap2.getHeight() / 2, paint);
        bitmap2.recycle();
    } else {
        bitmap1 = null;
    }
    return bitmap1;
}

From source file:Main.java

public static Bitmap getBitmap(Bitmap background, Bitmap contour, float alpha) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);//from  w  w  w  .  j a  v a  2s  .c o m
    paint.setColor(Color.WHITE);
    paint.setDither(false);
    Bitmap bitmap = Bitmap.createBitmap(contour.getWidth(), contour.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Matrix m = new Matrix();
    m.setScale(contour.getWidth() * 1.0f / background.getWidth(),
            contour.getHeight() * 1.0f / background.getHeight());
    paint.setAlpha((int) (alpha * 0xff));
    canvas.drawBitmap(background, m, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    paint.setAlpha(0xff);
    canvas.drawBitmap(contour, 0, 0, paint);
    return bitmap;
}

From source file:Main.java

public static Bitmap getBitmapFromView(View view) {
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

    view.layout(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
    view.draw(canvas);/*from www  . j  a  v a2  s  .  c om*/

    return bitmap;
}

From source file:Main.java

/**
 * Method to scale {@code sourceBitmap}, maintaining the same original size of the bitmap,
 * but with a transparent frame and the scaled and centered {@code sourceBitmap} inside.
 *
 * @return/*from w  ww .  j  a v a2s.com*/
 */
public static Bitmap scaleInsideWithFrame(Bitmap mutableBitmap, float factor, int color) {
    Bitmap clearBitmap = mutableBitmap.copy(Bitmap.Config.ARGB_8888, true);
    clearBitmap.eraseColor(color);

    Bitmap resizedInsideBitmap = scaleBitmapByFactor(mutableBitmap, factor);

    int frameWidth = clearBitmap.getWidth();
    int frameHeight = clearBitmap.getHeight();
    int imageWidth = resizedInsideBitmap.getWidth();
    int imageHeight = resizedInsideBitmap.getHeight();

    Canvas canvas = new Canvas(clearBitmap);
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    paint.setAntiAlias(true);
    canvas.drawBitmap(resizedInsideBitmap, (frameWidth - imageWidth) / 2, (frameHeight - imageHeight) / 2,
            paint);
    return clearBitmap;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int maxHeight, int pixels) {
    if (bitmap == null)
        return bitmap;
    maxHeight = (int) (getApplicationContext().getResources().getDisplayMetrics().density * maxHeight);
    float scale = bitmap.getHeight() * 1.0f / maxHeight;
    int newWidth = (int) (bitmap.getWidth() / scale);

    Bitmap output = Bitmap.createBitmap(newWidth, maxHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xffffffff;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final Rect dstRect = new Rect(0, 0, newWidth, maxHeight);
    final RectF rectF = new RectF(dstRect);

    paint.setAntiAlias(true);//from  w  ww . j  a v a2s  .  com
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, (float) pixels, (float) pixels, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, dstRect, paint);

    return output;
}

From source file:Main.java

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);//from w  w w  .j  av a2 s . c  om

    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);

    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

From source file:Main.java

public static Bitmap toBitmap(Drawable drawable) {
    int w = drawable.getMinimumWidth();
    int h = drawable.getMinimumHeight();
    if (w <= 0 || h <= 0) {
        return null;
    }//from  ww  w.  j a va 2s . c  o m
    Rect bounds = drawable.getBounds();
    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, w, h);
    drawable.draw(canvas);
    drawable.setBounds(bounds);
    return bitmap;
}