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

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

该图像是我想要得到的:

34c19e41973580fe9a299ad19bfcd714.png

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

这就是我现在得到的:

BLeFa.png

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

android:background="@drawable/list_row_bg"

android:orientation="horizontal"

android:layout_width="match_parent"

android:layout_height="match_parent">

android:layout_width="wrap_content"

android:layout_height="wrap_content">

android:background="@color/colorPrimary"

android:id="@+id/row_image"

android:scaleType="fitCenter"

android:layout_width="100dp"

android:layout_height="100dp" />

android:padding="@dimen/list_item_content_padding"

android:orientation="vertical"

android:layout_weight="1"

android:layout_width="wrap_content"

android:layout_height="wrap_content">

android:id="@+id/row_title"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:id="@+id/row_duration"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:id="@+id/row_file_size"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

android:id="@+id/row_description"

android:layout_width="wrap_content"

android:layout_height="wrap_content" />

我遵循了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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值