Rounded ImageView after scaling with ScaleType.CenterCrop

I try to crop the image and then rotate it in the corners so that it looks more beautiful on the screen.

What I managed to do is round the corners of the image, but cropping will sometimes crop the sides of the image (depending on the size / proportion of the image).

So, what I would like to do is complete the crop, then apply rounded corners. How can i do this?

Rounding the corners of the image:

private Bitmap getRoundedCornerBitmap(Bitmap bitmap) { Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff000000; final Paint paint = new Paint(); final Rect rect = new Rect(0,0,bitmap.getWidth(), bitmap.getHeight()); final RectF rectF = new RectF(rect); final float roundpx = 20; paint.setAntiAlias(true); canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); canvas.drawRoundRect(rectF, roundpx, roundpx, paint); paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); canvas.drawBitmap(bitmap, rect, rect, paint); return output; } 

And then I set the image scaling type like this: imageView.setScaleType(ScaleType.CENTER_CROP)

+6
source share
2 answers

if you wrap your image with a map, you will reach.

 <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto" android:layout_width="wrap_content" android:layout_height="wrap_content" card_view:cardCornerRadius="4dp" card_view:cardElevation="4dp" card_view:cardUseCompatPadding="true"> <ImageView android:id="@+id/ivImage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/your_image" /> </android.support.v7.widget.CardView> 
+4
source

try the following:

 public class RoundedImageView extends ImageView { private Path mMaskPath; private Paint mMaskPaint = new Paint(Paint.ANTI_ALIAS_FLAG); private int mCornerRadius = 10; public RoundedImageView(Context context) { super(context); init(); } public RoundedImageView(Context context, AttributeSet attributeSet) { super(context, attributeSet); init(); } public RoundedImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); init(); } private void init() { ViewCompat.setLayerType(this, ViewCompat.LAYER_TYPE_SOFTWARE, null); mMaskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR)); } public void setCornerRadius(int cornerRadius) { mCornerRadius = cornerRadius; generateMaskPath(getWidth(), getHeight()); invalidate(); } @Override protected void onSizeChanged(int w, int h, int oldW, int oldH) { super.onSizeChanged(w, h, oldW, oldH); if (w != oldW || h != oldH) { generateMaskPath(w, h); } } private void generateMaskPath(int w, int h) { mMaskPath = new Path(); mMaskPath.addRoundRect(new RectF(0,0,w,h), mCornerRadius, mCornerRadius, Path.Direction.CW); mMaskPath.setFillType(Path.FillType.INVERSE_WINDING); } @Override protected void onDraw(Canvas canvas) { if(canvas.isOpaque()) { // If canvas is opaque, make it transparent canvas.saveLayerAlpha(0, 0, canvas.getWidth(), canvas.getHeight(), 255, Canvas.HAS_ALPHA_LAYER_SAVE_FLAG); } super.onDraw(canvas); if(mMaskPath != null) { canvas.drawPath(mMaskPath, mMaskPaint); } } 

and

 <your.pack.name.RoundedImageView android:id="@+id/image_id" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="centerCrop" /> RoundedImageView iconImage = (RoundedImageView )findViewById(R.id.image_id); iconImage.setImageBitmap(bitmap); 
+1
source

Source: https://habr.com/ru/post/975901/


All Articles