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 setSaturation(Bitmap srcBitmap, float sat) {
    Bitmap bitmap = Bitmap.createBitmap(srcBitmap);
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(sat);/*from  w  w  w .j  av  a  2s. com*/
    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(cm));
    Canvas canvas = new Canvas(bitmap);
    canvas.drawBitmap(srcBitmap, 0, 0, paint);
    return bitmap;
}

From source file:Main.java

public static Bitmap tintBitmap(Bitmap src, int color) {
    Paint paint = new Paint();
    paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
    Bitmap dst = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(dst);
    canvas.drawBitmap(src, 0, 0, paint);
    return dst;//  w w w  .j  a  v a  2s . c o  m
}

From source file:Main.java

public static Bitmap cropCenter(Bitmap bitmap, boolean recycle) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    if (width == height)
        return bitmap;
    int size = Math.min(width, height);

    Bitmap target = Bitmap.createBitmap(size, size, getConfig(bitmap));
    Canvas canvas = new Canvas(target);
    canvas.translate((size - width) / 2, (size - height) / 2);
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    if (recycle)//from   ww w.j av  a  2 s.  c  o m
        bitmap.recycle();
    return target;
}

From source file:Main.java

public static Bitmap toGrayscale(Bitmap bmpOriginal) {
    int width, height;
    height = bmpOriginal.getHeight();/*www. j a  v  a 2s.co  m*/
    width = bmpOriginal.getWidth();
    try {
        Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bmpGrayscale);
        Paint paint = new Paint();
        ColorMatrix cm = new ColorMatrix();
        cm.setSaturation(0);
        ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
        paint.setColorFilter(f);
        c.drawBitmap(bmpOriginal, 0, 0, paint);
        return bmpGrayscale;
    } catch (OutOfMemoryError e) {
        return bmpOriginal;
    }
}

From source file:Main.java

public static Bitmap getRounded(Bitmap bm, int cornerRadiusPx) {
    int w = bm.getWidth();
    int h = bm.getHeight();

    Bitmap bmOut = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bmOut);

    Paint paint = new Paint();
    paint.setAntiAlias(true);/*w ww .j a  v a2  s  .co  m*/
    paint.setColor(0xff424242);

    Rect rect = new Rect(0, 0, w, h);
    RectF rectF = new RectF(rect);

    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawRoundRect(rectF, cornerRadiusPx, cornerRadiusPx, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bm, rect, rect, paint);

    return bmOut;
}

From source file:Main.java

/**
 * This method updates the opacity of the bitmap.
 *
 * @param bitmap  The bitmap.// w w  w. ja va2 s . co  m
 * @param opacity The value of alpha.
 * @return The bitmap after adjusting the opacity.
 */
public static Bitmap adjustOpacity(Bitmap bitmap, int opacity) {

    Bitmap mutableBitmap = bitmap.isMutable() ? bitmap : bitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas canvas = new Canvas(mutableBitmap);
    int color = (opacity & 0xFF) << 24;
    canvas.drawColor(color, PorterDuff.Mode.DST_IN);
    return mutableBitmap;
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    drawable.draw(canvas);//  ww w.  jav  a  2s.  co  m
    return bitmap;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float radius) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

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

    paint.setAntiAlias(true);//from   w w w .  ja v a  2s. c om
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, radius, radius, paint);

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

    return output;
}

From source file:Main.java

public static Bitmap drawTextToBitmap(Context mContext, int resourceId, String mText) {
    try {/*from   w  w  w  .j  a va 2 s  . c o m*/
        Resources resources = mContext.getResources();
        float scale = resources.getDisplayMetrics().density;
        Bitmap bitmap = BitmapFactory.decodeResource(resources, resourceId);

        android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
        // set default bitmap config if none
        if (bitmapConfig == null) {
            bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
        }
        // 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(77, 77, 77));
        // text size in pixels
        paint.setTextSize((int) (13 * scale));
        // text shadow
        paint.setShadowLayer(1f, 0f, 1f, Color.DKGRAY);

        // draw text to the Canvas center
        Rect bounds = new Rect();
        paint.getTextBounds(mText, 0, mText.length(), bounds);
        int x = (int) ((bitmap.getWidth() - bounds.width()) / 4);
        int y = (int) ((bitmap.getHeight() + bounds.height()) / 4);

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

        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;

}

From source file:Main.java

/**
 * Scales the provided bitmap to the height and width provided (using antialiasing).
 * (Alternative method for scaling bitmaps since Bitmap.createScaledBitmap(...) produces low quality bitmaps.)
 *
 * @param bitmap    is the bitmap to scale.
 * @param newWidth  is the desired width of the scaled bitmap.
 * @param newHeight is the desired height of the scaled bitmap.
 * @return the scaled bitmap./*from   w  w  w. j a v  a  2  s  .  c o  m*/
 */
public static Bitmap scaleBitmap(Bitmap bitmap, int newWidth, int newHeight) {
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);

    float scaleX = newWidth / (float) bitmap.getWidth();
    float scaleY = newHeight / (float) bitmap.getHeight();
    float pivotX = 0;
    float pivotY = 0;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(scaleX, scaleY, pivotX, pivotY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, 0, 0, new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG));

    return scaledBitmap;
}