关于 android ImageView 圆角处理 圆图

相关组件找到了4个

其一:http://stackoverflow.com/questions/16208365/create-circular-image-view-in-android

Java代码   收藏代码
  1. package cn.com.idress.view;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.Bitmap.Config;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Color;  
  8. import android.graphics.Paint;  
  9. import android.graphics.PixelFormat;  
  10. import android.graphics.PorterDuff.Mode;  
  11. import android.graphics.PorterDuffXfermode;  
  12. import android.graphics.Rect;  
  13. import android.graphics.drawable.BitmapDrawable;  
  14. import android.graphics.drawable.Drawable;  
  15. import android.util.AttributeSet;  
  16. import android.widget.ImageView;  
  17.   
  18. import com.lidroid.xutils.bitmap.core.AsyncDrawable;  
  19.   
  20. public class RoundedImageView1 extends ImageView {  
  21.   
  22. public RoundedImageView1(Context context) {  
  23.     super(context);  
  24.     // TODO Auto-generated constructor stub  
  25. }  
  26.   
  27. public RoundedImageView1(Context context, AttributeSet attrs) {  
  28.     super(context, attrs);  
  29. }  
  30.   
  31. public RoundedImageView1(Context context, AttributeSet attrs, int defStyle) {  
  32.     super(context, attrs, defStyle);  
  33. }  
  34.   
  35. @Override  
  36. protected void onDraw(Canvas canvas) {  
  37.   
  38.     Drawable drawable = getDrawable();  
  39.   
  40.     if (drawable == null) {  
  41.         return;  
  42.     }  
  43.   
  44.     if (getWidth() == 0 || getHeight() == 0) {  
  45.         return;   
  46.     }  
  47.     Bitmap b = null;  
  48.     if(drawable instanceof BitmapDrawable){  
  49.         b =  ((BitmapDrawable)drawable).getBitmap() ;  
  50.     }else if(drawable instanceof AsyncDrawable){  
  51.         b = Bitmap   
  52.                 .createBitmap(   
  53.                 getWidth(),   
  54.                 getHeight(),   
  55.                 drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888   
  56.                 : Bitmap.Config.RGB_565);   
  57.                 Canvas canvas1 = new Canvas(b);   
  58.                 // canvas.setBitmap(bitmap);   
  59.                 drawable.setBounds(00, getWidth(),   
  60.                 getHeight());   
  61.                 drawable.draw(canvas1);   
  62.     }  
  63.     Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);  
  64.   
  65.     int w = getWidth(), h = getHeight();  
  66.   
  67.   
  68.     Bitmap roundBitmap =  getCroppedBitmap(bitmap, w);  
  69.     canvas.drawBitmap(roundBitmap, 0,0null);  
  70.   
  71. }  
  72.   
  73. public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {  
  74.     Bitmap sbmp;  
  75.     if(bmp.getWidth() != radius || bmp.getHeight() != radius)  
  76.         sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);  
  77.     else  
  78.         sbmp = bmp;  
  79.     Bitmap output = Bitmap.createBitmap(sbmp.getWidth(),  
  80.             sbmp.getHeight(), Config.ARGB_8888);  
  81.     Canvas canvas = new Canvas(output);  
  82.   
  83.     final int color = 0xffa19774;  
  84.     final Paint paint = new Paint();  
  85.     final Rect rect = new Rect(00, sbmp.getWidth(), sbmp.getHeight());  
  86.   
  87.     paint.setAntiAlias(true);  
  88.     paint.setFilterBitmap(true);  
  89.     paint.setDither(true);  
  90.     canvas.drawARGB(0000);  
  91.     paint.setColor(Color.parseColor("#BAB399"));  
  92.     canvas.drawCircle(sbmp.getWidth() / 2+0.7f, sbmp.getHeight() / 2+0.7f,  
  93.             sbmp.getWidth() / 2+0.1f, paint);  
  94.     paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));  
  95.     canvas.drawBitmap(sbmp, rect, rect, paint);  
  96.   
  97.   
  98.             return output;  
  99. }  
  100.   
  101. }  

 

    else if(drawable instanceof AsyncDrawable){

              xxxxxxxxxxxxxxxxxx

    }

   加此处代码为解决xutils加载图片

 

   其二:https://github.com/vinc3m1/RoundedImageView

 此组件 用于圆角。圆图,但不支持xutils显示

 

   其三:https://github.com/lopspower/CircularImageView

   这个同样不支持xutils

   在 onDraw 方法中 加入判断即可

    else if(this.getDrawable() instanceof AsyncDrawable){

Drawable drawable = this.getDrawable();

image = Bitmap 

   .createBitmap( 

       getWidth(), 

       getHeight(), 

       drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 

       : Bitmap.Config.RGB_565); 

       Canvas canvas1 = new Canvas(image); 

       // canvas.setBitmap(bitmap); 

       drawable.setBounds(0, 0, getWidth(), 

       getHeight()); 

       drawable.draw(canvas1); 

}

 

         其四:https://github.com/hdodenhof/CircleImageView

   这个也不错。同样不支持xutils,判断尚不知道如何加

 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值