实现Imageview两个圆角的效果

一,用自定义imageview

[java]  view plain  copy
  1. import android.content.Context;  
  2. import android.graphics.Canvas;  
  3. import android.graphics.Path;  
  4. import android.graphics.RectF;  
  5. import android.util.AttributeSet;  
  6. import android.widget.ImageView;  
  7.   
  8.   
  9. /* 
  10. *用来显示不规则图片, 
  11. * 上面两个是圆角,下面两个是直角 
  12. * */  
  13. public class OvalImageView extends ImageView {  
  14.   
  15.   
  16.     /*圆角的半径,依次为左上角xy半径,右上角,右下角,左下角*/  
  17.     private float[] rids = {10.0f, 10.0f, 10.0f, 10.0f, 0.0f, 0.0f, 0.0f, 0.0f,};  
  18.   
  19.   
  20.     public OvalImageView(Context context) {  
  21.         super(context);  
  22.     }  
  23.   
  24.   
  25.     public OvalImageView(Context context, AttributeSet attrs) {  
  26.         super(context, attrs);  
  27.     }  
  28.   
  29.   
  30.     public OvalImageView(Context context, AttributeSet attrs, int defStyleAttr) {  
  31.         super(context, attrs, defStyleAttr);  
  32.     }  
  33.   
  34.   
  35.   
  36.   
  37.     /** 
  38.      * 画图 
  39.      * by Hankkin at:2015-08-30 21:15:53 
  40.      * 
  41.      * @param canvas 
  42.      */  
  43.     protected void onDraw(Canvas canvas) {  
  44.         Path path = new Path();  
  45.         int w = this.getWidth();  
  46.         int h = this.getHeight();    
  47.         /*向路径中添加圆角矩形。radii数组定义圆角矩形的四个圆角的x,y半径。radii长度必须为8*/  
  48.         path.addRoundRect(new RectF(00, w, h), rids, Path.Direction.CW);  
  49.         canvas.clipPath(path);  
  50. //或者在绘制bitmap时使用canvas.drawPath();
  51.         super.onDraw(canvas);  
  52.     }  
  53. }    

二,借助glide(glide是一个图片加载库,很强大,大家自己了解怎么使用,使用起来也很简单)
[java]  view plain  copy
  1. //上面两个圆角,下面两个直角  
  2.         Glide.with(this)  
  3.                 .load(images2)  
  4.                 //                .bitmapTransform(new GrayscaleTransformation(this))//带灰色蒙层  
  5.                 .bitmapTransform(new RoundedCornersTransformation(this600,  
  6.                         RoundedCornersTransformation.CornerType.TOP))  
  7.                 .into(image3);    
  8.   
  9.   
  10. //下面两个圆角,上面两个直角  
  11.         Glide.with(this)  
  12.                 .load(images2)  
  13.                 //                .bitmapTransform(new GrayscaleTransformation(this))//带灰色蒙层  
  14.                 .bitmapTransform(new RoundedCornersTransformation(this600,  
  15.                         RoundedCornersTransformation.CornerType.BOTTOM))  
  16.                 .into(image3);  
  17.   
  18.   
  19. 注:glide可以实现圆角图片,右边两个圆角左边两个直角等奇葩得效果  



效果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值