gpt4 book ai didi

android - 使 ImageView 变圆(不是图像)

转载 作者:行者123 更新时间:2023-11-29 15:18:02 24 4
gpt4 key购买 nike

要求是:

请求 1:从 url 中获取图像

R2:将它们保存在缓存中

R3: 使 ImageView 变圆而不是图像

所以对于 R1 和 R2,我找到了一个库: http://loopj.com/android-smart-image-view/

对于 R3,我做了很多研发工作,我发现的所有内容都会转换图像而不是 ImageView。这是我搜索过的:

Mask ImageView with round corner background

How to make an ImageView with rounded corners?

https://github.com/vinc3m1/RoundedImageView

https://github.com/lopspower/CircularImageView

我知道可以使用 ImageView 位图并使图像四舍五入,但是对于我想使用的特定库,这是不可能的(可能使用非常复杂的线程)。

所以请帮我把 ImageView 弄圆而不是图像。

最佳答案

所以这是简约版本:

class RoundImageView extends ImageView {
private static final int RADIUS = 32;
private Paint mPaint;
private Paint mSrcIn;
private RectF mRect;

public RoundImageView(Context context) {
super(context);
// setBackgroundColor(0xffffffff);
mSrcIn = new Paint();
mSrcIn.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mRect = new RectF();
}

@Override
public void onDraw(Canvas canvas) {
Drawable dr = getDrawable();
if (dr != null) {
mRect.set(dr.getBounds());
getImageMatrix().mapRect(mRect);
mRect.offset(getPaddingLeft(), getPaddingTop());

int rtc = canvas.saveLayer(mRect, null, Canvas.ALL_SAVE_FLAG);
// draw DST
canvas.drawRoundRect(mRect, RADIUS, RADIUS, mPaint);

canvas.saveLayer(mRect, mSrcIn, Canvas.ALL_SAVE_FLAG);
// draw SRC
super.onDraw(canvas);
canvas.restoreToCount(rtc);
}
}
}

或者在不使用硬件加速时使用更短的一个,你可以使用 Canvas.clipPath:

class RoundImageViewClipped extends ImageView {
private static final int RADIUS = 32;
private RectF mRect;
private Path mClip;

public RoundImageViewClipped(Context context) {
super(context);
// setBackgroundColor(0xffffffff);
mRect = new RectF();
mClip = new Path();
}

@Override
public void onDraw(Canvas canvas) {
Drawable dr = getDrawable();
if (dr != null) {
mRect.set(dr.getBounds());
getImageMatrix().mapRect(mRect);
mRect.offset(getPaddingLeft(), getPaddingTop());
mClip.reset();
mClip.addRoundRect(mRect, RADIUS, RADIUS, Direction.CCW);

canvas.clipPath(mClip);
super.onDraw(canvas);
}
}
}

关于android - 使 ImageView 变圆(不是图像),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22200614/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com