标签:
RoundedImageView,顾名思义,就是“圆形的ImageView”。RoundedImageView是基于Android官方的RoundedBitmapDrawable对ImageView进行的扩展,支持显示圆形,圆角图片。与Github上的众多CircleImageView项目比较,RoundedImageView实现细节相对简单,操作方便。
安装方法
在Android Studio中,以导入Module的方式添加RoundedImageView依赖(jCenter Remote Library Dependency等待更新...)。
基本用法
(1)显示圆形图片:
1 <org.warnier.zhang.widget.RoundedImageView 2 xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 android:layout_width="wrap_content" 5 android:layout_height="wrap_content" 6 android:scaleType="fitXY" 7 app:circular="true" 8 app:src="@drawable/logo" />
(2)显示圆角图片:
1 <org.warnier.zhang.nongji.widget.RoundedImageView 2 xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 android:layout_width="wrap_content" 5 android:layout_height="wrap_content" 6 android:scaleType="fitXY" 7 app:cornerRadius="8dp" 8 app:src="@drawable/logo" />
注意:“app:src”,而不是“android:src”,否则可能显示失败。
注意:同时使用“app:circular”,“app:cornerRadius”属性,显示圆角图片。
设计思想
重写setImageResource(),setImageBitmap()方法,将读取的图片资源用RoundedBitmapDrawable包装成需要的圆形或圆角图片。
源代码
1 package org.warnier.zhang.widget; 2 3 import android.content.Context; 4 import android.content.res.TypedArray; 5 import android.graphics.Bitmap; 6 import android.graphics.BitmapFactory; 7 import android.graphics.drawable.Drawable; 8 import android.support.v4.graphics.drawable.RoundedBitmapDrawable; 9 import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory; 10 import android.util.AttributeSet; 11 import android.widget.ImageView; 12 13 import org.warnier.zhang.nongji.R; 14 15 public class RoundedImageView extends ImageView { 16 private int mCornerRadius = 0; 17 private boolean mCircular = false; 18 private int mResource = 0; 19 20 public RoundedImageView(Context context) { 21 this(context, null); 22 } 23 24 public RoundedImageView(Context context, AttributeSet attrs) { 25 this(context, attrs, 0); 26 } 27 28 public RoundedImageView(Context context, AttributeSet attrs, int defStyleAttr) { 29 super(context, attrs, defStyleAttr); 30 TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs, R.styleable.RoundedImageView, 0, 0); 31 try { 32 mCornerRadius = typedArray.getDimensionPixelSize(R.styleable.RoundedImageView_cornerRadius, 0); 33 mCircular = typedArray.getBoolean(R.styleable.RoundedImageView_circular, false); 34 mResource = typedArray.getResourceId(R.styleable.RoundedImageView_src, 0); 35 if (mResource != 0) { 36 setImageResource(mResource); 37 } 38 } finally { 39 typedArray.recycle(); 40 } 41 } 42 43 @Override 44 public void setImageResource(int resId) { 45 super.setImageResource(resId); 46 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), resId); 47 setImageBitmap(bitmap); 48 } 49 50 @Override 51 public void setImageBitmap(Bitmap bitmap) { 52 super.setImageBitmap(bitmap); 53 setImageDrawable(round(bitmap)); 54 } 55 56 private Drawable round(Bitmap bitmap) { 57 RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(getResources(), bitmap); 58 drawable.setAntiAlias(true); 59 if (mCircular) { 60 drawable.setCircular(true); 61 } 62 if (mCornerRadius != 0) { 63 drawable.setCornerRadius(mCornerRadius); 64 } 65 return drawable; 66 } 67 68 public void setCornerRadius(int cornerRadius) { 69 mCornerRadius = cornerRadius; 70 } 71 72 public void setCircular(boolean circular) { 73 mCircular = circular; 74 } 75 76 public int getCornerRadius() { 77 return mCornerRadius; 78 } 79 80 public boolean isCircular() { 81 return mCircular; 82 } 83 }
标签:
原文地址:http://www.cnblogs.com/warnier-zhang/p/4973166.html