android - 使用 XML 中的 ImageView 在 android 中平铺图像

标签 android xml image tiling

我试图让背景上的图像平铺,直到背景满为止。

我当前的代码是:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
  <ImageView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:src="@drawable/cartoonclouds"
    android:contentDescription="@string/desc"
    android:tileMode="repeat" />
  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <TextView
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:text="@string/hello" />
  </LinearLayout>
</RelativeLayout>

然而,这只会使图像从下到上覆盖(不是平铺),而不是从左到右。我应该做什么?

编辑:尝试 XML 版本:

<bitmap
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:tileMode="repeat"
    android:src="@drawable/cartoonclouds" />

main.xml 可以在与该 XML 文件相同的文件夹中找到卡通云,但找不到该 XML 文件。

最佳答案

首先,将您的图像放在一个文件中,例如 res/drawable/cloud.png。这使得您的应用程序可以将其作为@drawable/cloud 访问。但它还没有平铺。

接下来,您应该使用 android:tileMode="repeat"定义一个位图资源 (1)。例如,您可以在 mytileablebitmap.xml 上定义它:

<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:tileMode="repeat" 
android:src="@drawable/cloud"/>

这是引用您的原始图 block 图像(可绘制/云)并制作一个知道如何平铺自身以填充可用空间的可绘制对象。

然后,当你想使用平铺背景时,使用“@drawable/mytileablebitmap”。

(1):http://developer.android.com/guide/topics/resources/drawable-resource.html

关于android - 使用 XML 中的 ImageView 在 android 中平铺图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8899222/

相关文章:

android - 单击启动器图标时恢复上次 Activity

android - 添加 PhoneGap Build Android 要求

java - 从根元素访问自定义 XML 属性

用于低 RAM 消耗的大文件的 C++ XML 解析库

Android:将图像转换为字节数组

android - 我怎样才能把日期和时间选择器放在android的对话框中

Android 应用架构 - 独立的代码层和数据库层

xml - 在 XPath 中提取以下同级节点的子节点属性

android - ActionBarSherlock 背景的图像大小

image - 如何批量处理色度关键照片(屏蔽绿屏)