Example usage for android.graphics Paint setShader

List of usage examples for android.graphics Paint setShader

Introduction

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

Prototype

public Shader setShader(Shader shader) 

Source Link

Document

Set or clear the shader object.

Usage

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);

    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 createReflectionImageWithOrigin(Bitmap bitmap) {
    int i = bitmap.getWidth();
    int j = bitmap.getHeight();
    Matrix matrix = new Matrix();
    matrix.preScale(1.0F, -1F);//  w  ww  .  ja v  a 2  s . c o  m
    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;
}

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);//from www .j  a  va2  s .  co 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 circle from cutout from a bitmap.
 * does not alter sizes.//from  w ww .java2  s  .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

protected static void setSweepGradientGradientPaint(Paint paint, float width, float height, int colorStart,
        int colorEnd) {
    paint.setShader(new SweepGradient(width, height, colorStart, colorEnd));
    paint.setAntiAlias(true);//from w w w . j a v  a 2s  .c  om
}

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 w  w .  j  av a  2s  . 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 createRoundedBitmap(Context context, Bitmap bitmap, int radiusDP) {
    Bitmap bmp;/*from   w  w w. j  ava 2  s . 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 createRoundedBitmap(Context context, Bitmap bitmap, int leftTop, int rightTop,
        int leftBottm, int rightBottom) {
    Bitmap bmp;// www  .  j  av a 2 s. co  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, leftTop,
            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 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);
    paint.setAntiAlias(true);/*from   www .  j  av  a 2  s.  c o m*/
    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 bitmap which is wrapped to circle
 * (similar to what you can see in G+ profile pic.)
 *
 * @param bitmap Original Bitmap//  ww  w  .  ja  v a2s  .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;
}