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 scrambleImage(Bitmap image, int piecesPerLine) {
    int px = (int) Math.ceil(image.getWidth() / (double) piecesPerLine);
    int ph = (int) Math.ceil(image.getHeight() / (double) piecesPerLine);
    int count = piecesPerLine * piecesPerLine;
    int[] arr = shuffle(arrayRange(count));

    Config config = image.getConfig() == null ? Bitmap.Config.ARGB_8888 : image.getConfig();
    Bitmap destBmp = Bitmap.createBitmap(image.getWidth(), image.getHeight(), config);
    Canvas canvas = new Canvas(destBmp);

    for (int i = 0; i < piecesPerLine; i++) {
        for (int j = 0; j < piecesPerLine; j++) {
            int n = arr[i * piecesPerLine + j];
            int row = n / piecesPerLine;
            int col = n % piecesPerLine;

            Rect fromRect = new Rect(row * px, col * ph, row * px + px, col * ph + ph);
            Rect toRect = new Rect(j * px, i * ph, j * px + px, i * ph + ph);

            canvas.drawBitmap(image, fromRect, toRect, null);
        }/*  w  ww  . j  a v a 2 s. com*/
    }

    return destBmp;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx, int w, int h) {
    Bitmap output = Bitmap.createBitmap(w, h, Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, w, h);
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);/*from  w w  w.  j  av  a 2 s .com*/
    canvas.drawARGB(0, 0xFF, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()), rect, paint);

    return output;
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }//from  w  w  w .  j a v  a2 s  .co m

    int width = drawable.getIntrinsicWidth();
    width = width > 0 ? width : 1;
    int height = drawable.getIntrinsicHeight();
    height = height > 0 ? height : 1;

    Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

From source file:Main.java

public static Bitmap convertDrawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }//  w  ww .  j  a  v a2 s  .  c om

    int width = drawable.getIntrinsicWidth();
    width = width > 0 ? width : 1;
    int height = drawable.getIntrinsicHeight();
    height = height > 0 ? height : 1;

    Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    return bitmap;
}

From source file:Main.java

/**
 * Get a circular (rounded) bitmap shape with the diameter is the smaller between target width and target height.
 * @param bitmap//from   w  w w  .j a  v  a 2 s. co  m
 * @param width target width
 * @param height target height
 * @return Rounded (circular) bitmap width diameter is the smaller between target width and target height.
 */
public static Bitmap getRoundedBitmap(Bitmap bitmap, int width, int height) {
    int diameter = width < height ? width : height;
    Bitmap targetBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(width / 2f, height / 2f, diameter / 2f, Path.Direction.CCW);
    canvas.clipPath(path);

    Bitmap sourceBitmap = bitmap;
    Rect destinationRect = new Rect(0, 0, width, height);
    Rect sourceRect = new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight());
    canvas.drawBitmap(sourceBitmap, sourceRect, destinationRect, null);
    return targetBitmap;
}

From source file:Main.java

public static Bitmap converttoBitmap565(Bitmap b) {
    if (b == null) {
        return null;
    }// w ww .j  a  v  a  2 s. c  o  m
    if (b.getConfig() == Bitmap.Config.RGB_565) {
        return b;
    }
    Bitmap b565 = Bitmap.createBitmap(b.getWidth(), b.getHeight(), Bitmap.Config.RGB_565);
    Canvas c = new Canvas(b565);
    c.drawBitmap(b, 0, 0, null);
    return b565;
}

From source file:Main.java

/**
 * ATTENTION: DON'T USE THIS METHOD BECAUSE IT HAS BAD PERFORMANCES.
 *
 * @param source The original Bitmap./*w w  w.  ja  v  a 2  s .  com*/
 * @param color  Color to overlay.
 * @return the result image.
 */
@Deprecated
private static Bitmap overlayColor(Bitmap source, int color) {
    Bitmap newBitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight());
    Bitmap mutableBitmap = newBitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(mutableBitmap);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    ColorFilter filter = new LightingColorFilter(color, 1);
    paint.setColorFilter(filter);
    canvas.drawBitmap(mutableBitmap, 0, 0, paint);
    return mutableBitmap;
}

From source file:Main.java

public static Bitmap combineDrawables(Resources resources, int head, int body, int legs) {
    Bitmap headBitmap = getBitmap(resources, head);
    Bitmap bodyBitmap = getBitmap(resources, body);
    Bitmap legsBitmap = getBitmap(resources, legs);

    int height = headBitmap.getHeight() + bodyBitmap.getHeight() + legsBitmap.getHeight();
    int width = Math.max(headBitmap.getWidth(), Math.max(bodyBitmap.getWidth(), legsBitmap.getWidth()));

    Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas comboImage = new Canvas(result);
    comboImage.drawBitmap(headBitmap, 0f, 0f, null);
    comboImage.drawBitmap(bodyBitmap, 0f, headBitmap.getHeight(), null);
    comboImage.drawBitmap(legsBitmap, 0f, headBitmap.getHeight() + bodyBitmap.getHeight(), null);

    return result;
}

From source file:Main.java

public static Bitmap getRemoveBitmap(Context context, Bitmap bitmap) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);//www.ja v a2  s . co m
    Bitmap bitmap1;
    try {
        Bitmap bitmap2 = BitmapFactory.decodeStream(context.getAssets().open("remove@2x.png"));
        bitmap1 = Bitmap.createBitmap(bitmap.getWidth() + bitmap2.getWidth() / 2,
                bitmap.getHeight() + bitmap2.getHeight() / 2, android.graphics.Bitmap.Config.ARGB_8888);
        Canvas canvas1 = new Canvas(bitmap1);
        canvas1.drawARGB(0, 0, 0, 0);
        canvas1.drawBitmap(bitmap, bitmap2.getWidth() / 2, bitmap2.getHeight() / 2, paint);
        bitmap.recycle();
        canvas1.drawBitmap(bitmap2, 0.0F, 0.0F, paint);
        bitmap2.recycle();
    } catch (IOException ioexception) {
        ioexception.printStackTrace();
        bitmap1 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                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();
    }
    return bitmap1;
}

From source file:Main.java

/**
 * Drawable convert to Bitmap/*from   w  w w  .  j ava 2 s . c  om*/
 */
public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable == null) {

        throw new NullPointerException("drawableToBitmap()-->drawable is null.");
    }

    int width = drawable.getIntrinsicWidth();
    int height = 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);
    drawable.draw(canvas);
    return bitmap;

}