簡體   English   中英

以編程方式更改小部件的漸變背景

[英]Programmatically Change Gradient Background of Widget

我想要完成的事情:

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

顯然這是不可能的。

我怎么能做到這一點? (如果它是可能的)

我不想在xml文件中為不同的顏色創建多個形狀,因為這限制了選項。

我已經嘗試將我的drawable轉換為位圖並調用setImageViewBitmap 我使用此代碼轉換並使用此代碼獲取寬度/高度,但我無法填充窗口小部件(此外,設備的顯示寬度/高度確實不是我需要的)

我只是在猜測。 您可以嘗試擴展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;
    }
}
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:

您是否可以添加具有采用單個字符串的方法的自定義視圖。 然后你可以使用RemoteViews.setString調用這個方法,這個方法可以解析字符串中的數字並將其作為背景應用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM