Java 无法将颜色转换为int
我遇到了一个奇怪的错误,我无法用颜色对象设置绘制对象的颜色,这很奇怪,因为Java 无法将颜色转换为int,java,android,colors,Java,Android,Colors,我遇到了一个奇怪的错误,我无法用颜色对象设置绘制对象的颜色,这很奇怪,因为绘制.setColor(color.RED)有效,而绘制.setColor(this.color)无效 这是我的代码 public class Shape{ protected GameView2 game_view; protected int x; protected int y; protected int width; protected int height; pr
绘制.setColor(color.RED)
有效,而绘制.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);
}
}
编辑:
形状对象已声明为另一个名为GameView的类,
它非常大,因此我不会粘贴整个类,但在创建形状对象时,会这样做:
new Shape(this, Color.BLACK, 0, 0, 100, 100)
我得到的错误是
类型不兼容:无法将颜色转换为int
设置颜色需要int类型,但此类型。颜色
不是int
,您尝试设置颜色对象而不是int值
public native void setColor(int color);
setColor
需要int类型,但此类型。color
不是int
,您尝试设置颜色对象而不是int值
public native void setColor(int color);
查看,您会发现Color
是一个实用程序类,它提供了处理颜色int的静态方法。实际上,您可以实例化Color
对象似乎是一个历史错误。查看,您会发现Color
是一个实用程序类,它提供了处理颜色整数的静态方法。实际上可以实例化颜色
对象似乎是一个历史错误。如何将颜色
传递给新的形状
你应该使用
getResources().getColor(R.color.idcolor);
在xml中:
<color name="idcolor">#123456</color>
如何将颜色
传递给新的形状
你应该使用
getResources().getColor(R.color.idcolor);
在xml中:
<color name="idcolor">#123456</color>
你会犯什么样的错误?你能添加油漆类的代码吗?请阅读文档。特别是Color.BLACK
或Color.RED
的文档会出现什么样的错误?你能添加油漆类的代码吗?请阅读文档。特别是Color.BLACK
或Color.RED
的文档,谢谢,我已将Color
更改为int Color
好,谢谢,我已将Color
更改为int Color
是的。我不明白为什么颜色不是抽象的。举一个例子没有多大意义。是的。我不明白为什么颜色不是抽象的。举一个例子没有多大意义。