Android Bitmap Corner Round getRoundedCornerBitmap(Bitmap bitmap, int maxHeight, int pixels)

Here you can find the source of getRoundedCornerBitmap(Bitmap bitmap, int maxHeight, int pixels)

Description

get Rounded Corner Bitmap

License

Open Source License

Declaration

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,
            int maxHeight, int pixels) 

Method Source Code

//package com.java2s;
//  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of

import android.content.Context;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;

public class Main {
    private static Context sApplicationContext;

    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,
            int maxHeight, int pixels) {
        if (bitmap == null)
            return bitmap;
        maxHeight = (int) (getApplicationContext().getResources()
                .getDisplayMetrics().density * maxHeight);
        float scale = bitmap.getHeight() * 1.0f / maxHeight;
        int newWidth = (int) (bitmap.getWidth() / scale);

        Bitmap output = Bitmap.createBitmap(newWidth, maxHeight,
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xffffffff;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(),
                bitmap.getHeight());// w  w w .  ja v  a2 s. c o  m
        final Rect dstRect = new Rect(0, 0, newWidth, maxHeight);
        final RectF rectF = new RectF(dstRect);

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, (float) pixels, (float) pixels, paint);

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

        return output;
    }

    public static Context getApplicationContext() {
        return sApplicationContext;
    }
}

Related

  1. getRoundedCornerBitmap(Bitmap bitmap, int roundPx)
  2. getRoundBitmapFromUrl(String url, int pixels)
  3. toRoundCorner(Bitmap bitmap, int pixels)
  4. toRoundCorner( BitmapDrawable bitmapDrawable, int pixels)
  5. getRoundedCornerBitmap(Bitmap bitmap, float roundPx)
  6. getRoundedRectBitmap(Bitmap bitmap, int pixels)
  7. toRoundBitmap(Bitmap bitmap)
  8. toRoundBitmap(Bitmap bitmap)
  9. getCircleBitmap(Uri uri, Context context)