我有两种颜料,并将颜色设置为不同的颜色。问题是第二种涂料会覆盖第一种涂料。
这是我的代码:
public class Score {
static Globals g = Globals.getInstance();
private Rect bounds = new Rect();
private Paint paintBG = new Paint();
private Paint paintFG = new Paint();
private int mid;
public Score(Paint paint) {
this.paintBG = paint;
this.paintFG = paint;
// PROBLEME
paintBG.setColor(Color.GRAY);
paintFG.setColor(Color.WHITE); // <-- this paint overrides the paint before
}
public void draw(Canvas canvas) {
String score = String.valueOf(g.getScore());
paintFG.getTextBounds(score, 0, score.length(), bounds);
mid = (canvas.getWidth() / 2) - (bounds.width() / 2);
// different "paints" but the same color
canvas.drawText(score, mid, 60, paintBG);
canvas.drawText(score, mid, 50, paintFG);
}
}
来自德国的问候。 :)
答案 0 :(得分:1)
amahfouz解释了这个问题。该问题的一个解决方案是使用Paint
的复制构造函数。
public Score(Paint paint) {
paintBG = new Paint(paint);
paintFG = new Paint(paint);
paintBG.setColor(Color.GRAY);
paintFG.setColor(Color.WHITE);
}
答案 1 :(得分:0)
这是预期的。两个paint都引用了相同的Paint对象,作为参数传递。所以基本上,对setColor的调用都是修改同一个对象,最后一个是粘贴的对象!
zum wohle!