How to adapt to the corner of ImageView only on the side to adapt to the parent layout in Android?

I'm a beginner of Android. Now, I'm learning how to correctly design android app. But I have problems with ImageView. I want to bypass all corners of ImageView, but only one side

This image is what I want:

As you can see, only the left corner of the ImageView is rounded to fit the parent view

That's what I get now:

This is the XML layout of my list view rows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/list_row_bg"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <ImageView
            android:background="@color/colorPrimary"
            android:id="@+id/row_image"
            android:scaleType="fitCenter"
            android:layout_width="100dp"
            android:layout_height="100dp" />
    </LinearLayout>

    <LinearLayout
        android:padding="@dimen/list_item_content_padding"
        android:orientation="vertical"
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/row_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/row_duration"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/row_file_size"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/row_description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>
</LinearLayout>

I followed this tutorial. But ImageView contains padding. However, it doesn't work when I delete padding

resolvent:

I think you should apply the transformation when loading an image. For example, if you load an image using Picasso, you can apply this transformation:

Base class:

public class RoundedCornersBitmap implements Transformation {

private static final float DEFAULT_RADIUS = 5.f;
private static final int DEFAULT_BORDER_COLOR = Color.WHITE;
private static final int DEFAULT_stroke_WIDTH = 3;

protected float mCornerRadius;
protected int mBorderColor;
protected int mstrokeWidth;

@Override
public String key() {
    return "roundedCorners()";
}

public RoundedCornersBitmap() {
    mCornerRadius = DEFAULT_RADIUS;
    mBorderColor = DEFAULT_BORDER_COLOR;
    mstrokeWidth = DEFAULT_stroke_WIDTH;
}

public RoundedCornersBitmap(float cornderRadius, int borderColor, int strokeWidth) {
    mCornerRadius = cornderRadius;
    mBorderColor = borderColor;
    mstrokeWidth = strokeWidth;
}

@Override
public Bitmap transform(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final Paint paint = new Paint();
    final Rect rect = new Rect(mstrokeWidth, mstrokeWidth, (bitmap.getWidth() - mstrokeWidth), bitmap.getHeight()
            - mstrokeWidth);
    final RectF rectF = new RectF(0, 0, bitmap.getWidth(), bitmap.getHeight());

    paint.setAntiAlias(true);
    paint.setColor(mBorderColor);
    paint.setstrokeWidth(3);

    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawRoundRect(rectF, mCornerRadius, mCornerRadius, 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.recycle();
    return output;
}

/**
 * @return the mCornerRadius
 */
public float getCornerRadius() {
    return mCornerRadius;
}

/**
 * @param mCornerRadius
 *            the mCornerRadius to set
 */
public void setCornerRadius(float mCornerRadius) {
    this.mCornerRadius = mCornerRadius;
}

/**
 * @return the mBorderColor
 */
public int getBorderColor() {
    return mBorderColor;
}

/**
 * @param mBorderColor
 *            the mBorderColor to set
 */
public void setBorderColor(int mBorderColor) {
    this.mBorderColor = mBorderColor;
}

/**
 * @return the mstrokeWidth
 */
public int getstrokeWidth() {
    return mstrokeWidth;
}

/**
 * @param mstrokeWidth
 *            the mstrokeWidth to set
 */
public void setstrokeWidth(int mstrokeWidth) {
    this.mstrokeWidth = mstrokeWidth;
}

}

LeftRoundedCornersBitmap:

public class LeftRoundedCornersBitmap extends RoundedCornersBitmap {

    public LeftRoundedCornersBitmap() {
        super();
    }

    public LeftRoundedCornersBitmap(float cornderRadius, int borderColor, int strokeWidth) {
        super(cornderRadius, borderColor, strokeWidth);
    }

    @Override
    public Bitmap transform(Bitmap bitmap) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

       final Paint paint = new Paint();
       final Rect rect = new Rect(mstrokeWidth, mstrokeWidth, (bitmap.getWidth() - mstrokeWidth), bitmap.getHeight()
            - mstrokeWidth);
       final RectF rectF = new RectF(rect);
       final Rect topRightRect = new Rect(bitmap.getWidth() / 2, 0, bitmap.getWidth(), bitmap.getHeight() / 2);
       final Rect bottomRect = new Rect(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth(), bitmap.getHeight());

       paint.setAntiAlias(true);
       paint.setColor(mBorderColor);
       paint.setstrokeWidth(3);

       canvas.drawARGB(0, 0, 0, 0);
       canvas.drawRoundRect(rectF, mCornerRadius, mCornerRadius, paint);
       canvas.drawRect(topRightRect, paint);
       canvas.drawRect(bottomRect, paint);

       paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
       canvas.drawBitmap(bitmap, rect, rect, paint);
       bitmap.recycle();
       return output;
   }
}

Picasso dependency in gradle file:

compile 'com.squareup.picasso:picasso:2.5.2'

Example:

Picasso.with(context).load(imageUrl).transform(new LeftRoundedCornersBitmap()).into(youImageView);

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>