2011-04-30 85 views

回答

55

您可以为您的ImageView的‘边界’(实际上是背景)的资源(层绘制XML),并在themeImageView声明的背景资源是可绘制的XML。

如果你需要这个“边界”的基础上ImageView的状态(focusedselected等)被改变,那么你就应该创造更多的层可绘制,并把它们连成一个选择XML(可拉伸的状态) 。
然后在您的主题中,您应该将ImageView的背景设置为selector.xml

更新
下面是如何指定一个简单的边框为图像的样本,这将导致

efteling with border

你必须

  1. 创建一个新层可绘制文件(image_border.xml),
  2. 修改/创建您的styles.xml文件
  3. 修改/创建colors.xml文件
  4. 修改您的布局xml文件(或者代码)的样式应用到ImageView

RES /抽拉/ image_border.xml

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item> 
     <shape android:shape="rectangle"> 
      <gradient android:angle="90" 
       android:startColor="@color/image_border_start" 
       android:centerColor="@color/image_border_center" 
       android:endColor="@color/image_border_end" /> 
     </shape> 
    </item> 
    <item android:top="2dp" android:left="2dp" 
     android:right="2dp" android:bottom="2dp"> 
     <shape android:shape="rectangle"> 
      <solid android:color="@color/default_back_color" /> 
     </shape> 
    </item> 
</layer-list> 

RES /值/ styles.xml
添加下列行:

<style name="myImageView"> 
    <!-- 3dp so the background border to be visible --> 
    <item name="android:padding">3dp</item> 
    <item name="android:background">@drawable/image_border</item> 
    <item name="android:scaleType">fitCenter</item> 
</style> 

RES /值/ colors.xml
添加以下行:

<color name="image_border_start">#40990000</color> 
<color name="image_border_end">#FF660000</color> 
<color name="image_border_center">#FFFF3333</color> 

最后指定您布局XMLImageView的风格:

<ImageView android:id="@+id/my_image" 
    android:layout_width="100dp" android:layout_height="100dp" 
    android:src="@drawable/efteling" 
    style="@style/myImageView" /> 
+0

感谢朋友提供的信息。 – Piyush 2011-04-30 11:37:50

+1

没问题,我希望你可以从上面的例子开始。如果你需要更多的信息,随时问:) – rekaszeru 2011-04-30 11:39:26

+0

再次感谢代码....... – Piyush 2011-04-30 14:08:34

8

您可以使用此XML文件作为绘制的,而不是多个文件,但这个 文件被放置在可绘制的文件夹中。在ImageView中,使用这个XML文件作为 后台可绘制,如:android:background="@drawable/following code file name"

我希望这对你有帮助。

<?xml version="1.0" encoding="UTF-8"?> 
<shape xmlns:android="http://schemas.android.com/apk/res/android"> 
    <solid android:color="#FFFFFF" /> 
    <stroke android:width="1dp" android:color="#000000" /> 
    <padding android:left="1dp" android:top="1dp" android:right="1dp" 
     android:bottom="1dp" /> 
</shape> 
1

我尝试了所有上述解决方案,但他们不适合我! 所以我想出了一个简单的解决方案! :-)

我记得我在下面的article上阅读了关于Android的FrameLayout,它说它可以帮助我们按照我们添加它们的相同顺序堆叠我们的UI元素。

解决方案:

<FrameLayout 
    android:layout_width="112dp" 
    android:layout_height="112dp" 
    android:layout_marginLeft="16dp" <!-- May vary according to your needs --> 
    android:layout_marginRight="16dp" <!-- May vary according to your needs --> 
    android:layout_centerVertical="true"> 
    <!-- following imageView acts as the boarder which sitting in the background of our main container ImageView --> 
    <ImageView 
     android:layout_width="112dp" 
     android:layout_height="112dp" 
     android:background="#000"/> 
    <!-- following imageView holds the image as the container to our image --> 
    <!-- layout_margin defines the width of our boarder, here it's 1dp --> 
    <ImageView 
     android:layout_width="110dp" 
     android:layout_height="110dp" 
     android:layout_margin="1dp" 
     android:id="@+id/itemImageThumbnailImgVw" 
     android:src="@drawable/banana" 
     android:background="#FFF"/> 
</FrameLayout> 

前瞻:

就是这样。你会得到如下imageView!

preview of the ImageView with boarder

优点和缺点:

我觉得这是很简单的解决方案在其他地方使用,它是你做的所有事情坐落在一个单独的地方,以便更容易地对其进行修改。但我不喜欢不得不添加两个ImageView。