欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 圆角 ImageView类可设置弧度(代码简单)

程序员文章站 2024-02-28 11:02:22
废话不多说了,直接给大家贴代码了,具体代码如下所示: public class roundimageview extends imageview { priva...

废话不多说了,直接给大家贴代码了,具体代码如下所示:

public class roundimageview extends imageview {
private paint paint;
private int roundwidth = 50;
private int roundheight = 50;
private paint paint2;
public roundimageview(context context, attributeset attrs, int defstyle) {
super(context, attrs, defstyle);
init(context, attrs);
}
public roundimageview(context context, attributeset attrs) {
super(context, attrs);
init(context, attrs);
}
public void setroundvalue(float roundvalue) {
roundwidth = (int) roundvalue;
roundheight = (int) roundvalue;
}
public roundimageview(context context) {
super(context);
init(context, null);
}
@suppresslint("recycle")
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);
} 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);
drawliftup(canvas2);
drawrightup(canvas2);
drawliftdown(canvas2);
drawrightdown(canvas2);
canvas.drawbitmap(bitmap, 0, 0, paint2);
bitmap.recycle();
bitmap = null;
}
private void drawliftup(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 drawliftdown(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, getwidth()), 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 drawrightup(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);
}
}

好了,有关android 圆角 imageview类可设置弧度的内容小编就给大家介绍到这里,希望对大家有所帮助!