【问题标题】:Android tiling a background image: using a Bitmap resizes the image?Android平铺背景图像:使用位图调整图像大小?
【发布时间】:2011-06-13 20:57:13
【问题描述】:

我想在我创建的布局中在 x 方向上有一个平铺背景。我已经按照网上找到的一些很好的说明正确平铺,但是源图像现在垂直拉伸了很多。我认为这与位图文件类型的性质有关。

示例图片:

第一张图片是平铺前的背景图片。第二张图片是平铺后的。如您所见,图像被垂直拉伸了一点,并且由于调整大小而显得有点模糊。

我尝试将图像放在 drawable-hdpi 和 drawable 文件夹中。我尝试将 XML 文件放在两个文件夹中。这些似乎都不重要。

这是生成这两个图像的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:background="@drawable/bg" />
<ImageView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dip"
    android:background="@drawable/bg_repeat" />
</LinearLayout>

"@drawable/bg" 是实际图像本身,bg.png。 “@drawable/bg_repeat”是以下位图 XML 文件:

<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/cdetail_bg_unclaimed_details"
android:antialias="false"
android:dither="false"
android:filter="false"
android:gravity="center|center_vertical"
android:tileMode="repeat" />

对于 x-repeat 平铺是否有替代方法?还是有一些我还没有检查过的解决方法?我已经更改了抗锯齿、抖动、过滤器等的所有选项。似乎没有任何改变。

感谢您的帮助!

编辑:这似乎是使用图形布局工具的错误。在我的手机上看起来不错。

【问题讨论】:

    标签: android android-layout drawable xml-drawable


    【解决方案1】:

    这似乎是使用图形布局工具的错误。在我的手机上看起来不错。

    【讨论】:

      【解决方案2】:

      您不能使用 xml 在一个方向上重复位图背景,但 Java 代码肯定可以做到这一点。

      BitmapDrawable bitmap = (BitmapDrawable) getResources().getDrawable(R.drawable.image);
       bitmap.setTileModeX(TileMode.REPEAT);
      

      所以现在如果你将这个“位图”设置为任何视图的背景

      例如:如果我们有一个 TextView "text" 那么 text.setBackgroundDrawable(bitmap) 将设置此文本视图的背景
      到“位图”。

      它将在 x 方向重复,但在 y 方向保持高度

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 2015-12-19
        • 1970-01-01
        • 2011-11-16
        • 1970-01-01
        • 2015-06-04
        • 1970-01-01
        • 2013-02-17
        • 2015-04-30
        • 2018-01-01
        相关资源
        最近更新 更多