Android自定義圓角ImageView

gcmc 9年前發布 | 3K 次閱讀 Java Android

我們經常看到一些app中可以顯示圓角圖片,比如qq的聯系人圖標等等,實現圓角圖片一種辦法是直接使用圓角圖片資源,當然如果沒有圓角圖片資源,我們也可以自己通過程序實現的,下面介紹一個自定義圓角ImageView的方法:

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;

public class RoundAngleImageView extends ImageView {
private int roundWidth = 13;
private int roundHeight = 13;

public RoundAngleImageView(Context context) {  
    super(context);  
    init(context, null);  
}  

public RoundAngleImageView(Context context, AttributeSet attrs, int defStyle) {  
    super(context, attrs, defStyle);  
    init(context, attrs);  
}  

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

private void init(Context context, AttributeSet attrs) {  
    if (attrs != null) {  
        TypedArray a = context.obtainStyledAttributes(attrs,  
                R.styleable.RoundAngleImageView);  
        roundWidth = a.getDimensionPixelSize(  
                R.styleable.RoundAngleImageView_roundWidth, roundWidth);  
        roundHeight = a.getDimensionPixelSize(  
                R.styleable.RoundAngleImageView_roundHeight, roundHeight);  
        a.recycle();  
    } else {  
        float density = context.getResources().getDisplayMetrics().density;  
        roundWidth = (int)(roundWidth * density);  
        roundHeight = (int)(roundHeight * density);  
    }  
}  

/** 重寫draw() */  
@Override  
public void draw(Canvas canvas) {  

    //實例化一個和ImageView一樣大小的bitmap  
    Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(),  
            Config.ARGB_8888);  

    //實例化一個canvas,這個canvas對應的內存為上面的bitmap  
    Canvas canvas2 = new Canvas(bitmap);  
    if (bitmap.isRecycled()) {  
        bitmap = Bitmap.createBitmap(getWidth(), getHeight(),  
                Config.ARGB_8888);  
        canvas2 = new Canvas(bitmap);  
    }  

    //將imageView自己繪制到canvas2上,這個導致bitmap里面存放了imageView  
    super.draw(canvas2);  

    //利用canvas畫一個圓角矩形,這個會修改bitmap的數據  
    drawRoundAngle(canvas2);  

    //將裁剪好的bitmap繪制到系統當前canvas上,這樣裁剪好的imageview就能顯示到屏幕上  
    Paint paint = new Paint();  
    paint.setXfermode(null);  
    canvas.drawBitmap(bitmap, 0, 0, paint);  
    bitmap.recycle();  
}  

public void setRoundWidth(int roundWidth, int roundHeight) {  
    this.roundWidth = roundWidth;  
    this.roundHeight = roundHeight;  
}  

private void drawRoundAngle(Canvas canvas)  
{  
    Paint maskPaint = new Paint();  
    maskPaint.setAntiAlias(true);  
    maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));  
    Path maskPath = new Path();    
    maskPath.addRoundRect(new RectF(0.0F, 0.0F, getWidth(), getHeight()), roundWidth, roundHeight, Path.Direction.CW);  

    //這是設置了填充模式,非常關鍵  
    maskPath.setFillType(Path.FillType.INVERSE_WINDING);  
    canvas.drawPath(maskPath, maskPaint);  
}  

} </pre>

 本文由用戶 gcmc 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!