自定义view圆角图片带边框

[java]  view plain  copy
 print ?
  1. package com.zyh.cricleimageview;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.Canvas;  
  6. import android.graphics.Color;  
  7. import android.graphics.Matrix;  
  8. import android.graphics.Paint;  
  9. import android.graphics.Path;  
  10. import android.util.AttributeSet;  
  11. import android.widget.ImageView;  
  12.   
  13. /** 
  14.  * 圆角图片 
  15.  * @author zyh 
  16.  */  
  17. public class CircleImageView extends ImageView {  
  18.   
  19.     public CircleImageView(Context context) {  
  20.         super(context);  
  21.     }  
  22.   
  23.     public CircleImageView(Context context, AttributeSet attrs) {  
  24.         super(context, attrs);  
  25.     }  
  26.   
  27.     public CircleImageView(Context context, AttributeSet attrs, int defStyle) {  
  28.         super(context, attrs, defStyle);  
  29.     }  
  30.   
  31.     private int bWidth = 10;// 边框宽度  
  32.     private int bColor = Color.WHITE;// 边框颜色  
  33.   
  34.     /** 
  35.      * 设置圆形图片的边框颜色 
  36.      * @param color 
  37.      */  
  38.     public void setBorderColor(int color) {  
  39.         bColor = color;  
  40.     }  
  41.   
  42.     /** 
  43.      * 设置圆形图片的边框宽度 
  44.      * @param 
  45.      */  
  46.     public void setBorderWidth(int width) {  
  47.         bWidth = width;  
  48.     }  
  49.   
  50.     /** 
  51.      * 设置圆形图片的边框宽度和颜色 
  52.      * @param 
  53.      */  
  54.     public void setBorderWidth(int width, int color) {  
  55.         bWidth = width;  
  56.         bColor = color;  
  57.     }  
  58.   
  59.     @Override  
  60.     public void setImageBitmap(Bitmap bm) {  
  61.   
  62.         int d = Math.min(bm.getWidth(), bm.getHeight());// 或得圆直径  
  63.         Bitmap dest = Bitmap.createBitmap(d, d, bm.getConfig());// 创建一个副本  
  64.         // 画边框  
  65.         Canvas c = new Canvas(dest);  
  66.         Paint paint = new Paint();  
  67.         paint.setColor(bColor); // 边框颜色  
  68.         paint.setAntiAlias(true);// 设置抗锯齿  
  69.         c.drawCircle(d / 2, d / 2, d / 2, paint);  
  70.         // 画圆  
  71.         Path path = new Path();  
  72.         path.addCircle(d / 2, d / 2, d / 2 - bWidth, Path.Direction.CW);  
  73.         c.clipPath(path); // 裁剪区域  
  74.   
  75.         Matrix matrix = new Matrix();// 不缩放  
  76.         c.drawBitmap(bm, matrix, paint);// 把图画上去  
  77.         super.setImageBitmap(dest);  
  78.     }  
  79.   
  80. }  




使用 

[java]  view plain  copy
 print ?
  1. public class MainActivity extends Activity {  
  2.   
  3.     private CircleImageView iv;  
  4.   
  5.     @Override  
  6.     protected void onCreate(Bundle savedInstanceState) {  
  7.         super.onCreate(savedInstanceState);  
  8.         setContentView(R.layout.activity_main);  
  9.         iv = (CircleImageView) findViewById(R.id.iv);  
  10.         iv.setBorderWidth(15, Color.BLUE);// 设置边框  
  11.         iv.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.photo)); // 设置图片  
  12.     }  
  13. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值