转载 Android简单实现ImageView的加边框颜色的方法

对于一个ImageView,如果我们要加上边框,比如宽为3dp的灰色框,我们怎么做呢?

有种方法,是自己定义一个MyImageView类,继承自ImageView,在其onDraw方法中画canvas。

还有设置一个带有边框的背景图片的方法。

这里给出一种简单方法,既不需要创建衍生的类,也不需要准备图片。采用xml定义的方式实现。

背景定义xml: bg_border1.xml

  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android">  
  3.   <solid android:color="#EEEEEE" />  
  4.   <stroke android:width="3dp" android:color="#EEEEEE" />  
  5.   <corners android:radius="0dp" />  
  6.   <padding android:left="0dp" android:top="0dp"   
  7.     android:right="0dp" android:bottom="0dp" />  
  8. </shape>  


在Imageview定义页面上使用代码:

  1. <ImageView android:id="@+id/iv_thumbnail"  
  2.            android:layout_height="63dp"   
  3.            android:layout_width="63dp"  
  4.            android:background="@drawable/bg_border1"  
  5.            android:padding="3dp"  
  6.         />  

这样就可以使用了。

效果如下图所示:

posted @ 2011-09-13 09:40  小0  阅读(948)  评论(0编辑  收藏  举报