[英]Programmatically Change Gradient Background of Widget
What I am trying to accomplish: 我想要完成的事情:
int[] colors = new int[]{colorDark,colorLight}
GradientDrawable gd = new GradientDrawable(TOP_BOTTOM, colors);
remoteView.setBackgroundDrawable(gd); //method does not exist
Obviously this is not possible. 显然这是不可能的。
How can I accomplish this? 我怎么能做到这一点? (if it's possible) (如果它是可能的)
I do not want to have to create multiple shapes in xml files for different colors, because this limits options. 我不想在xml文件中为不同的颜色创建多个形状,因为这限制了选项。
I have tried converting my drawable to a bitmap and calling setImageViewBitmap
. 我已经尝试将我的drawable转换为位图并调用setImageViewBitmap
。 I converted with this code and used this code to get the width/height, but I'm unable to get the widget to be filled (additionally, the device's display width/height really aren't what I need anyway) 我使用此代码转换并使用此代码获取宽度/高度,但我无法填充窗口小部件(此外,设备的显示宽度/高度确实不是我需要的)
I am just guessing. 我只是在猜测。 Can you try to extend RemoteViews and override the apply function: 您可以尝试扩展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");
Haven't done RemoteViews in a while: 一段时间没有完成RemoteViews:
Can you add a custom view that has a method that takes a single string. 您是否可以添加具有采用单个字符串的方法的自定义视图。 Then you could call this method using RemoteViews.setString and this method could parse the numbers from the string and apply it as a background. 然后你可以使用RemoteViews.setString调用这个方法,这个方法可以解析字符串中的数字并将其作为背景应用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.