码迷,mamicode.com
首页 > 移动开发 > 详细

android 利用Bitmap获取圆角矩形、圆形图片

时间:2014-07-22 22:43:15      阅读:207      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   color   使用   width   

1.在很多时候,我们要显示图片资源,需要将他的资源显示为圆角的;示例源码如下:

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap,float roundPx){    
            
        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();    
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());    
        final RectF rectF = new RectF(rect);    
     
        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;    
    }    

注意:bitmap为图片资源;roundPx圆角的角度值float;

2.得到圆形资源图片。

public static Bitmap getOvalBitmap(Bitmap bitmap){    
        
        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();    
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 
        
        final RectF rectF = new RectF(rect); 
     
        paint.setAntiAlias(true);    
        canvas.drawARGB(0, 0, 0, 0);    
        paint.setColor(color);    
        
        canvas.drawOval(rectF, paint);
     
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));    
        canvas.drawBitmap(bitmap, rect, rect, paint);    
        return output;  
    }    

注:以上代码均经测试实用,复制即可使用;如有问题请留言交谈;

 

android 利用Bitmap获取圆角矩形、圆形图片,布布扣,bubuko.com

android 利用Bitmap获取圆角矩形、圆形图片

标签:android   style   blog   color   使用   width   

原文地址:http://www.cnblogs.com/gaolizhong/p/3860045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!