【问题标题】:Programmatically Change Gradient Background of Widget以编程方式更改小部件的渐变背景
【发布时间】:2014-02-13 06:01:19
【问题描述】:

我想要完成的事情:

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

显然这是不可能的。

我怎样才能做到这一点?(如果可能的话)

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

我尝试将我的可绘制对象转换为位图并调用setImageViewBitmap。我用this code 转换并使用this code 来获取宽度/高度,但我无法填充小部件(此外,设备的显示宽度/高度真的不是我需要的)

【问题讨论】:

  • View.setBackgroundDrawable(Drawable) 在 API16 中已弃用。该方法现在是View.setBackground(Drawable)。这不行吗?
  • 这是一个小部件,所以我必须通过不提供该方法的RemoteViews 设置它。我写这行只是为了明确说明我想要什么。

标签: android bitmap widget drawable


【解决方案1】:

我只是猜测。能否尝试扩展 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;
    }
}

【讨论】:

  • 我看到了那个方法,但使用它并不是特别舒服,因为文档状态为Caller beware: this may throw
  • 我还没有机会测试或玩弄这个,但它似乎是最有前途的方法。
  • 我很好奇 what 可能会使用该方法抛出...似乎从一开始就在该短语中缺少单词(我记得看过 RemoteViews 文档3年前的第一次)
【解决方案2】:
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 访问。我只能将位图传递给小部件,我找不到一种方法来设置具有适当宽度/高度的位图以匹配小部件的大小。
  • 找不到颜色定义结果
【解决方案3】:

好久没做RemoteViews了:

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

【讨论】:

  • 使用 RemoteViews 时不能自定义视图!
猜你喜欢
  • 2012-06-08
  • 2014-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-28
  • 1970-01-01
  • 2011-03-07
相关资源
最近更新 更多