如何在自定义形状xml中添加图像 [英] How can I add an Image in my custom shape xml

查看:127
本文介绍了如何在自定义形状xml中添加图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个XML选择器

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>

我只希望state1是透明的,以便将图像视为-是

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

状态1

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

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

现在我想让state2遮盖我已经完成的图片.我的问题是,除了阴影渐变以外,我还希望图标显示在中心.我不知道如何通过xml

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" />

推荐答案

您可以使用ex的layer-list来做到这一点:

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>

将以上内容另存为@ drawable/image_state2.

Save the above as @drawable/image_state2.

有关更多信息: 在可绘制图层列表中调整位图大小

这篇关于如何在自定义形状xml中添加图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆