Example usage for android.graphics Paint Paint

List of usage examples for android.graphics Paint Paint

Introduction

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

Prototype

public Paint() 

Source Link

Document

Create a new paint with default settings.

Usage

From source file:Main.java

/**
 * TODO write documentation/*from  www  .j  a v a2 s.  co m*/
 *
 * @param sourceBitmap
 * @param color
 * @return
 */
public static Bitmap overlayColor(Bitmap sourceBitmap, int color) {
    Bitmap newBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(),
            sourceBitmap.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 getRoundedCornerBitmap(final Bitmap bitmap, final float roundPx) {

    if (bitmap != null) {
        try {//from w  ww .j  av  a  2s. co m
            final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
            Canvas canvas = new Canvas(output);

            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);
            canvas.drawARGB(0, 0, 0, 0);
            canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

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

            return output;

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

From source file:Main.java

public static final Bitmap alpha(Bitmap bitmap, int alpha) {
    float[] matrixItems = new float[] { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, alpha / 255f, 0,
            0, 0, 0, 0, 1 };//from w  ww  . j  ava2  s  .  com
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Bitmap alphaBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(alphaBitmap);
    Paint paint = new Paint();
    ColorMatrix colorMatrix = new ColorMatrix(matrixItems);
    ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
    paint.setColorFilter(colorMatrixFilter);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return alphaBitmap;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Bitmap output = Bitmap.createBitmap(w, h, Bitmap.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  ww  w.jav  a2 s  . co m*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float cornerRadius) {
    if (bitmap == null) {
        return null;
    }//ww  w.  java 2  s.  com
    Bitmap result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(result);

    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);
    canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG));
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, paint);

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

    return result;
}

From source file:Main.java

public static Bitmap getRoundedRectBitmap(Bitmap bitmap) {
    Bitmap result = null;/*from   www  .  ja  va2s.  c  o m*/
    Canvas canvas;
    Paint paint;
    try {
        final int width = bitmap.getWidth();
        final int height = bitmap.getHeight();
        result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        canvas = new Canvas(result);
        int color = 0xff424242;
        float radius = width > height ? width / 2 : height / 2;
        paint = new Paint();
        Rect rect = new Rect(0, 0, width, height);
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawCircle(width / 2, height / 2, radius, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
    } catch (NullPointerException | OutOfMemoryError e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static Bitmap getRoundImage(Bitmap oriImg, boolean recycleOld) {

    Bitmap targetBitmap = null;/* w  ww.  jav a2s  . c o  m*/

    if (oriImg != null && !oriImg.isRecycled()) {
        int size = Math.min(oriImg.getWidth(), oriImg.getHeight());
        int targetWidth = size;
        int targetHeight = size;

        targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(targetBitmap);

        Paint paint = new Paint();
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);
        canvas.drawARGB(0, 0, 0, 0);
        canvas.drawCircle(targetWidth / 2f, targetHeight / 2f, targetWidth / 2f, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(oriImg, new Rect(0, 0, size, size), new Rect(0, 0, targetWidth, targetHeight), paint);

        if (recycleOld) {
            oriImg.recycle();
        }
    }
    return targetBitmap;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Context context, Bitmap bitmap, int color) {

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    color = 0xff424242; // FIXME
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);
    final float roundPx = convertDipsToPixels(context, ROUND_DIPS);

    paint.setAntiAlias(true);/*from  w  w  w .j av a  2 s  . co  m*/
    canvas.drawARGB(0, 0, 0, 0);

    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

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

    return output;
}

From source file:Main.java

public static Bitmap getCircleBitmap(Bitmap bitmap, boolean recycleable) {
    final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);

    final int color = Color.RED;
    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. j a  va 2  s  . co  m
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    if (recycleable) {
        bitmap.recycle();
    }
    return output;
}

From source file:Main.java

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
    int i = bitmap.getWidth();
    int j = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.preScale(1.0F, -1F);/*from   ww  w  .  j  a va 2  s  .c om*/
    Bitmap bitmap1 = Bitmap.createBitmap(bitmap, 0, j / 2, i, j / 2, matrix, false);
    Bitmap bitmap2 = Bitmap.createBitmap(i, j + j / 2, android.graphics.Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap2);
    canvas.drawBitmap(bitmap, 0.0F, 0.0F, null);
    Paint paint = new Paint();
    canvas.drawRect(0.0F, j, i, j + 4, paint);
    canvas.drawBitmap(bitmap1, 0.0F, j + 4, null);
    Paint paint1 = new Paint();
    paint1.setShader(new LinearGradient(0.0F, bitmap.getHeight(), 0.0F, 4 + bitmap2.getHeight(), 0x70ffffff,
            0xffffff, android.graphics.Shader.TileMode.CLAMP));
    paint1.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));
    canvas.drawRect(0.0F, j, i, 4 + bitmap2.getHeight(), paint1);
    return bitmap2;
}