一:處理bitmap達(dá)到圓角效果
/*** 自定義的圓角矩形ImageView确憨,可以直接當(dāng)組件在布局中使用译荞。
*@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,一般設(shè)置成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瓤的。存在問(wèn)題:圖片只能為全部顯示,而且是fitxy的形式吞歼。如果要其他的圖片顯示方式圈膏,不支持。
二:通過(guò)設(shè)置canves篙骡,直接畫(huà)出圓角控件
@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);
這行代碼操作畫(huà)出10度的角
可能有一些小問(wèn)題稽坤,因?yàn)橹划?huà)了控件,沒(méi)有處理bitmap糯俗。不太清楚有沒(méi)有優(yōu)化問(wèn)題尿褪。
三:參見(jiàn)弘揚(yáng)大神微博? 給跪!http://blog.csdn.net/lmj623565791/article/details/41967509