圓形 ImageView 的實(shí)現(xiàn)方法

廢話不多說,直接上代碼

public class CircleDrawable extends Drawable {
            protected final float cornerRadius;
            protected final int margin;
            protected final RectF mRect = new RectF(),
                    mBitmapRect;
            protected final BitmapShader bitmapShader;
            protected final Paint paint;
            protected float center;

            public CircleDrawable(Bitmap bitmap, float center, int cornerRadius, int margin) {
                this.cornerRadius = cornerRadius;
                this.margin = margin;
                this.center = center;
                bitmapShader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
                mBitmapRect = new RectF (margin, margin, bitmap.getWidth() - margin, bitmap.getHeight() - margin);
                paint = new Paint();
                paint.setAntiAlias(true);
                paint.setShader(bitmapShader);
            }

            @Override
            protected void onBoundsChange(Rect bounds) {
                super.onBoundsChange(bounds);
                mRect.set(margin, margin, bounds.width() - margin, bounds.height() - margin);

                // Resize the original bitmap to fit the new bound
                Matrix shaderMatrix = new Matrix();
                shaderMatrix.setRectToRect(mBitmapRect, mRect, Matrix.ScaleToFit.FILL);
                bitmapShader.setLocalMatrix(shaderMatrix);

            }

            @Override
            public void draw(Canvas canvas) {
                canvas.drawCircle(center, center, cornerRadius, paint);
            }

            @Override
            public int getOpacity() {
                return PixelFormat.TRANSLUCENT;
            }

            @Override
            public void setAlpha(int alpha) {
                paint.setAlpha(alpha);
            }

            @Override
            public void setColorFilter(ColorFilter cf) {
                paint.setColorFilter(cf);
            }
        }
imageView.setImageDrawable(new CircleDrawable(bitmap, center, cornerRadius, margin));

實(shí)現(xiàn)效果圖如下


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,040評(píng)論 25 709
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,327評(píng)論 4 61
  • 日熙風(fēng)暖淡云天,草木悲思漫墓前。 紀(jì)念先人遙祭酒,子孫后裔續(xù)連綿。 【清風(fēng)閣】 ?
    清風(fēng)閣閱讀 184評(píng)論 0 1
  • 從技術(shù)細(xì)節(jié)看美團(tuán)的架構(gòu) 是2015年InfoQ 的一個(gè)演講, 鏈接可以直接看視頻. 看完我學(xué)到很多. 尤其對(duì)這個(gè)...
    haitaoyao閱讀 1,775評(píng)論 0 6
  • 秋天的樣子越來越顯眼了,早起的時(shí)候正好看到了太陽的升起。跑兩公里的時(shí)候也是感受陽光從軟弱變成強(qiáng)烈的過程。而...
    苿遺閱讀 297評(píng)論 0 0

友情鏈接更多精彩內(nèi)容