我是一名Android开发人员。我正在做一个游戏,我需要改变我的字母样式,如下图所示。如何在画布中添加渐变,如图
此代码添加渐变但是整个视图。如果底部的字母是白色的,如果顶部的字母是黑色的
Paint p = new Paint();
// start at 0,0 and go to 0,max to use a vertical
// gradient the full height of the screen.
p.setShader(new LinearGradient(0, 0, 0, getHeight(), Color.BLACK, Color.WHITE, Shader.TileMode.MIRROR));
canvas.drawPaint(p);
答案 0 :(得分:1)
尝试这样的事情:
class V extends View {
private String text = "Back To The Future";
private Paint paint;
public V(Context context) {
super(context);
paint = new Paint();
paint.setTextSize(32);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
Shader shader = new LinearGradient(0, paint.getTextSize() + bounds.top, 0, paint.getTextSize(), Color.RED, Color.YELLOW, TileMode.MIRROR);
paint.setShader(shader);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawText(text, 0, paint.getTextSize(), paint);
}
}