Android 圆角 ImageView类可设置弧度(代码简单)
废话不多说了,直接给大家贴代码了,具体代码如下所示:
publicclassRoundImageViewextendsImageView{
privatePaintpaint;
privateintroundWidth=50;
privateintroundHeight=50;
privatePaintpaint2;
publicRoundImageView(Contextcontext,AttributeSetattrs,intdefStyle){
super(context,attrs,defStyle);
init(context,attrs);
}
publicRoundImageView(Contextcontext,AttributeSetattrs){
super(context,attrs);
init(context,attrs);
}
publicvoidSetRoundValue(floatroundValue){
roundWidth=(int)roundValue;
roundHeight=(int)roundValue;
}
publicRoundImageView(Contextcontext){
super(context);
init(context,null);
}
@SuppressLint("Recycle")
privatevoidinit(Contextcontext,AttributeSetattrs){
if(attrs!=null){
TypedArraya=context.obtainStyledAttributes(attrs,R.styleable.RoundAngleImageView);
roundWidth=a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundWidth,roundWidth);
roundHeight=a.getDimensionPixelSize(R.styleable.RoundAngleImageView_roundHeight,roundHeight);
}else{
floatdensity=context.getResources().getDisplayMetrics().density;
roundWidth=(int)(roundWidth*density);
roundHeight=(int)(roundHeight*density);
}
paint=newPaint();
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
paint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.DST_OUT));
paint2=newPaint();
paint2.setXfermode(null);
}
@Override
publicvoiddraw(Canvascanvas){
Bitmapbitmap=Bitmap.createBitmap(getWidth(),getHeight(),Config.ARGB_8888);
Canvascanvas2=newCanvas(bitmap);
super.draw(canvas2);
drawLiftUp(canvas2);
drawRightUp(canvas2);
drawLiftDown(canvas2);
drawRightDown(canvas2);
canvas.drawBitmap(bitmap,0,0,paint2);
bitmap.recycle();
bitmap=null;
}
privatevoiddrawLiftUp(Canvascanvas){
Pathpath=newPath();
path.moveTo(0,roundHeight);
path.lineTo(0,0);
path.lineTo(roundWidth,0);
path.arcTo(newRectF(0,0,roundWidth*2,roundHeight*2),-90,-90);
path.close();
canvas.drawPath(path,paint);
}
privatevoiddrawLiftDown(Canvascanvas){
Pathpath=newPath();
path.moveTo(0,getHeight()-roundHeight);
path.lineTo(0,getHeight());
path.lineTo(roundWidth,getHeight());
path.arcTo(newRectF(0,getHeight()-roundHeight*2,0+roundWidth*2,getWidth()),90,90);
path.close();
canvas.drawPath(path,paint);
}
privatevoiddrawRightDown(Canvascanvas){
Pathpath=newPath();
path.moveTo(getWidth()-roundWidth,getHeight());
path.lineTo(getWidth(),getHeight());
path.lineTo(getWidth(),getHeight()-roundHeight);
path.arcTo(newRectF(getWidth()-roundWidth*2,getHeight()-roundHeight*2,getWidth(),getHeight()),0,
90);
path.close();
canvas.drawPath(path,paint);
}
privatevoiddrawRightUp(Canvascanvas){
Pathpath=newPath();
path.moveTo(getWidth(),roundHeight);
path.lineTo(getWidth(),0);
path.lineTo(getWidth()-roundWidth,0);
path.arcTo(newRectF(getWidth()-roundWidth*2,0,getWidth(),0+roundHeight*2),-90,90);
path.close();
canvas.drawPath(path,paint);
}
}
好了,有关Android圆角ImageView类可设置弧度的内容小编就给大家介绍到这里,希望对大家有所帮助!