自定义圆图片(2)

第一步首先自定义一个类继承ImageView

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实现绘制自定义图片

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);  
        }  

        public CircularImage(Context paramContext, AttributeSet paramAttributeSet) {  
            super(paramContext, paramAttributeSet);  
        }  

        public CircularImage(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {  
            super(paramContext, paramAttributeSet, paramInt);  
        }  
        public Bitmap createMask() {  
            int i = getWidth();  //宽度
            int j = getHeight();  //高度
            //图片配合 Bitmap.Config.ARGB_8888这个是颜色设置a代表的是Alpha rgb是RED GREEN BLUE三原色
            Bitmap.Config localConfig = Bitmap.Config.ARGB_8888; 
            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;  
        }  
}

第三步在布局中引入自定义的类

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
    <ImageView  
        android:layout_width="82.0dip"  
        android:layout_height="82.0dip"  
        android:layout_centerInParent="true"  
        android:contentDescription="@null" 
        android:background="@drawable/me_head_bg" 
        />  

    <com.example.head.CircularImage  
        android:id="@+id/cover_user_photo"  
        android:layout_width="74.0dip"  
        android:layout_height="74.0dip"  
        android:layout_centerInParent="true" />  

</RelativeLayout>

//在主类中为自定义图片赋值

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {
     @Override  
        protected void onCreate(Bundle savedInstanceState) {  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.activity_main);
            //控件获得id 
            CircularImage cover_user_photo = (CircularImage) findViewById(R.id.cover_user_photo);  
           //为控件设置背景图片
            cover_user_photo.setImageResource(R.drawable.face);  
        }  
}

运行的效果图:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值