【发布时间】:2014-11-21 17:35:40
【问题描述】:
我遇到了一个奇怪的错误,我无法使用 Color 对象设置绘画对象的颜色,这很奇怪,因为 paint.setColor(Color.RED) 有效而 paint.setColor(this.color) 无效
这是我的代码。
public class Shape{
protected GameView2 game_view;
protected int x;
protected int y;
protected int width;
protected int height;
protected Color color;
public Shape(GameView2 game_view, Color color, int x, int y, int width, int height) {
this.game_view = game_view;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
this.color = color;
}
public void onDraw(Canvas canvas){
Paint paint = new Paint();
Rect rec = new Rect(x, y, x + width, y + height);
paint.setColor(this.color); //does not work
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(rec, paint);
}
}
编辑:
Shape 对象已被声明为另一个名为 GameView 的类, 它非常大,所以我不会粘贴整个类,但是当创建一个 Shape 对象时,这是完成的:
new Shape(this, Color.BLACK, 0, 0, 100, 100)
我得到的错误是incompatible types: Color cannot be converted to int
【问题讨论】:
-
你得到什么样的错误?能否请您添加 Paint 类的代码?
-
阅读文档。特别是
Color.BLACK或Color.RED的文档