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

时间:2014-01-22 22:34:46

标签: android bitmap widget drawable

我想要完成的任务:

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

显然这是不可能的。

我该如何做到这一点?(如果可能的话)

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

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

3 个答案:

答案 0 :(得分:7)

我只是在猜测。您可以尝试扩展RemoteView并覆盖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;
    }
}

答案 1 :(得分: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");

答案 2 :(得分:0)

暂时没有完成RemoteViews:

是否可以添加具有接收单个字符串的方法的自定义视图。 然后你可以使用RemoteViews.setString调用这个方法,这个方法可以解析字符串中的数字并将其作为背景应用。