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); 
要为 ImageView 边框,你可以使用以下方法: 1. 使用 xml 定义边框样式 在 res/drawable 目录下创建一个 xml 文件,例如 border.xml,内容如下: ```xml <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@android:color/transparent" /> <stroke android:width="2dp" android:color="@color/border_color" /> </shape> ``` 然后在布局文件中设置 ImageView 的背景为该 xml 文件: ```xml <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/border" /> ``` 2. 使用代码设置边框样式 在代码中使用 ShapeDrawable 类创建边框样式,并设置给 ImageView 的背景: ```java int borderWidth = 2; // 边框宽度,单位为像素 int borderColor = getResources().getColor(R.color.border_color); // 边框颜色 ShapeDrawable shapeDrawable = new ShapeDrawable(); shapeDrawable.setShape(new RectShape()); shapeDrawable.getPaint().setColor(Color.TRANSPARENT); // 设置背景颜色为透明 shapeDrawable.getPaint().setStyle(Paint.Style.STROKE); // 设置画笔样式为描边 shapeDrawable.getPaint().setStrokeWidth(borderWidth); // 设置描边宽度 shapeDrawable.getPaint().setStrokeCap(Paint.Cap.ROUND); // 设置描边端点形状为圆形 shapeDrawable.getPaint().setStrokeJoin(Paint.Join.ROUND); // 设置描边连接处形状为圆形 shapeDrawable.getPaint().setAntiAlias(true); // 开启抗锯齿 ImageView imageView = findViewById(R.id.image); imageView.setBackground(shapeDrawable); // 设置背景为 shapeDrawable ``` 以上两种方法都可以实现为 ImageView 边框的效果,选择哪种方法取决于你的实际需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值