Android 自定义圆角的实现

来源:互联网 发布:淘宝店铺发布宝贝视频 编辑:程序博客网 时间:2024/06/01 22:41

1.概述:

本文所说的方法是继承ImageView,使用Xfermode实现。
步骤:
在values目录下新建attrs文件,
1.自定义属性,可以定制颜色,半径,字体颜色和大小,例如:

<declare-styleable name="mView"> <attr name="round_radius" format="dimension" /> <attr name="round_color" format="color" /> <attr name="text_color" format="color" /> <attr name="text_size" format="dimension" /></declare-styleable>

但是暂时不需要定义那么多属性
下面定义圆角的度数:




2.然后在构造方法获得属性对应的值



3.重写ondraw方法:
  @Override    public void draw(Canvas canvas) {        //创建bitmap        final Bitmap composedBitmap;        final Bitmap originalBitmap;        //创建画布        final Canvas composedCanvas;        final Canvas originalCanvas;        final Paint paint;        final int height;        final int width;        width = getWidth();        height = getHeight();        //ARGB_4444 代表16位Alpha的位图        //ARGB_8888 代表32位ARGB位图        composedBitmap = Bitmap.createBitmap(width, height,                Bitmap.Config.ARGB_8888);        originalBitmap = Bitmap.createBitmap(width, height,                Bitmap.Config.ARGB_8888);        originalCanvas = new Canvas(originalBitmap);        paint = new Paint();        paint.setAntiAlias(true);        paint.setColor(Color.BLACK);        super.draw(originalCanvas);        composedCanvas.drawARGB(0, 0, 0, 0);        //指定RectF对象以及圆角半径来实现,该方法是绘制圆形的主要方法,同时也可以通过设置画笔的空心效果来绘制空心的圆形        //thi,roundness 分别是x,y方向的圆角半径        composedCanvas.drawRoundRect(new RectF(0, 0, width, height),                this.roundness, this.roundness, paint);        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));        composedCanvas.drawBitmap(originalBitmap, 0, 0, paint);        canvas.drawBitmap(composedBitmap, 0, 0, new Paint());    }

4.在xml使用自己定义的attr,要定义一个xml的命名空间然后再给自定义属性赋值,可以统一用:
http://schemas.android.com/apk/res-auto   res-auto是所有的自定义包名
也就是:


使用:xmlns:app="http://schemas.android.com/apk/App的Package名"应该也可以

5.最后再xml使用自定义view:


rectRoundRadius这个值可以任意设置自己想要的值。

最后附上自定义RoundedCornerImageView类:

public class RoundedCornerImageView extends ImageView {    private final float density = getContext().getResources().getDisplayMetrics().density;    private float roundness;    private static final int DEFAULT_RECT_ROUND_RADIUS = 0;    public RoundedCornerImageView(Context context) {//                super(context);        this(context, null);        //   init();    }    public RoundedCornerImageView(Context context, AttributeSet attrs) {//                super(context, attrs);        this(context, attrs, 0);        //   init();    }    public RoundedCornerImageView(Context context, AttributeSet attrs,                                  int defStyle) {        super(context, attrs, defStyle);        //获取attr文件下,名为RoundedCornerImageView        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.RoundedCornerImageView, defStyle, 0);        //获取值        roundness = ta.getDimensionPixelSize(R.styleable.RoundedCornerImageView_rectRoundRadius, dip2px(DEFAULT_RECT_ROUND_RADIUS));        ta.recycle();        //  init();    }    @Override    public void draw(Canvas canvas) {        //创建bitmap        final Bitmap composedBitmap;        final Bitmap originalBitmap;        //创建画布        final Canvas composedCanvas;        final Canvas originalCanvas;        final Paint paint;        final int height;        final int width;        width = getWidth();        height = getHeight();        //ARGB_4444 代表16位Alpha的位图        //ARGB_8888 代表32位ARGB位图        composedBitmap = Bitmap.createBitmap(width, height,                Bitmap.Config.ARGB_8888);        originalBitmap = Bitmap.createBitmap(width, height,                Bitmap.Config.ARGB_8888);        composedCanvas = new Canvas(composedBitmap);        originalCanvas = new Canvas(originalBitmap);        paint = new Paint();        paint.setAntiAlias(true);        paint.setColor(Color.BLACK);        super.draw(originalCanvas);        composedCanvas.drawARGB(0, 0, 0, 0);        //指定RectF对象以及圆角半径来实现,该方法是绘制圆形的主要方法,同时也可以通过设置画笔的空心效果来绘制空心的圆形        //thi,roundness 分别是x,y方向的圆角半径        composedCanvas.drawRoundRect(new RectF(0, 0, width, height),                this.roundness, this.roundness, paint);        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));        composedCanvas.drawBitmap(originalBitmap, 0, 0, paint);        canvas.drawBitmap(composedBitmap, 0, 0, new Paint());    }//    public float getRoundness() {//        return this.roundness / this.density;//    }////    public void setRoundness(float roundness) {//        this.roundness = roundness * this.density;//    }//    private void init() {//        setRoundness(5);//    }   //dp转px    private int dip2px(int dipVal) {        float scale = getResources().getDisplayMetrics().density;        return (int) (dipVal * scale + 0.5f);    }}

效果如下:




原创粉丝点击