收藏几种圆角ImageView

一:处理bitmap达到圆角效果

/*** 自定义的圆角矩形ImageView,可以直接当组件在布局中使用。

*@authorcaizhiming

**/publicclassXCRoundRectImageViewextendsImageView{privatePaint paint;publicXCRoundRectImageView(Context context) {this(context,null);

}publicXCRoundRectImageView(Context context, AttributeSet attrs) {this(context, attrs,0);

}publicXCRoundRectImageView(Context context, AttributeSet attrs,intdefStyle) {super(context, attrs, defStyle);

paint=newPaint();

}/*** 绘制圆角矩形图片

*@authorcaizhiming*/@OverrideprotectedvoidonDraw(Canvas canvas) {

Drawable drawable=getDrawable();if(null!=drawable) {

Bitmap bitmap=((BitmapDrawable) drawable).getBitmap();

Bitmap b= getRoundBitmap(bitmap,20);finalRect rectSrc =newRect(0,0, b.getWidth(), b.getHeight());finalRect rectDest =newRect(0,0,getWidth(),getHeight());

paint.reset();

canvas.drawBitmap(b, rectSrc, rectDest, paint);

}else{super.onDraw(canvas);

}

}/*** 获取圆角矩形图片方法

*@parambitmap

*@paramroundPx,一般设置成14

*@returnBitmap

*@authorcaizhiming*/privateBitmap getRoundBitmap(Bitmap bitmap,introundPx) {

Bitmap output=Bitmap.createBitmap(bitmap.getWidth(),

bitmap.getHeight(), Config.ARGB_8888);

Canvas canvas=newCanvas(output);finalintcolor =0xff424242;finalRect rect =newRect(0,0, bitmap.getWidth(), bitmap.getHeight());finalRectF rectF =newRectF(rect);

paint.setAntiAlias(true);

canvas.drawARGB(0,0,0,0);

paint.setColor(color);intx =bitmap.getWidth();

canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

paint.setXfermode(newPorterDuffXfermode(Mode.SRC_IN));

canvas.drawBitmap(bitmap, rect, rect, paint);returnoutput;

}

}

//这种情况下是处理的圆角的bitmap。存在问题:图片只能为全部显示,而且是fitxy的形式。如果要其他的图片显示方式,不支持。

二:通过设置canves,直接画出圆角控件

@Override

protectedvoidonDraw(Canvas canvas) {

Path clipPath =newPath();

intw =this.getWidth();

inth =this.getHeight();

clipPath.addRoundRect(newRectF(0,0, w, h),10.0f,10.0f, Path.Direction.CW);

canvas.clipPath(clipPath);

super.onDraw(canvas);

}

}

clipPath.addRoundRect(newRectF(0,0, w, h),10.0f,10.0f, Path.Direction.CW);

这行代码操作画出10度的角

可能有一些小问题,因为只画了控件,没有处理bitmap。不太清楚有没有优化问题。

三:参见弘扬大神微博  给跪!http://blog.csdn.net/lmj623565791/article/details/41967509

你可能感兴趣的:(收藏几种圆角ImageView)