android 实现图片的边框

方案一:继承ImageView,在onDraw 重写画图函数,在图片边延画条灰色的线

方案二:在灰色背景下,内缩2个像索,再内嵌个图片。

  1. packagecom.xmz.activity;
  2. importandroid.content.Context;
  3. importandroid.graphics.Canvas;
  4. importandroid.graphics.Color;
  5. importandroid.graphics.Paint;
  6. importandroid.graphics.Rect;
  7. importandroid.util.AttributeSet;
  8. importandroid.widget.ImageView;
  9. publicclassImageViewBorderextendsImageView{
  10. privateStringnamespace="http://xmz.com";
  11. privateintcolor;
  12. publicImageViewBorder(Contextcontext,AttributeSetattrs){
  13. super(context,attrs);
  14. color=Color.parseColor(attrs.getAttributeValue(namespace,"BorderColor"));
  15. }
  16. @Override
  17. protectedvoidonDraw(Canvascanvas){
  18. super.onDraw(canvas);
  19. //画边框暂时去除小边框
  20. Rectrec=canvas.getClipBounds();
  21. rec.bottom--;
  22. rec.right--;
  23. Paintpaint=newPaint();
  24. paint.setColor(color);
  25. paint.setStyle(Paint.Style.STROKE);
  26. canvas.drawRect(rec,paint);
  27. }
  28. }

  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:xmz="http://xmz.com"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="wrap_content"
  7. >
  8. <TextView
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:text="方案一:"
  12. />
  13. <com.xmz.activity.ImageViewBorder
  14. android:layout_width="124px"
  15. android:layout_height="180px"
  16. android:background="@drawable/movie"
  17. xmz:BorderColor="GRAY"
  18. />
  19. <TextView
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:text="方案二:"
  23. />
  24. <LinearLayout
  25. android:layout_width="124px"
  26. android:layout_height="180px"
  27. android:background="@drawable/image_bg"
  28. android:padding="2px">
  29. <ImageView
  30. android:layout_width="wrap_content"
  31. android:layout_height="wrap_content"
  32. android:background="@drawable/movie"
  33. />
  34. </LinearLayout>
  35. </LinearLayout>

工程的资源下载:http://download.youkuaiyun.com/source/3386837


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值