Example usage for android.graphics BitmapShader BitmapShader

List of usage examples for android.graphics BitmapShader BitmapShader

Introduction

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

Prototype

private BitmapShader(Bitmap bitmap, int tileX, int tileY) 

Source Link

Usage

From source file:Main.java

public static Shader createShader(Bitmap paramBitmap) {
    return new BitmapShader(paramBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
}

From source file:Main.java

public static Bitmap getRoundedImage(Bitmap bitmap) {
    Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);

    BitmapShader shader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);
    Paint paint = new Paint();
    paint.setShader(shader);/*  w w w . j  a  va 2 s .c o  m*/

    Canvas c = new Canvas(circleBitmap);
    c.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);

    return circleBitmap;
}

From source file:Main.java

public static Bitmap createRoundedBitmap(Context context, Bitmap bitmap, int radiusDP) {
    Bitmap bmp;/*from  www.j a  v  a  2s  .  c o m*/

    bmp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    BitmapShader shader = new BitmapShader(bitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);

    float radius = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, radiusDP,
            context.getResources().getDisplayMetrics());
    Canvas canvas = new Canvas(bmp);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(shader);

    RectF rect = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());
    canvas.drawRoundRect(rect, radius, radius, paint);

    return bmp;
}

From source file:Main.java

public static Bitmap makeCircleBitmap(Bitmap original) {
    final int width = original.getWidth();
    final int height = original.getHeight();
    final float radius = Math.min(width, height) / 2;

    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Paint paint = new Paint();
    paint.setAntiAlias(true);//  w ww .java2s. c o m
    paint.setShader(new BitmapShader(original, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));

    Canvas canvas = new Canvas(bitmap);
    canvas.drawCircle(radius, radius, radius, paint);

    original.recycle();
    return bitmap;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap3(Drawable imageDrawable, int radius) {

    Bitmap d = ((BitmapDrawable) imageDrawable).getBitmap();
    BitmapShader shader = new BitmapShader(d, TileMode.CLAMP, TileMode.CLAMP);

    int size = Math.min(d.getWidth(), d.getHeight());
    Bitmap output = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    RectF outerRect = new RectF(0, 0, size, size);
    //      float cornerRadius = radius;

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setShader(shader);//from   w  w w.  ja v a  2  s. c o m
    paint.setAntiAlias(true);
    paint.setColor(Color.RED);
    canvas.drawCircle(outerRect.centerX(), outerRect.centerY(), d.getWidth() / 2, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    imageDrawable.setBounds(0, 0, size, size);

    Canvas canvas1 = new Canvas(output);

    RectF outerRect1 = new RectF(0, 0, size, size);

    Paint paint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint1.setShader(shader);
    paint1.setAntiAlias(true);
    paint1.setColor(Color.RED);
    canvas1.drawCircle(outerRect1.centerX(), outerRect1.centerY(), d.getWidth() / 2, paint);

    paint1.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    return output;
}

From source file:Main.java

/**
 * create a circle from cutout from a bitmap.
 * does not alter sizes.//from  w w w  . jav a 2s. c  om
 *
 * @param bitmap the bitmap
 * @see #cutCircleFromBitmap(String, int)
 * @return a bitmap circle cutout
 */
public static Bitmap roundBitMap(Bitmap bitmap) {
    Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);

    BitmapShader shader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    paint.setShader(shader);

    Canvas c = new Canvas(circleBitmap);

    c.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);

    return circleBitmap;
}

From source file:Main.java

public static Bitmap getCircularBitmapImage(Bitmap source) {
    int size = Math.min(source.getWidth(), source.getHeight());
    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;
    Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
    if (squaredBitmap != source) {
        source.recycle();/*from   w w w .j  av  a 2  s  .  c  o m*/
    }
    Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP,
            BitmapShader.TileMode.CLAMP);
    paint.setShader(shader);
    paint.setAntiAlias(true);
    float r = size / 2f;
    canvas.drawCircle(r, r, r, paint);
    squaredBitmap.recycle();
    return bitmap;
}

From source file:Main.java

static private Bitmap smallCoverPostProc(Bitmap smallBitmap) {
    try {//  w ww. j  av a  2  s . c  o m
        Bitmap smallBitmapPostProc = Bitmap.createBitmap(smallBitmap.getWidth(), smallBitmap.getHeight(),
                Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas();
        canvas.setBitmap(smallBitmapPostProc);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        Bitmap bitmapToShade = Bitmap.createBitmap(smallBitmap, 2, 2, smallBitmap.getWidth() - 4,
                smallBitmap.getHeight() - 4);
        BitmapShader bmShader = new BitmapShader(bitmapToShade, TileMode.CLAMP, TileMode.CLAMP);
        paint.setShader(bmShader);
        canvas.drawRoundRect(new RectF(2, 2, smallBitmap.getWidth() - 2, smallBitmap.getHeight() - 2), 4, 4,
                paint);
        return smallBitmapPostProc;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

public static Bitmap createCircularClip(Bitmap input, int width, int height) {
    if (input == null)
        return null;

    final int inWidth = input.getWidth();
    final int inHeight = input.getHeight();
    final Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);
    final Paint paint = new Paint();
    paint.setShader(new BitmapShader(input, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
    paint.setAntiAlias(true);/* w w w  .ja va2  s .  c o  m*/
    final RectF srcRect = new RectF(0, 0, inWidth, inHeight);
    final RectF dstRect = new RectF(0, 0, width, height);
    final Matrix m = new Matrix();
    m.setRectToRect(srcRect, dstRect, Matrix.ScaleToFit.CENTER);
    canvas.setMatrix(m);
    canvas.drawCircle(inWidth / 2, inHeight / 2, inWidth / 2, paint);
    return output;
}

From source file:Main.java

/**
 * Create a bitmap which is wrapped to circle
 * (similar to what you can see in G+ profile pic.)
 *
 * @param bitmap Original Bitmap//w ww .  j ava  2  s  .c o m
 * @return Circled bitmap
 */
public static Bitmap createCircleBitmap(Bitmap bitmap) {
    final int width = bitmap.getWidth();
    final int height = bitmap.getHeight();

    BitmapShader bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
    Paint bitmapPaint = new Paint();
    bitmapPaint.setAntiAlias(true);
    bitmapPaint.setShader(bitmapShader);

    Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    canvas.drawCircle(width / 2, height / 2, Math.min(width, height) / 2, bitmapPaint);

    return output;
}