纸上得来终觉浅,绝知此事要躬行。

 

Android 画圆角

代码如下:

    /**
     * 画圆角
     * @param bitmap 要画圆角的bitmap
     * @param radiusPx  圆角大小,值越大越圆
     * @return    画完圆角的Bitmap
     */
    private Bitmap DrawRadius(Bitmap bitmap,float radiusPx){
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888); 
        Canvas canvas = new Canvas(output);            
        final int color = 0xff424242;          
        final Paint paint = new Paint();
        // 下面的四个参数分别为:left,top,right,bottom
        final Rect rect = new Rect(1, 0, bitmap.getWidth()-1, bitmap.getHeight()-1);          
        final RectF rectF = new RectF(rect);          
        final float roundPx = radiusPx;            
        paint.setAntiAlias(true);          
        canvas.drawARGB(0, 0, 0, 0);          
        paint.setColor(color);          
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);            
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));          
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }

Rect的四个参数前两个是以父窗口的左上角为参考点开始画,后两个可以分别理解为要画的宽度和高度。

 

posted on 2012-08-24 16:38  JRoger  阅读(1521)  评论(0编辑  收藏  举报

导航