怎么&是在android中声明bitmap / drawable

时间:2012-03-12 20:48:28

标签: java android android-layout

我在使用适当的xml声明我的位图/ drawable时遇到问题 根据“复杂的”android文档,你可以通过十几种方式声明drawables

<?xml version="1.0" encoding="utf-8"?>
<ImageView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/myimage" />

OR

<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
    android:src="@drawable/icon"
    android:tileMode="repeat" />

OR等......

Belows是我尝试过的十几个drawables.xml的两个例子但没有成功。 例1:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
    <item xmlns:android="http://schemas.android.com/apk/res/android" android:id="@id/enemyAirplane1" android:drawable="@drawable/enemyAirplane1" >
<bitmap  android:src="@drawable/enemyAirplane1" />  
 </item>
  <item xmlns:android="http://schemas.android.com/apk/res/android" android:id="@id/playerShape1" android:drawable="@drawable/playerShape1">
<bitmap  android:src="@drawable/playerShape1" />  
 </item>
</resources>

示例2:

<?xml version="1.0" encoding="UTF-8"?>
<resources>
<bitmap  android:src="@drawable/enemyAirplane1" />  
 <bitmap  android:src="@drawable/playerShape1" />  
</resources>

最后我的确切问题是: 1)声明drawable / bitmap资源(drawables.xml)或?的xml名称应该是什么? 2)xml文件应驻留在哪个资源文件夹(res / values)OR? 3)有人可以提供xml中声明的位图的工作示例,没有任何xml或IDE错误吗?

1 个答案:

答案 0 :(得分:2)

如果您有位图文件(.png,.jpg或.gif),您可以将其放在res / drawable /(或res / drawable /的任何带注释版本/例如res / drawable-hdpi)中并引用在您的布局文件中,就像在您的第一个代码段中一样。该片段将在res / drawable中查找以下四种可能之一:myimage.png,myimage.jpg,myimage.gif或myimage.xml。在xml文件中声明的位图也必须引用.png,.jpg或.gif资源。 xml文件中的声明用于创建别名和/或指定其他属性,例如抖动。

回答您的具体问题:

  1. 您不必创建xml文件来声明drawable 资源。您只需将文件放在res / drawable /文件夹中即可。
  2. 如上所述,声明drawable不需要xml文件 资源。
  3. 许多Android示例项目都包含drawable 资源。 Snake就是一个简单的例子。