【问题标题】:Repeat image gradient on background image在背景图像上重复图像渐变
【发布时间】:2013-05-31 17:00:58
【问题描述】:

我正在从服务器下载图像并将其设置为背景。但我需要为该背景图像分配重复图像渐变,并且我有一些文本需要在其上显示。

这是我的代码,用于从服务器下载图像并将其设置为背景。

Bitmap bitmap = DownloadImage(imageUrl);
BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
bitmapDrawable.setGravity(Gravity.BOTTOM|Gravity.RIGHT);
//Drawable d = new BitmapDrawable(bitmap);
myLinearLayout.setBackgroundDrawable(bitmapDrawable);   

我创建了一个需要显示在背景顶部的图层列表。

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="rectangle">
        <gradient
            android:type="radial" android:gradientRadius="500"
            android:startColor="#17568A"
            android:endColor="#494C4F" />  

    </shape>
</item>
<item>
    <bitmap
        android:src="@drawable/gradient_dish"
        android:tileMode="repeat" />
</item>
</layer-list>        

但我已经将服务器图像分配给布局背景,因此无法在背景中分配此 xml 文件。

我有一张图像“gradient_dish”,我想重复它并希望与背景图像一起显示。

我该怎么做?请给我一些参考或链接。
提前谢谢...

【问题讨论】:

  • 你不能简单地在代码中创建一个LayerDrawable,该代码来自从网络获取的BitmapDrawable + 你发布的 xml 可绘制图层中的图层并将其用作背景?
  • 你有这方面的参考代码吗?
  • 你能发布你的答案吗……你解决了我的问题,所以我只想把这个赏金分配给你……:-)

标签: android image gradient


【解决方案1】:

使用您从网络上检索到的图像,您可以构建所需的可绘制对象,还可以从您发布的 xml 文件中添加可绘制对象。您可以使用以下方法:

public Drawable createBackground(Bitmap bitmap) {
    Drawable[] layers = new Drawable[3];
    BitmapDrawable bitmapDrawable = new BitmapDrawable(bitmap);
    bitmapDrawable.setGravity(Gravity.BOTTOM | Gravity.RIGHT);
    layers[0] = bitmapDrawable;
    GradientDrawable gd = new GradientDrawable();
    gd.setShape(GradientDrawable.RECTANGLE);
    gd.setGradientRadius(500);
    gd.setGradientType(GradientDrawable.RADIAL_GRADIENT);
    gd.setColors(new int[] { Color.parseColor("#17568A"),
            Color.parseColor("#494C4F") });
    layers[1] = gd;
    BitmapDrawable lastBitmapDrawable = (BitmapDrawable) getResources()
            .getDrawable(R.id.gradient_dish);
    lastBitmapDrawable.setTileModeX(TileMode.REPEAT);
    layers[2] = lastBitmapDrawable;
    LayerDrawable bck = new LayerDrawable(layers);
    return bck;
}

那么你只需这样做:

Bitmap bitmap = DownloadImage(imageUrl);
myLinearLayout.setBackgroundDrawable(createBackground(bitmap)); 

【讨论】:

  • 你能帮个忙吗?它可以在模拟器上运行,但不能在真实设备上运行……怎么样?谢谢...
  • @SandipArmalPatil 哪种方式不起作用(不完整,图像根本没有出现)?另外,您在哪个设备上测试过它以及使用什么 api 级别(您是否也尝试过在其他设备上测试)?
【解决方案2】:

为什么不将图像作为背景放置在一个跨越整个屏幕的简单视图中,然后将布局的其余部分放在“顶部”,并使渐变半透明(分别从不透明的颜色,然后变成半透明或完全透明的颜色)。 这样,图像将根据需要通过真实的方式闪耀。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多