How can I add an Image in my custom shape xml

不想你离开。 提交于 2019-12-11 12:59:54

问题


I'm writing an XML selector

<?xml version="1.0" encoding="utf-8"?>
   <selector xmlns:android="http://schemas.android.com/apk/res/android" >
   <item android:drawable="@drawable/image_state2" android:state_pressed="true"/>
   <item android:drawable="@drawable/image_state1"/>
</selector>

I just want state1 to be transparent.So that the image to be seen as - is

state1

   <shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<gradient
    android:angle="270"
    android:endColor="#00000000"
    android:startColor="#00000000" />

Now I want state2 to shade the pic a bit which I have accomplished already. My problem is that other than the shading gradient I want an icon to appear on the center. I don't know how to add this through xml

state2

   <shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >

<gradient
    android:angle="270"
    android:endColor="#80000000"
    android:startColor="#80000000" />


回答1:


You can do it by using layer-list for ex:

<?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="270"
    android:endColor="#80000000"
    android:startColor="#80000000" />
  </shape>
 </item>
 <item>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/background"
android:tileMode="repeat" android:gravity="center" />
 </item>
</layer-list>

Save the above as @drawable/image_state2.

For more info: Resize bitmap inside a drawable layer-list



来源:https://stackoverflow.com/questions/24993309/how-can-i-add-an-image-in-my-custom-shape-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!