studio rounded round redondear img corners android imageview android-imageview crop rounded-corners

android - rounded - ImageView con esquinas redondeadas después de escalar con ScaleType.CenterCrop



rounded imageview android (2)

Intento recortar una imagen y luego rodear las esquinas para que se vea mejor en la pantalla.

Lo que he podido hacer es redondear las esquinas de la imagen, pero el recorte algunas veces cortará los lados de la imagen (dependiendo de la relación tamaño / aspecto de la imagen).

Entonces, lo que me gustaría hacer es realizar el recorte, LUEGO aplicar las esquinas redondeadas. ¿Cómo puedo hacer esto?

Redondeando las esquinas de la imagen:

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

Y luego configuré el tipo de escala de la imagen de la siguiente manera: imageView.setScaleType(ScaleType.CENTER_CROP)


prueba este:

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

y

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


si ajusta su imagen con cardview, lo logrará.

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