当前位置: 移动技术网 > IT编程>移动开发>Android > Android 圆角 ImageView类可设置弧度(代码简单)

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

2019年07月24日  | 移动技术网IT编程  | 我要评论

兵不厌诈的意思,七十年代电影,投影仪论坛

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

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类可设置弧度的内容小编就给大家介绍到这里,希望对大家有所帮助!

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网