Android以编程方式平铺图像作为背景

标签 android image background

我有一个图像,我想水平平铺,我需要以编程方式完成它。两种方法都试过了,都不行。

button_inner_shadow 是图像 navigation_background 是用来平铺上图的xml

1:直接平铺图片

BitmapDrawable navigationBackground = new BitmapDrawable(BitmapFactory.decodeResource(
                getResources(), R.drawable.button_inner_shadow));
navigationBackground.setTileModeX(Shader.TileMode.REPEAT);
navigationTextViews[id].setBackgroundDrawable(navigationBackground);

2:使用xml平铺图片

navigationBackground = new BitmapDrawable(BitmapFactory.decodeResource(
                getResources(), R.drawable.navigation_background));
navigationTextViews[id].setBackgroundDrawable(navigationBackground);

导航背景

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

我做错了什么?

我还在程序的另一部分使用 setBackgroundResource 设置更改背景颜色,我认为这是一个问题。我添加了 navigationTextViews[id].setBackgroundResource(0);,它应该会删除背景资源,但它不适用于我使用的上述解决方案。

编辑:navigationTextViews[] 是一个 TextViews 数组

最佳答案

嗯,出于某种原因,第一个选项使用不同的图像,所以我猜问题是我的第一个图像(具有一定透明度的渐变)太透明了。

关于Android以编程方式平铺图像作为背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11199064/

相关文章:

android - 蓝牙SPP(串行)故障(Android)

c++ - 在服务器上使用的 2D 图形库

html - 在 CSS 中给 <html> 一个背景图片可以吗?

html - 来自不透明图像的半透明背景

css - 为什么当我将元素位置设置为 :absolute? 时,线性渐变消失了

android - 如何在安卓浏览器中查看pdf?

javascript - 是否可以将 Cordova exec 命令从外部网络发送到 cordova 应用程序?

java - firestore collectionreference .get() 未执行

css - 像启动画面一样的背景图像

javascript - jsp 页面中不显示背景图像和 Logo 图像?