Android中的ImageView只能顯示矩形的圖片,為了用戶體驗更多,Android實現(xiàn)圓角矩形,圓形或者橢圓等圖形,一般通過自定義ImageView來實現(xiàn),首先獲取到圖片的Bitmap,然后通過Paint和onDraw()進行圓形圖片顯示。

效果
代碼:
自定義ImageView類
/**
* 實現(xiàn)圓形、圓角,橢圓等自定義圖片View。
* @author zq
*
*/
public class ZQImageViewRoundOval extends ImageView {
private Paint mPaint;
private int mWidth;
private int mHeight;
private int mRadius;//圓半徑
private RectF mRect;//矩形凹行大小
private int mRoundRadius;// 圓角大小
private BitmapShader mBitmapShader;//圖形渲染
private Matrix mMatrix;
private int mType;// 記錄是圓形還是圓角矩形
public static final int TYPE_CIRCLE = 0;// 圓形
public static final int TYPE_ROUND = 1;// 圓角矩形
public static final int TYPE_OVAL = 2;//橢圓形
public static final int DEFAUT_ROUND_RADIUS = 10;//默認圓角大小
public ZQImageViewRoundOval(Context context) {
this(context, null);
// TODOAuto-generated constructor stub
}
public ZQImageViewRoundOval(Context context, AttributeSet attrs) {
this(context,attrs, 0);
// TODOAuto-generated constructor stub
}
public ZQImageViewRoundOval(Context context, AttributeSet attrs, int defStyle){
super(context,attrs, defStyle);
initView();
}
private void initView() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mMatrix = new Matrix();
mRoundRadius = DEFAUT_ROUND_RADIUS;
}
@Override
protected void onMeasure(int widthMeasureSpec, intheightMeasureSpec) {
// TODOAuto-generated method stub
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
// 如果是繪制圓形,則強制寬高大小一致
if (mType == TYPE_CIRCLE) {
mWidth = Math.min(getMeasuredWidth(),getMeasuredHeight());
mRadius = mWidth / 2;
setMeasuredDimension(mWidth, mWidth);
}
}
@Override
protected void onDraw(Canvas canvas) {
if (null ==getDrawable()) {
return;
}
setBitmapShader();
if (mType == TYPE_CIRCLE) {
canvas.drawCircle(mRadius, mRadius, mRadius, mPaint);
} else if (mType == TYPE_ROUND) {
mPaint.setColor(Color.RED);
canvas.drawRoundRect(mRect, mRoundRadius, mRoundRadius, mPaint);
}else if(mType == TYPE_OVAL){
canvas.drawOval(mRect, mPaint);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
// TODOAuto-generated method stub
super.onSizeChanged(w,h, oldw, oldh);
mRect = new RectF(0,0, getWidth(), getHeight());
}
/**
* 設(shè)置BitmapShader
*/
private void setBitmapShader() {
Drawable drawable = getDrawable();
if (null ==drawable) {
return;
}
Bitmap bitmap = drawableToBitmap(drawable);
// 將bitmap作為著色器來創(chuàng)建一個BitmapShader
mBitmapShader = newBitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);
float scale =1.0f;
if (mType == TYPE_CIRCLE) {
// 拿到bitmap寬或高的小值
int bSize =Math.min(bitmap.getWidth(), bitmap.getHeight());
scale = mWidth * 1.0f /bSize;
} else if (mType == TYPE_ROUND || mType == TYPE_OVAL) {
// 如果圖片的寬或者高與view的寬高不匹配,計算出需要縮放的比例;縮放后的圖片的寬高,一定要大于我們view的寬高;所以我們這里取大值;
scale = Math.max(getWidth() * 1.0f/ bitmap.getWidth(), getHeight() * 1.0f / bitmap.getHeight());
}
// shader的變換矩陣,我們這里主要用于放大或者縮小
mMatrix.setScale(scale,scale);
// 設(shè)置變換矩陣
mBitmapShader.setLocalMatrix(mMatrix);
mPaint.setShader(mBitmapShader);
}
/**
* drawable轉(zhuǎn)bitmap
*
* @paramdrawable
* @return
*/
private Bitmap drawableToBitmap(Drawable drawable) {
if (drawableinstanceof BitmapDrawable) {
BitmapDrawable bitmapDrawable =(BitmapDrawable) drawable;
returnbitmapDrawable.getBitmap();
}
int w =drawable.getIntrinsicWidth();
int h =drawable.getIntrinsicHeight();
Bitmap bitmap = Bitmap.createBitmap(w,h, Config.ARGB_8888);
Canvas canvas = newCanvas(bitmap);
drawable.setBounds(0, 0, w, h);
drawable.draw(canvas);
return bitmap;
}
/**
* 單位dp轉(zhuǎn)單位px
*/
public int dpTodx(int dp){
return (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dp,getResources().getDisplayMetrics());
}
public int getType(){
return mType;
}
/**
* 設(shè)置圖片類型:圓形、圓角矩形、橢圓形
* @param mType
*/
public void setType(int mType) {
if(this.mType !=mType){
this.mType = mType;
invalidate();
}
}
public int getRoundRadius() {
return mRoundRadius;
}
/**
* 設(shè)置圓角大小
* @parammRoundRadius
*/
public void setRoundRadius(int mRoundRadius) {
if(this.mRoundRadius !=mRoundRadius){
this.mRoundRadius =mRoundRadius;
invalidate();
}
}
}
MainActivity類
/****
*
* 自定義ImageView實現(xiàn)圓形圖片,圓角矩形圖片 橢圓圖片
*
* @authorzq
*
*/
public class MainActivity extends Activity {
private ZQImageViewRoundOvaliv_circle;//圓形圖片
private ZQImageViewRoundOvaliv_roundRect;//圓角矩形圖片
private ZQImageViewRoundOvaliv_oval;//橢圓圖片
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
initViews();
}
/**
* 初始化Views
*/
private void initViews(){
iv_circle =(ZQImageViewRoundOval)findViewById(R.id.cicle);
iv_roundRect =(ZQImageViewRoundOval)findViewById(R.id.roundRect);
iv_oval =(ZQImageViewRoundOval)findViewById(R.id.oval);
iv_roundRect.setType(ZQImageViewRoundOval.TYPE_ROUND);
iv_roundRect.setRoundRadius(6);//矩形凹行大小
iv_oval.setType(ZQImageViewRoundOval.TYPE_OVAL);
iv_oval.setRoundRadius(45);//圓角大小
}
}
源碼下載:
Eclipse下載:http://download.csdn.net/detail/dickyqie/9621330
AndroidStudio下載:https://github.com/DickyQie/android-imageview