以编程方式更改小部件的渐变背景

7

我想要实现的目标:

int[] colors = new int[]{colorDark,colorLight}
GradientDrawable gd = new GradientDrawable(TOP_BOTTOM, colors);
remoteView.setBackgroundDrawable(gd); //method does not exist

显然这是不可能的。

我该如何实现这个目标?(如果可能的话)

我不想为不同颜色创建多个xml文件中的形状,因为这样会限制选项。

我尝试将我的drawable转换为位图,并调用setImageViewBitmap。我使用这段代码进行转换,并使用这段代码获取宽度/高度,但我无法使小部件被填充(此外,设备的显示宽度/高度并不是我需要的)。


View.setBackgroundDrawable(Drawable)在API16中已被弃用。该方法现在是View.setBackground(Drawable)。这个不行吗? - MartinHaTh
1
这是一个小部件,所以我必须通过RemoteViews来设置它,但它并没有提供这个方法。我只是为了给出一个明确的示例而添加了那行代码。 - Reed
3个回答

8

我猜你可以尝试扩展RemoteViews并覆盖apply函数:

public class MySpecialRemoteViews extends RemoteViews {

    //add the Constructors

    public View apply(Context context, ViewGroup parent) {
        View result = super.apply(context, parent);

        //your code
        int[] colors = new int[]{colorDark,colorLight}
        GradientDrawable gd = new GradientDrawable(TOP_BOTTOM, colors);
        result.setBackgroundDrawable(gd);
        //end of your code

        return result;
    }
}

我看到了那个方法,但由于文档中声明“调用者需注意:这可能会抛出异常”,所以并不是特别舒服使用它。 - Reed
我还没有机会测试或玩弄这个,但它似乎是最有前途的方法。 - Reed
我很好奇在使用那种方法时会遇到什么问题...从开头看,这个短语中似乎有些词语失踪了(我记得三年前第一次看 RemoteViews 文档的时候就注意到这点了) - Jose_GD

0
use following class 

import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Shader;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.StateListDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.view.View;

public class UtilForGradientBackground {

   public static void gradientBgCreatorFromHex(View view, String bgColorHex, String gradColorHex) {

      ColorDefinitionResult bgColor = getArgbFromHexaString(bgColorHex);

      ColorDefinitionResult gradientColor = getArgbFromHexaString("1b6da7");
      CreateGradientBackground(view, bgColor, gradientColor);

   }

   public static void CreateGradientBackground(View view, ColorDefinitionResult bgColor, ColorDefinitionResult gradientColor) {

      int argbBgColor = Color.argb((int) bgColor.Alpha, bgColor.Red, bgColor.Green, bgColor.Blue);
      int argbGradient = Color.argb((int) gradientColor.Alpha, gradientColor.Red, gradientColor.Green, gradientColor.Blue);

      final Shader upperShader = new LinearGradient(0, 0, 0, 40, argbBgColor, argbGradient, Shader.TileMode.CLAMP);

      float[] roundedCorner = new float[] { 0, 0, 0, 0, 0, 0, 0, 0 };

      ShapeDrawable normal = new ShapeDrawable(new RoundRectShape(roundedCorner, null, null));
      normal.getPaint().setShader(upperShader);

      normal.setPadding(7, 3, 7, 0);
      StateListDrawable stateList = new StateListDrawable();
      stateList.addState(new int[] {}, normal);
      view.setBackgroundDrawable(stateList);
   }

   public static ColorDefinitionResult getArgbFromHexaString(String hexColorString) {
      ColorDefinitionResult colorDefinitionResult = new ColorDefinitionResult();
      if (hexColorString.length() == 6) {
         String redHex = hexColorString.substring(0, 2);
         String greenHex = hexColorString.substring(2, 4);
         String blueHex = hexColorString.substring(4, 6);
         colorDefinitionResult.Red = Integer.parseInt(redHex, 16);
         colorDefinitionResult.Green = Integer.parseInt(greenHex, 16);
         colorDefinitionResult.Blue = Integer.parseInt(blueHex, 16);
         colorDefinitionResult.Alpha = 255;

      }
      return colorDefinitionResult;
   }
}


and use it as follow:
give it your View id and RGB values as arguments
      View findViewById = findViewById(R.id.your_view_id);
      UtilForGradientBackground.gradientBgCreatorFromHex(findViewById, "2E64FE", "819FF7");

我很感激你的帮助,但这对小部件不起作用。我已经有了创建渐变的工作代码。那部分相当简单。问题在于我无法将可绘制对象传递给“RemoteViews”。而且我不能调用“view.setBackgroundDrawable(...)”,因为它只能通过“RemoteViews”访问。我只能将位图传递给小部件,并且我找不到一种方法来设置具有与小部件大小匹配的正确宽度/高度的位图。 - Reed
未找到ColorDefinitionResult。 - Sharath kumar

0

已经有一段时间没有使用RemoteViews了:

你能否添加一个自定义视图,其中包含一个接受单个字符串的方法。 然后,您可以使用RemoteViews.setString调用此方法,并且该方法可以从字符串中解析数字并将其应用为背景。


1
使用 RemoteViews 时无法创建自定义视图! - Jose_GD

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接