gpt4 book ai didi

java - 如何在 android 中声明位图/可绘制对象

转载 作者:行者123 更新时间:2023-11-29 22:10:50 25 4
gpt4 key购买 nike

我无法在适当的 xml 中声明我的位图/可绘制对象根据“令人费解的”android 文档,您可以通过多种方式声明可绘制对象

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

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

或者,等等......

下面是我尝试过但没有成功的十几个 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) 声明可绘制/位图资源 (drawables.xml) 的 xml 的名称应该是什么?2)xml文件应该驻留在/在哪个资源文件夹(res/values)中或者?3) 有人可以提供一个在 xml 中声明的位图的工作示例,没有任何 xml 或 IDE 错误吗?

最佳答案

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

回答您的具体问题:

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

关于java - 如何在 android 中声明位图/可绘制对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9674609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com