首页 > 移动平台 > 详细

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

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

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获取圆角矩形、圆形图片

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!