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

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获取圆角矩形、圆形图片,,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。