加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Android自定义圆角ImageView

发布时间:2020-12-15 00:23:49 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import android.content.Context; import android.content.res.TypedArray; import android.graphics.Bitmap; import android.graphics.Bitmap.Config

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

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

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读