How to crop circular area from bitmap in Android
After long brainstorming I have found the solution
public Bitmap getCroppedBitmap(Bitmap bitmap) {
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());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
// canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2,
bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
//Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
//return _bmp;
return output;
}
Cropping image into circle
To accomplish what you're trying to do, you could subclass ImageView
and make it implement Picasso
's Target
interface. When the bitmap is loaded, just use a method that centercrops the bitmap into a square, and then shades the image into a circular shape. For example:
public class ImageViewTarget extends ImageView implements Target {
//constructors
@Override
public void onBitmapFailed(Drawable drawable) {
//TODO
}
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom loadFrom) {
bitmap = cropCircle(bitmap.isMutable() ? bitmap : bitmap.copy(Config.ARGB_8888, true));
setImageBitmap(bitmap);
}
@Override
public void onPrepareLoad(Drawable arg0) {
//TODO
}
public Bitmap cropCricle(Bitmap bm){
int width = bm.getWidth();
int height = bm.getHeight();
Bitmap cropped_bitmap;
/* Crop the bitmap so it'll display well as a circle. */
if (width > height) {
cropped_bitmap = Bitmap.createBitmap(bm,
(width / 2) - (height / 2), 0, height, height);
} else {
cropped_bitmap = Bitmap.createBitmap(bm, 0, (height / 2)
- (width / 2), width, width);
}
BitmapShader shader = new BitmapShader(cropped_bitmap, TileMode.CLAMP, TileMode.CLAMP);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setShader(shader);
height = cropped_bitmap.getHeight();
width = cropped_bitmap.getWidth();
Bitmap mCanvasBitmap = Bitmap.createBitmap(width, height,
Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(mCanvasBitmap);
canvas.drawCircle(width/2, height/2, width/2, paint);
return mCanvasBitmap;
}
}
There might be a better why to handle the cropCircle(Bitmap bitmap);
method, but the above works as sometime to optimize/build off of.
Android - Cut a circle from a square Bitmap
Seems like PorterDuff.Mode.Clear did not work for gingerbread
Solved the problem (cut circle from square using this code)
public Bitmap BitmapCircularCroper(Bitmap bitmapimg){
Bitmap output = Bitmap.createBitmap(bitmapimg.getWidth(),
bitmapimg.getHeight(), 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, bitmapimg.getWidth(),
bitmapimg.getHeight());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawCircle(bitmapimg.getWidth() / 2,
bitmapimg.getHeight() / 2, bitmapimg.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmapimg, rect, rect, paint);
return output;
}
Android Crop Center of Bitmap
This can be achieved with: Bitmap.createBitmap(source, x, y, width, height)
if (srcBmp.getWidth() >= srcBmp.getHeight()){
dstBmp = Bitmap.createBitmap(
srcBmp,
srcBmp.getWidth()/2 - srcBmp.getHeight()/2,
0,
srcBmp.getHeight(),
srcBmp.getHeight()
);
}else{
dstBmp = Bitmap.createBitmap(
srcBmp,
0,
srcBmp.getHeight()/2 - srcBmp.getWidth()/2,
srcBmp.getWidth(),
srcBmp.getWidth()
);
}
Crop square image to circle - Programmatically
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
DrawingView dv = new DrawingView(this);
setContentView(dv);
}
class DrawingView extends View {
Bitmap bitmap;
public DrawingView(Context context) {
super(context);
bitmap = BitmapFactory.decodeResource(context.getResources(),
R.drawable.glossy_overlay);
}
@Override
public void onDraw(Canvas canvas) {
Paint paint = new Paint();
// paint.setColor(Color.CYAN);
canvas.drawBitmap(getclip(), 30, 20, paint);
}
public Bitmap getclip() {
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());
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
// paint.setColor(color);
canvas.drawCircle(bitmap.getWidth() / 2,
bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return output;
}
}
}
Related Topics
Listen to Volume Buttons in Background Service
Android: Broadcast Receiver for Screen on and Screen Off
How to Pass Arraylist<Customeobject> from One Activity to Another
Perform a Task on Uninstall in Android
Android - How to Investigate an Anr
Generate Sha-1 for Flutter/React-Native/Android-Native App
Android Update Activity UI from Service
Android Writing Logs to Text File
How to Pass Data from 2Nd Activity to 1St Activity When Pressed Back? - Android
Android: How to Bind Spinner to Custom Object List
How to Get the Resource Id of an Image If I Know Its Name
Integrate Mupdf Reader in an App
How to Have Onscrolllistener for a Scrollview
How to Programmatically Add Views to Views