让圆形图像视图(而不是图像)图像、视图、圆形、而不是

2023-09-06 22:56:29 作者:可是你从来不在乎

要求是:

所需物品1:从URL抓取图像

Req 1 : Fetch images from url

R2:将它们保存在高速缓存

R2: save them in cache

R3:请ImageView的不圆润的形象

R3: make ImageView rounded not the image

因此​​,对于R1和放大器; R2我发现了一个库: http://loopj.com/android-smart-image-view/

So for R1 & R2 I found a library: http://loopj.com/android-smart-image-view/

有关R3我已经做了很多研究及发展; D,&安培;一切,我发现图像不ImageView的转换。这是我搜索:

For R3 I've done a lot of R&D , & everything I found converts the image not the ImageView. This is what I've searched:

​​面膜​​的ImageView与圆角背景

How作出的ImageView有圆角

https://github.com/vinc3m1/RoundedImageView

https://github.com/lopspower/CircularImageView

我知道这是可以使用ImageView的位图和放大器;获得圆润的形象,但与具体的图书馆中,我想用这是不可能的(也许可以用非常复杂的线程)。

I know it's possible to use the ImageView bitmap & get the image rounded but with the specific library I want to use that isn't possible(maybe possible with very complex threading).

所以,请帮我拿到了ImageView的不圆润的形象。

So please help me to get the ImageView rounded not the image.

推荐答案

所以这是简约版本:

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:

or use even shorter one when hardware acceleration is not used and you can use 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);
        }
    }
}