Java 画布(onDraw)安卓

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20641869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 03:18:21  来源:igfitidea点击:

Canvas (onDraw) Android

javaandroidandroid-canvasondraw

提问by pancakeleh

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.

请指教。谢谢你。

采纳答案by industrychanger

You should be able to invalidate the canvas in your onClick method for each button. Add a few boolean variables to tell the onDraw method what color to 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();
    }
    });
}

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 方法无效时循环遍历它。

回答by Blackbelt

yes you have to onTouchEventin your view subclass. 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. When you get the ACTION_MOVE event you can change the position of the rectangle and call invalidate()to redraw. Plese get rid on the draw call inside the onClickListenerin your activity

是的,你必须 onTouchEvent在你的视图子类中。你可以在这里阅读文档。event 参数包含有关您获得的触摸类型的信息(ACTION_DOWN, ACTION_MOVE, ACTION_UP),还包含触摸事件的坐标。当您获得 ACTION_MOVE 事件时,您可以更改矩形的位置并调用invalidate()重绘。请onClickListener在您的活动中摆脱绘制调用