其他分享
首页 > 其他分享> > 如何只在侧面适应ImageView的角落以适应Android中的父级布局?

如何只在侧面适应ImageView的角落以适应Android中的父级布局?

作者:互联网

我是Android的初学者.现在,我正在学习如何正确设计Android App.但是我对ImageView有问题.我想绕过ImageView的各个角落,但仅限于一侧.

该图像是我想要得到的:

enter image description here

如您所见,只有ImageView的左角是四舍五入的以适合父视图.

这就是我现在得到的:

enter image description here

这是我的列表视图行的XML布局:

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

我遵循了this tutorial.但是ImageView包含了填充.但是,当我删除填充时,它无法正常工作.

解决方法:

我认为您应该在加载图像时应用转换.例如,如果您使用picasso加载图像,则可以应用此转换:

基类:

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

gradle文件中的毕加索依赖关系:

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

例:

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

标签:android-layout,android-view,android-imageview,android
来源: https://codeday.me/bug/20191119/2033407.html