android自定义view之圆形头像的完美例子

            前段时间对Android的paint和canvas做了深入的了解,针对于开发过程中遇到的头像几乎都是圆形的问题,抽空写了个demo,,废话不多说,直接上代码。

      首先自定义一个view,继承imageview。关于PorterDuff及Xfermode详解可点击链接,代码中就不做详细解释

package com.example.text;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Xfermode;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.ImageView;



public abstract class MaskedImage extends ImageView{
	private static final Xfermode MASK_XFERMODE;  
    private Bitmap mask;  
    private Paint paint;  
  
    static {  
        PorterDuff.Mode localMode = PorterDuff.Mode.DST_IN;  //取两层绘制交集。显示上层。
        MASK_XFERMODE = new PorterDuffXfermode(localMode);  
    }  
  
    public MaskedImage(Context paramContext) {  
        super(paramContext);  
    }  
  
    public MaskedImage(Context paramContext, AttributeSet paramAttributeSet) {  
        super(paramContext, paramAttributeSet);  
    }  
  
    public MaskedImage(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {  
        super(paramContext, paramAttributeSet, paramInt);  
    }  
  
    public abstract Bitmap createMask();  
   
    protected void onDraw(Canvas paramCanvas) {  
        Drawable localDrawable = getDrawable();  
        if (localDrawable == null)  
            return;  
        try {  
            if (this.paint == null) {  
                Paint localPaint1 = new Paint();  
                this.paint = localPaint1;  
                this.paint.setFilterBitmap(false);  
                Paint localPaint2 = this.paint;  
                Xfermode localXfermode1 = MASK_XFERMODE;  
                @SuppressWarnings("unused")  
                Xfermode localXfermode2 = localPaint2.setXfermode(localXfermode1);  
            }  
            float f1 = getWidth();  
            float f2 = getHeight();  
            int i = paramCanvas.saveLayer(0.0F, 0.0F, f1, f2, null, 31);  
            int j = getWidth();  
            int k = getHeight();  
            localDrawable.setBounds(0, 0, j, k);  
            localDrawable.draw(paramCanvas);  
            if ((this.mask == null) || (this.mask.isRecycled())) {  
                Bitmap localBitmap1 = createMask();  
                this.mask = localBitmap1;  
            }  
            Bitmap localBitmap2 = this.mask;  
            Paint localPaint3 = this.paint;  
            paramCanvas.drawBitmap(localBitmap2, 0.0F, 0.0F, localPaint3);  
            paramCanvas.restoreToCount(i);  
            return;  
        } catch (Exception localException) {  
            StringBuilder localStringBuilder = new StringBuilder()  
                    .append("Attempting to draw with recycled bitmap. View ID = ");  
            System.out.println("localStringBuilder=="+localStringBuilder);  
        }  
    }  
	
}

        然后再自定义一个类,继承上面自定义的MaskedImage。关于paint和canvas的详解点击打开链接

package com.example.text;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;

public class CircularImage extends MaskedImage{

	public CircularImage(Context paramContext) {
		super(paramContext);
		// TODO Auto-generated constructor stub
	}
	public CircularImage(Context paramContext, AttributeSet paramAttributeSet) {  
        super(paramContext, paramAttributeSet);  
    }  
  
    public CircularImage(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {  
        super(paramContext, paramAttributeSet, paramInt);  
    }  
  
	@Override
	public Bitmap createMask() {
		// TODO Auto-generated method stub
		int i = getWidth();  
        int j = getHeight();  
        Bitmap.Config localConfig = Bitmap.Config.ARGB_8888;  //Bitmap.Config是图片的质量参数,工四种,常用ARGB_8888(ALPHA_8,ARGB_4444,ARGB_8888,RGB_565)
        Bitmap localBitmap = Bitmap.createBitmap(i, j, localConfig);  //剪裁图片 
        Canvas localCanvas = new Canvas(localBitmap); //画布
        Paint localPaint = new Paint(1);  //画笔
        localPaint.setColor(-16777216);  
        float f1 = getWidth();  
        float f2 = getHeight();  
        RectF localRectF = new RectF(0.0F, 0.0F, f1, f2);  //画矩形
        localCanvas.drawOval(localRectF, localPaint); 
        return localBitmap;  	}

}
      到这自定义的view就算完成了,在xml布局中引用就可以。

  

      
  
  
      MainActivity的代码。

public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		CircularImage cover_user_photo = (CircularImage) findViewById(R.id.cover_user_photo);  
        cover_user_photo.setImageResource(R.drawable.face);  
	}

}

效果图如下:

android自定义view之圆形头像的完美例子_第1张图片



你可能感兴趣的:(原创)