自定义圆形imageview

- 编辑:小昭 -

自定义圆形imageview

创建类

 public class ImageViewPlus extends ImageView {

        private Paint mpaint;
        private static Xfermode xfermode;
        private static Bitmap bitmap;
        private RectF rect;
    private String mImageUrl;
    private static HashMap<String, SoftReference<Drawable>> mImageCache = new HashMap<String, SoftReference<Drawable>>();

    public ImageViewPlus(Context context) {
            super(context);
            init();
        }

        public ImageViewPlus(Context context, AttributeSet attrs) {
            super(context, attrs);
            init();
        }

        public ImageViewPlus(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
            init();
        }
        private void init(){
            setLayerType(LAYER_TYPE_SOFTWARE, null);
            xfermode = new PorterDuffXfermode(PorterDuff.Mode.DST_IN);
            mpaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            int width = getWidth();
            int height = getHeight();
            Bitmap.Config config = Bitmap.Config.ARGB_8888;
            Canvas canvas1=null;
            BitmapDrawable drawable = (BitmapDrawable)getDrawable();
            drawable.setBounds(0, 0, width, height);
            drawable.draw(canvas);
            if (bitmap == null){
                Log.i("porterduffviewimage","bitmap==null");
                Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
                rect = new RectF(0,0,width,height);
                bitmap = Bitmap.createBitmap(width, height, config);
                canvas1 = new Canvas(bitmap);
                canvas1.drawOval(rect, paint);
            }
            mpaint.setXfermode(xfermode);
            canvas.drawBitmap(bitmap,0,0,mpaint);
        }


    public void setImageURI(String ttx) {
        mImageUrl = ttx;
        if (mImageCache.containsKey(ttx)) {
            SoftReference<Drawable> softReference = mImageCache.get(ttx);
            Drawable drawable = softReference.get();
            if (drawable != null) {
                setImageDrawable(drawable);
                return;
            }
    }


        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                Drawable drawable = (Drawable) msg.obj;
                setImageDrawable(drawable);
            }

        };

        new Thread() {
            @Override
            public void run() {
                Drawable drawable = LoadImageFromUrl(mImageUrl);
                mImageCache.put(mImageUrl,
                        new SoftReference<Drawable>(drawable));
                Message message = handler.obtainMessage();
                message.obj = drawable;
                handler.sendMessage(message);
            }
        }.start();
    }

    private Drawable LoadImageFromUrl(String imageUrl) {
        try {
            return Drawable.createFromStream(new URL(imageUrl).openStream(),
                    "src");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

控件那边

<com.zxwl.zxyph.ImageViewPlus
    android:id="@+id/grtx"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:layout_gravity="center"
    android:src="@mipmap/ic_launcher"
    android:layout_marginTop="98dp"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />


你会喜欢下面的文章? You'll like the following article.