简体   繁体   English

画布(onDraw)Android

[英]Canvas (onDraw) Android

I have been trying to draw different rectangles on the canvas after several times of button click. 多次单击按钮后,我一直试图在画布上绘制不同的矩形。 It should display different colored rectangle and the rectangle should remain on canvas after every button click. 它应显示不同颜色的矩形,并且每次单击按钮后该矩形应保留在画布上。 The rectangles should be able to move around the canvas. 矩形应该能够在画布上移动。 I have written the View class but i have no idea how to implement the onDraw() method on activity after a button click and also no idea of ways to create different color of rectangle. 我已经编写了View类,但是我不知道如何在单击按钮后在活动上实现onDraw()方法,也不清楚如何创建不同颜色的矩形。

I have 4 buttons on my main.xml file. 我的main.xml文件上有4个按钮。

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);

}

} }

This is my activity class. 这是我的活动课。

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();
    }
});
}

} }

Do i have to implement the onTouchListener as well so that the rectangles can move around? 我是否还必须实现onTouchListener以便矩形可以移动?

Please advice. 请指教。 Thank you. 谢谢。

yes you have to onTouchEvent in your view subclass. 是的,您必须在视图子类中使用onTouchEvent you can read the documentation here . 您可以在此处阅读文档。 The event parameter contains information about the kind of touch you are getting ( ACTION_DOWN , ACTION_MOVE , ACTION_UP ) and contains also the coordinates of the touch events. 事件参数包含有关您获得的触摸类型的信息( ACTION_DOWNACTION_MOVEACTION_UP ),还包含触摸事件的坐标。 When you get the ACTION_MOVE event you can change the position of the rectangle and call invalidate() to redraw. 当您收到ACTION_MOVE事件时,可以更改矩形的位置并调用invalidate()进行重绘。 Plese get rid on the draw call inside the onClickListener in your activity 普莱舍摆脱对内部绘图调用onClickListener您的活动

You should be able to invalidate the canvas in your onClick method for each button. 您应该能够在每个按钮的onClick方法中使画布无效。 Add a few boolean variables to tell the onDraw method what color to draw. 添加一些布尔变量以告诉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();
    }
    });
}

The update the onDraw method to check to see what color to draw. 更新onDraw方法以检查要绘制的颜色。

//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)

} } }}

In order to draw more Rectangles, you should store the previously drawn Rectangles in a Vector and loop through it each time you invalidate the onDraw method. 为了绘制更多的矩形,您应该将先前绘制的矩形存储在Vector中,并在每次使onDraw方法无效时在其中循环遍历。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM