gpt4 book ai didi

android - 试图画一个按钮 : how to set a stroke color and how to "align" a gradient to the bottom without knowing the height?

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:55:07 25 4
gpt4 key购买 nike

我正在以编程方式创建一个按钮。它是圆形的,具有渐变背景,工作正常,看起来也不错,但我无法做我想做的两件事:

  1. 设置具有给定颜色的 1 像素笔划。我尝试了 getPaint().setStroke(),但不知道如何设置描边颜色。我应该怎么做?
  2. 将渐变与按钮的底部对齐,无论它的高度是多少。这可能吗?

作为引用,这是我正在使用的代码:

Button btn = new Button(context);
btn.setPadding(7, 3, 7, 5);
btn.setTextColor(text_color);

// Create a gradient for the button. Height is hardcoded to 30 (I don't know the height beforehand).
// I wish I could set the gradient aligned to the bottom of the button.
final Shader shader = new LinearGradient(0, 0, 0, 30,
new int[] { color_1, color_2 },
null, Shader.TileMode.MIRROR);

float[] roundedCorner = new float[] { 5, 5, 5, 5, 5, 5, 5, 5 }
ShapeDrawable normal = new ShapeDrawable(new RoundRectShape(roundedCorner, null, null));
normal.getPaint().setShader(shader);
normal.setPadding(7, 3, 7, 5);

// Create a state list (I suppressed settings for pressed).
StateListDrawable state_list = new StateListDrawable();
state_list.addState(new int[] { }, normal);

btn.setBackgroundDrawable(state_list);

最佳答案

关于您的第一个问题,我也曾为此苦恼过,看起来 Drawables 本身(我使用的是 ShapeDrawable)或 Paint 类中没有任何合适的方法。但是,我能够扩展 ShapeDrawable 并覆盖 draw 方法,如下所示,以提供相同的结果:

public class CustomBorderDrawable extends ShapeDrawable {
private Paint fillpaint, strokepaint;
private static final int WIDTH = 3;

public CustomBorderDrawable(Shape s) {
super(s);
fillpaint = this.getPaint();
strokepaint = new Paint(fillpaint);
strokepaint.setStyle(Paint.Style.STROKE);
strokepaint.setStrokeWidth(WIDTH);
strokepaint.setARGB(255, 0, 0, 0);
}

@Override
protected void onDraw(Shape shape, Canvas canvas, Paint fillpaint) {
shape.draw(canvas, fillpaint);
shape.draw(canvas, strokepaint);
}

public void setFillColour(int c){
fillpaint.setColor(c);
}
}

关于android - 试图画一个按钮 : how to set a stroke color and how to "align" a gradient to the bottom without knowing the height?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2145131/

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