关闭

安卓图片圆角

75人阅读 评论(0) 收藏 举报
分类:



//首先写一个接口类继承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);  
        }  
    }  
}  

//接着写一个继承这个接口类


public class MyHeadImageView extends MaskedImage {  
    public MyHeadImageView(Context paramContext) {  
        super(paramContext);  
    }  
 
    public MyHeadImageView(Context paramContext, AttributeSet paramAttributeSet) {  
        super(paramContext, paramAttributeSet);  
    }  
 
    public MyHeadImageView(Context paramContext, AttributeSet paramAttributeSet, int paramInt) {  
        super(paramContext, paramAttributeSet, paramInt);  
    }  
 
    public Bitmap createMask() {  
        int i = getWidth();  
        int j = getHeight();  
        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;  
    }  
}

//最后在布局里引用(包名+类名)

ok  写个id直接将网络上获取的图片或是本地图片放在这个自定义的控件上就是圆角了


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:5404次
    • 积分:190
    • 等级:
    • 排名:千里之外
    • 原创:14篇
    • 转载:0篇
    • 译文:0篇
    • 评论:3条
    文章存档
    最新评论