gpt4 book ai didi

Android Shape Drawable改变属性

转载 作者:行者123 更新时间:2023-11-30 02:25:22 26 4
gpt4 key购买 nike

我有几个形状可绘制资源,我想用作按钮的背景,除了渐变开始和结束颜色以及描边颜色外,它们是相似的。问题是我不想为此可绘制对象创建多个类似的 XML 文件。

我想我可以像使用标签的样式一样继承可绘制的形状,但这是不可能的。

我找到了这个 Defining XML Parent Style of Gradient / Shape / Corners但我不明白它是如何工作的。他如何改变属性值?

Using parents for drawable resources我发现我可以通过图层列表更改背景的外观,图层列表将一层一层地绘制可绘制对象。但是如何使用图层列表来处理渐变和笔触颜色?

我发现我可以在运行时更改按钮背景的外观,但可能有更简单的方法来做到这一点?

最佳答案

我最初的建议是不可行的。我以为 <shape>将返回 ShapeDrawable但我错了,这里是我将如何修改渐变。

点击按钮之前 before

点击按钮后,其背景发生变化。 after

这是主要 Activity

public class MainActivity extends Activity {

Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button = (Button)findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
try
{
// you can use the GradientDrawable directly, check the docs for details,
// I extended GradientDrawable just to make myself look like a badass :)

MyReflection.invokeMethod(button, new Gradient(), "setBackground");
}

catch (NoSuchMethodException e)
{
e.printStackTrace();
}

catch (InvocationTargetException e)
{
e.printStackTrace();
}

catch (IllegalAccessException e)
{
e.printStackTrace();
}
}
});

}
}

使用对象获取新方法的实用方法来调用该方法,Drawable用作新背景,方法名称不带 ()例如 setBackground在旧版本上运行。反射很慢。谨慎使用。

/*
* We want to use reflection because the View.setBackground() method
* is only available on API level 16 and up.
* If you don't understand this, you can follow the tutorial provided by
* Oracle, just search for it on Google with the keyword "reflection tutorial"
*/

public class MyReflection {

public static void invokeMethod(Object receiverObject, Drawable background, String methodName)
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException
{
Class<?> clazz = View.class;

Method method = clazz.getMethod(methodName, Drawable.class);

// Allowing non-public access call to this method.
method.setAccessible(true);
method.invoke(receiverObject, background);
}
}

我的自定义渐变

/**
* I am extending the GradientDrawable just for fun, and also
* because I want to set the shader property for this drawable,
* you could, however, use the GradientDrawable directly.
*/
public class Gradient extends GradientDrawable {

private Paint paint;
private LinearGradient gradient;

public Gradient()
{
super();
// check the docs for LinearGradient for these parameters
gradient = new LinearGradient(0,0, 100, 100, Color.BLUE, Color.GREEN, Shader.TileMode.REPEAT);
paint = new Paint();
paint.setShader(gradient);
}

public Gradient(GradientDrawable.Orientation orientation, int[] colors)
{
super(orientation, colors);
gradient = new LinearGradient(0,0, 100, 100, Color.BLUE, Color.GREEN, Shader.TileMode.REPEAT);
paint = new Paint();
paint.setShader(gradient);
}

@Override
public void draw(Canvas canvas)
{
canvas.drawPaint(paint);
}
}

按钮的初始背景

<?xml version="1.0" encoding="utf-8"?>
<!-- The initial shape with just a solid yellow color -->
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<solid
android:color="#ff0"/>

</shape>

关于Android Shape Drawable改变属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27961045/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com