I'm using a code to make ImageView circular. The problem is that I need to use " scaleType: centerCrop ", which leaves the oval image instead of circular , depending on the resolution of the image.
I tried using the code below to resize the image, but it ended up losing a lot of quality. I've been thinking instead of resizing, show only what ImageView can show. That is, if ImageView has 150px for 150px , it will only display 150px / 150px in the image, remaining. But I do not know how I can do this.
Resize code:
public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight){
int Width = bm.getWidth();
int Height = bm.getHeight();
float scaleWidth = ((float) newWidth / Width);
float scaleHeight = ((float) newHeight / Height);
//Create a matrix for manipulation
Matrix matrix = new Matrix();
//Resize the bitmap
matrix.postScale(scaleWidth, scaleHeight);
//Recreate bitmap
Bitmap resizedBitmap = Bitmap.createBitmap
( bm, 0, 0, Width, Height, matrix, false );
bm.recycle();
return resizedBitmap;
}
Here is the class I use to make ImageView circular:
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
public class RoundImage extends Drawable {
private final Bitmap mBitmap;
private final Paint mPaint;
private final RectF mRectF;
private final int mBitmapWidth;
private final int mBitmapHeight;
public RoundImage(Bitmap bitmap) {
mBitmap = bitmap;
mRectF = new RectF();
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setDither(true);
final BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
mPaint.setShader(shader);
mBitmapWidth = mBitmap.getWidth();
mBitmapHeight = mBitmap.getHeight();
}
@Override
public void draw(Canvas canvas) {
canvas.drawOval(mRectF, mPaint);
}
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);
mRectF.set(bounds);
}
@Override
public void setAlpha(int alpha) {
if (mPaint.getAlpha() != alpha) {
mPaint.setAlpha(alpha);
invalidateSelf();
}
}
@Override
public void setColorFilter(ColorFilter cf) {
mPaint.setColorFilter(cf);
}
@Override
public int getOpacity() {
return PixelFormat.TRANSLUCENT;
}
@Override
public int getIntrinsicWidth() {
return mBitmapWidth;
}
@Override
public int getIntrinsicHeight() {
return mBitmapHeight;
}
public void setAntiAlias(boolean aa) {
mPaint.setAntiAlias(aa);
invalidateSelf();
}
@Override
public void setFilterBitmap(boolean filter) {
mPaint.setFilterBitmap(filter);
invalidateSelf();
}
@Override
public void setDither(boolean dither) {
mPaint.setDither(dither);
invalidateSelf();
}
public Bitmap getBitmap() {
return mBitmap;
}
}