Android:圆角ImageView

package com.example.adfaf;

import com.example.adfaf.R;
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 FilletImageView extends ImageView {  
  
    private Paint paint;  
    private Paint paint2; 
    private int roundWidth = 5;  
    private int roundHeight = 5;  
     
    public FilletImageView(Context context, AttributeSet attrs, int defStyle) {  
        super(context, attrs, defStyle);  
        init(context, attrs);  
    }  
  
    public FilletImageView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        init(context, attrs);  
    }  
  
    public FilletImageView(Context context) {  
        super(context);  
        init(context, null);  
    }  
      
    private void init(Context context, AttributeSet attrs) {  
        
        if(attrs != null) {     
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.FilletImageView);   
            roundWidth = a.getDimensionPixelSize(R.styleable.FilletImageView_roundWidth, roundWidth);  
            roundHeight = a.getDimensionPixelSize(R.styleable.FilletImageView_roundHeight, roundHeight);  
        }else {  
            float density = context.getResources().getDisplayMetrics().density;  
            roundWidth = (int) (roundWidth*density);  
            roundHeight = (int) (roundHeight*density);  
        }   
          
        paint = new Paint();  
        paint.setColor(Color.WHITE);  
        paint.setAntiAlias(true);  
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));  
          
        paint2 = new Paint();  
        paint2.setXfermode(null);  
    }  
      
    @Override  
    public void draw(Canvas canvas) {  
        Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);  
        Canvas canvas2 = new Canvas(bitmap);  
        super.draw(canvas2);  
        drawLeftTop(canvas2);  
        drawRightTop(canvas2);  
        drawLeftDown(canvas2);  
        drawRightDown(canvas2);  
        canvas.drawBitmap(bitmap, 0, 0, paint2);  
        bitmap.recycle();  
    }  
      
    private void drawLeftTop(Canvas canvas) {  
        Path path = new Path();  
        path.moveTo(0, roundHeight);  
        path.lineTo(0, 0);  
        path.lineTo(roundWidth, 0);  
        path.arcTo(new RectF(  
                0,   
                0,   
                roundWidth*2,   
                roundHeight*2),   
                -90,   
                -90);  
        path.close();  
        canvas.drawPath(path, paint);  
    }  
      
    private void drawLeftDown(Canvas canvas) {  
        Path path = new Path();  
        path.moveTo(0, getHeight()-roundHeight);  
        path.lineTo(0, getHeight());  
        path.lineTo(roundWidth, getHeight());  
        path.arcTo(new RectF(  
                0,   
                getHeight()-roundHeight*2,   
                0+roundWidth*2,   
                getHeight()),  
                90,   
                90);  
        path.close();  
        canvas.drawPath(path, paint);  
    }  
      
    private void drawRightDown(Canvas canvas) {  
        Path path = new Path();  
        path.moveTo(getWidth()-roundWidth, getHeight());  
        path.lineTo(getWidth(), getHeight());  
        path.lineTo(getWidth(), getHeight()-roundHeight);  
        path.arcTo(new RectF(  
                getWidth()-roundWidth*2,   
                getHeight()-roundHeight*2,   
                getWidth(),   
                getHeight()), 0, 90);  
        path.close();  
        canvas.drawPath(path, paint);  
    }  
      
    private void drawRightTop(Canvas canvas) {  
        Path path = new Path();  
        path.moveTo(getWidth(), roundHeight);  
        path.lineTo(getWidth(), 0);  
        path.lineTo(getWidth()-roundWidth, 0);  
        path.arcTo(new RectF(  
                getWidth()-roundWidth*2,   
                0,   
                getWidth(),   
                0+roundHeight*2),   
                -90,   
                90);  
        path.close();  
        canvas.drawPath(path, paint);  
    }

    //使ImageView的高等于宽,成正方形
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

		super.onMeasure(widthMeasureSpec, widthMeasureSpec);
	}  
    
}  

定义一个attr.xml的文件,放在values目录下面,内容如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
	<declare-styleable name="FilletImageView">
		<attr name="roundWidth" format="dimension" />
		<attr name="roundHeight" format="dimension" />
	</declare-styleable>   
</resources>

使用示例如下:

圆角效果图如下:


当ImageView为正方形且roundWidth=roundHeight=1/2 layout_width时,则变成的ImageView。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值