android ImageView 加边框, 加阴影,shadow

Java代码   收藏代码
  1. 比较简单的办法,效果比较丑陋,同理,你可以用阴影图片替代画线得到好的效果  
  2.   
  3. 重写ImageView  
  4.   
  5. public class HKImageView extends ImageView {  
  6.   
  7.     public HKImageView(Context context, AttributeSet attrs) {  
  8.         super(context, attrs, 0);  
  9.     }  
  10.   
  11.     public HKImageView(Context context) {  
  12.         super(context);  
  13.     }  
  14.   
  15.     @Override  
  16.     protected void onDraw(Canvas canvas) {  
  17.         Log.d("lg""onDraw");  
  18.         super.onDraw(canvas);  
  19.   
  20.         // 画边框  
  21.         Rect rect1 = getRect(canvas);  
  22.         Paint paint = new Paint();  
  23.         paint.setColor(Color.GRAY);  
  24.         paint.setStyle(Paint.Style.STROKE);  
  25.   
  26.         // 画边框  
  27.         canvas.drawRect(rect1, paint);  
  28.           
  29.         paint.setColor(Color.LTGRAY);  
  30.   
  31.         // 画一条竖线,模拟右边的阴影  
  32.         canvas.drawLine(rect1.right + 1, rect1.top + 2, rect1.right + 1,  
  33.                 rect1.bottom + 2, paint);  
  34.         // 画一条横线,模拟下边的阴影  
  35.         canvas.drawLine(rect1.left + 2, rect1.bottom + 1, rect1.right + 2,  
  36.                 rect1.bottom + 1, paint);  
  37.           
  38.         // 画一条竖线,模拟右边的阴影  
  39.         canvas.drawLine(rect1.right + 2, rect1.top + 3, rect1.right + 2,  
  40.                 rect1.bottom + 3, paint);  
  41.         // 画一条横线,模拟下边的阴影  
  42.         canvas.drawLine(rect1.left + 3, rect1.bottom + 2, rect1.right + 3,  
  43.                 rect1.bottom + 2, paint);  
  44.     }  
  45.   
  46.     Rect getRect(Canvas canvas) {  
  47.         Rect rect = canvas.getClipBounds();  
  48.         rect.bottom -= getPaddingBottom();  
  49.         rect.right -= getPaddingRight();  
  50.         rect.left += getPaddingLeft();  
  51.         rect.top += getPaddingTop();  
  52.         return rect;  
  53.     }  
  54. }  
  55.   
  56. 使用  
  57. 要给图片添加padding才有效果  
  58. imageView.setPadding(3355); 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值