Java 画布(onDraw)Android
我一直试图在画布上画不同的矩形后,多次点击按钮。它应该显示不同颜色的矩形,并且在每次单击按钮后矩形应该保持在画布上。矩形应该能够在画布上移动。我已经编写了View类,但我不知道如何在单击按钮后在activity上实现onDraw()方法,也不知道如何创建不同颜色的矩形 我的main.xml文件上有4个按钮Java 画布(onDraw)Android,java,android,android-canvas,ondraw,Java,Android,Android Canvas,Ondraw,我一直试图在画布上画不同的矩形后,多次点击按钮。它应该显示不同颜色的矩形,并且在每次单击按钮后矩形应该保持在画布上。矩形应该能够在画布上移动。我已经编写了View类,但我不知道如何在单击按钮后在activity上实现onDraw()方法,也不知道如何创建不同颜色的矩形 我的main.xml文件上有4个按钮 public class DrawRectangle extends View { public DrawRectangle(Context context){ super(con
public class DrawRectangle extends View {
public DrawRectangle(Context context){
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
Rect ourRect = new Rect();
ourRect.set(0, 0, canvas.getWidth()/2, canvas.getHeight()/2);
Paint blue = new Paint();
blue.setColor(Color.BLUE);
blue.setStyle(Paint.Style.FILL);
//Draw to actual canvas
canvas.drawRect(ourRect, blue);
}
}
这是我的活动课
public class MainActivity extends Activity {
Button bluebutton, redbutton, yellowbutton, greenbutton;
DrawRectangle dr;
Canvas canvas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dr = new DrawRectangle(this);
bluebutton = (Button)findViewById(R.id.bluebutton);
redbutton = (Button)findViewById(R.id.redbutton);
yellowbutton = (Button)findViewById(R.id.yellowbutton);
greenbutton = (Button)findViewById(R.id.greenbutton);
bluebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
dr.onDraw();
}
});
}
}
我是否也必须实现onTouchListener以便矩形可以四处移动
请给我一些建议。谢谢。是的,您必须在视图子类中
onTouchEvent
。您可以阅读文档。事件参数包含有关您正在获得的触摸类型的信息(ACTION\u DOWN
、ACTION\u MOVE
、ACTION\u UP
)以及触摸事件的坐标。
当您得到ACTION\u MOVE事件时,您可以更改矩形的位置并调用
invalidate()
重新绘制。请摆脱活动中onClickListener
中的draw调用是的,您必须在视图子类中onTouchEvent
。您可以阅读文档。事件参数包含有关您正在获得的触摸类型的信息(ACTION\u DOWN
、ACTION\u MOVE
、ACTION\u UP
)以及触摸事件的坐标。
当您得到ACTION\u MOVE事件时,您可以更改矩形的位置并调用
invalidate()
重新绘制。请在活动中的onClickListener
中删除draw调用您应该能够在onClick方法中为每个按钮使画布无效。添加一些布尔变量,告诉onDraw方法要绘制什么颜色
public static boolean isBlue, isRed, isYellow, isGreen;
public class MainActivity extends Activity {
Button bluebutton, redbutton, yellowbutton, greenbutton;
DrawRectangle dr;
Canvas canvas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dr = new DrawRectangle(this);
bluebutton = (Button)findViewById(R.id.bluebutton);
redbutton = (Button)findViewById(R.id.redbutton);
yellowbutton = (Button)findViewById(R.id.yellowbutton);
greenbutton = (Button)findViewById(R.id.greenbutton);
boolean blueColor = false;
bluebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
isBlue = true;
isRed = false;
isGreen = false;
isYellow = false;
dr.invalidate();
}
});
}
//Used for storing rectangles
public static List<Rect> rectangles;
public class DrawRectangle extends View {
public DrawRectangle(Context context){
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//Draw previous rectangles
for(int i=0;i<rectangles.size();i++){
canvas.drawRect(rectangles.get(i), paintColor );
}
Rect ourRect = new Rect();
ourRect.set(0, 0, canvas.getWidth()/2, canvas.getHeight()/2);
Paint paintColor = new Paint();
if(MainActivity.isBlue){
paintColor.setColor(Color.BLUE);
}
paintColor.setStyle(Paint.Style.FILL);
//Draw to actual canvas
canvas.drawRect(ourRect, paintColor );
rectangles.add(ourRect)
更新onDraw方法以查看要绘制的颜色
public static boolean isBlue, isRed, isYellow, isGreen;
public class MainActivity extends Activity {
Button bluebutton, redbutton, yellowbutton, greenbutton;
DrawRectangle dr;
Canvas canvas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dr = new DrawRectangle(this);
bluebutton = (Button)findViewById(R.id.bluebutton);
redbutton = (Button)findViewById(R.id.redbutton);
yellowbutton = (Button)findViewById(R.id.yellowbutton);
greenbutton = (Button)findViewById(R.id.greenbutton);
boolean blueColor = false;
bluebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
isBlue = true;
isRed = false;
isGreen = false;
isYellow = false;
dr.invalidate();
}
});
}
//Used for storing rectangles
public static List<Rect> rectangles;
public class DrawRectangle extends View {
public DrawRectangle(Context context){
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//Draw previous rectangles
for(int i=0;i<rectangles.size();i++){
canvas.drawRect(rectangles.get(i), paintColor );
}
Rect ourRect = new Rect();
ourRect.set(0, 0, canvas.getWidth()/2, canvas.getHeight()/2);
Paint paintColor = new Paint();
if(MainActivity.isBlue){
paintColor.setColor(Color.BLUE);
}
paintColor.setStyle(Paint.Style.FILL);
//Draw to actual canvas
canvas.drawRect(ourRect, paintColor );
rectangles.add(ourRect)
//用于存储矩形
公共静态列表矩形;
公共类DrawRectangle扩展视图{
公共绘图矩形(上下文){
超级(上下文);
}
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);
//绘制前面的矩形
对于(int i=0;i您应该能够在onClick方法中为每个按钮使画布无效。添加一些布尔变量来告诉onDraw方法要绘制什么颜色
public static boolean isBlue, isRed, isYellow, isGreen;
public class MainActivity extends Activity {
Button bluebutton, redbutton, yellowbutton, greenbutton;
DrawRectangle dr;
Canvas canvas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dr = new DrawRectangle(this);
bluebutton = (Button)findViewById(R.id.bluebutton);
redbutton = (Button)findViewById(R.id.redbutton);
yellowbutton = (Button)findViewById(R.id.yellowbutton);
greenbutton = (Button)findViewById(R.id.greenbutton);
boolean blueColor = false;
bluebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
isBlue = true;
isRed = false;
isGreen = false;
isYellow = false;
dr.invalidate();
}
});
}
//Used for storing rectangles
public static List<Rect> rectangles;
public class DrawRectangle extends View {
public DrawRectangle(Context context){
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//Draw previous rectangles
for(int i=0;i<rectangles.size();i++){
canvas.drawRect(rectangles.get(i), paintColor );
}
Rect ourRect = new Rect();
ourRect.set(0, 0, canvas.getWidth()/2, canvas.getHeight()/2);
Paint paintColor = new Paint();
if(MainActivity.isBlue){
paintColor.setColor(Color.BLUE);
}
paintColor.setStyle(Paint.Style.FILL);
//Draw to actual canvas
canvas.drawRect(ourRect, paintColor );
rectangles.add(ourRect)
更新onDraw方法以查看要绘制的颜色
public static boolean isBlue, isRed, isYellow, isGreen;
public class MainActivity extends Activity {
Button bluebutton, redbutton, yellowbutton, greenbutton;
DrawRectangle dr;
Canvas canvas;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dr = new DrawRectangle(this);
bluebutton = (Button)findViewById(R.id.bluebutton);
redbutton = (Button)findViewById(R.id.redbutton);
yellowbutton = (Button)findViewById(R.id.yellowbutton);
greenbutton = (Button)findViewById(R.id.greenbutton);
boolean blueColor = false;
bluebutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
isBlue = true;
isRed = false;
isGreen = false;
isYellow = false;
dr.invalidate();
}
});
}
//Used for storing rectangles
public static List<Rect> rectangles;
public class DrawRectangle extends View {
public DrawRectangle(Context context){
super(context);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//Draw previous rectangles
for(int i=0;i<rectangles.size();i++){
canvas.drawRect(rectangles.get(i), paintColor );
}
Rect ourRect = new Rect();
ourRect.set(0, 0, canvas.getWidth()/2, canvas.getHeight()/2);
Paint paintColor = new Paint();
if(MainActivity.isBlue){
paintColor.setColor(Color.BLUE);
}
paintColor.setStyle(Paint.Style.FILL);
//Draw to actual canvas
canvas.drawRect(ourRect, paintColor );
rectangles.add(ourRect)
//用于存储矩形
公共静态列表矩形;
公共类DrawRectangle扩展视图{
公共绘图矩形(上下文){
超级(上下文);
}
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);
//绘制前面的矩形
对于(int i=0;我感谢您的回复和建议。我已对代码进行了修改,但当单击按钮时,不会绘制矩形。问题,我是否必须在xml上指定表面视图?我已将setContentView()设置为=setContentView(dr);并绘制了矩形。但是4个按钮丢失了。我如何保留按钮和矩形?请提供main.xml文件好吗?我已经设法解决了。如果您不介意,可以详细说明用于存储先前绘制的矩形部分的向量吗?我可以使用List ListOfRect=new ArrayList();或矩形r=新矩形();要存储?我也可以使用此方法存储图像,对吗?此外,现在,当按钮单击时,矩形仅显示一次。我如何对其进行编码,以便即使按钮单击两次,也会显示两个矩形?感谢您的回复和建议。我已对代码进行了修改,但当按钮单击r时未绘制ectangle。问题,是否必须在xml上指定surfaceView?我已将setContentView()设置为be=setContentView(dr);并绘制了矩形。但是4个按钮丢失了。我如何保留按钮和矩形?请提供main.xml文件好吗?我已经设法解决了。如果您不介意,可以详细说明用于存储先前绘制的矩形部分的向量吗?我可以使用List ListOfRect=new ArrayList();或矩形r=new Rectangle();要存储?我也可以使用此方法存储图像,对吗?而且,现在,当按钮单击时,矩形仅显示一次。我如何对其进行编码,以便即使按钮单击两次,也会显示两个矩形?