【问题标题】:Android bitmap tileModeY="repeat" with gravity="fill_horizontal" creates ugly edge带有重力=“fill_horizo​​ntal”的Android位图tileModeY=“repeat”创建丑陋的边缘
【发布时间】:2019-12-30 16:25:30
【问题描述】:

我想为调整到手机宽度的整个应用创建垂直重复的背景。

我的位图是:

资源/drawable/repeating_bitmap.xml

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

我的风格是:

资源/值/style.xml

  <style name="AppTheme" parent="BaseTheme">
    <item name="android:windowBackground">@drawable/repeating_bitmap</item>
  </style>

我的问题就在这里:

如您所见,不是整个图像,而是边缘被拉伸,很奇怪。

如何实现水平拉伸和垂直重复背景? 我在位图上使用了正确的标志... (android:gravity="fill_horizo​​ntal" android:tileModeY="repeat")

如果这可以帮助我附加我重复的原始图像(mdpi)

我想使用重复位图来节省内存。这也是我目前唯一可用的 jpeg 模式。

更新

现在我知道为什么只有边缘被拉伸了:来自文档: https://developer.android.com/reference/android/graphics/drawable/BitmapDrawable

“启用平铺模式时忽略重力。默认值为“禁用”。”

这意味着当我得到 tileModeY="repeat" 时,默认情况下为 tileModeX,因为我整体启用了 tile 模式,必须是 tileModeX="clamp",即:

"clamp 复制边缘颜色。"

有什么想法可以同时使用 tileMode 和重力吗?我试图将此位图插入到重力为“fill_horizo​​ntal”的另一个视图中。可惜没有成功。

【问题讨论】:

  • 我面临着完全相同的问题。当我启用 tileModeY 时,重力几乎被忽略了。

标签: android bitmap repeat tile gravity


【解决方案1】:

我发现了一种非常奇特的方法来实现这一点:

  1. 从“位图”中移除 tileMode 属性
  2. 在 XML 中创建一个“layer-list”根标签
  3. 在带有顶部和高度属性的“位图”周围添加一个“项目”标签(将高度设置为图像的高度)
  4. 复制和粘贴此“项目”的次数不限,次数不限,以填满显示屏
  5. 为每个“项目”标签输入正确的最大值

这是一个示例,其中背景图像的高度为 116 像素。因此,以下每个标签都有一个按此高度增加的顶部值。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="0dp" android:height="116dp">
        <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
            android:gravity="fill_horizontal" android:src="@drawable/background" />
    </item>
    <item android:top="116dp" android:height="116dp">
        <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
            android:gravity="fill_horizontal" android:src="@drawable/background" />
    </item>
    <item android:top="232dp" android:height="116dp">
        <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
            android:gravity="fill_horizontal" android:src="@drawable/background" />
    </item>
    <item android:top="348dp" android:height="116dp">
        <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
            android:gravity="fill_horizontal" android:src="@drawable/background" />
    </item>
    <item android:top="464dp" android:height="116dp">
        <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
            android:gravity="fill_horizontal" android:src="@drawable/background" />
    </item>
    <item android:top="580dp" android:height="116dp">
        <bitmap xmlns:android="http://schemas.android.com/apk/res/android"
            android:gravity="fill_horizontal" android:src="@drawable/background" />
    </item>
</layer-list>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    • 2013-12-14
    • 1970-01-01
    • 1970-01-01
    • 2014-01-16
    • 2012-03-08
    • 1970-01-01
    相关资源
    最近更新 更多