3

目前,我正在为一个名为 PaintPot 的 Android 程序提供给我们的代码,该程序允许用户在他们的 Android 设备上进行手指绘画。

此代码处理在点击屏幕、单击按钮等时将发生的事件。

// Here is the event dispatcher for our app.  We need to Override the method for the Form
// superclass
@Override
public boolean dispatchEvent(Component component, String id, String eventName,
                             Object[] args) {

    //if the canvas is touched by a tapping finger
    if (component.equals(myCanvas) && eventName.equals("Touched")) {
        canvasTouced(((Float) args[0]).intValue(), ((Float) args[1]).intValue());
        return true;

        //if the canvas is touched by a dragging finger, paint the line this way
    } else if (component.equals(myCanvas) && eventName.equals("Dragged")) {
        drawLine(((Float) args[2]).intValue(),
                ((Float) args[3]).intValue(),
                ((Float) args[4]).intValue(),
                ((Float) args[5]).intValue());
        return true;

        //if the canvas is touched while the blue button is selected
    } else if (component.equals(btnBlue) && eventName.equals("Click")) {
        myCanvas.PaintColor(COLOR_BLUE);
        return true;

        //if the canvas is touched while the green button is selected
    } else if (component.equals(btnGreen) && eventName.equals("Click")) {
        myCanvas.PaintColor(COLOR_GREEN);
        return true;

        //if the canvas is touched while the red button is selected
    } else if (component.equals(btnRed) && eventName.equals("Click")) {
        myCanvas.PaintColor(COLOR_RED);
        return true;

        //if the wipe button is selected
    } else if (component.equals(btnWipe) && eventName.equals("Click")) {
        myCanvas.Clear();
        return true;
    }


    return false;
}

该代码工作正常,并且可以“按原样”执行我想要的操作。但我真的不明白的是,如果用户在画布上点击或拖动他或她的手指的 if 语句处于“同一级别”,或者如果选择了颜色按钮,则在同一 if 语句中。不应该是当用户在屏幕上点击或拖动他的手指时,确定是否制作的线或点的相同代码还应该询问线或点应该是什么颜色,而不仅仅是大小点,取决于选择什么颜色的按钮?

如需更完整的参考,这里是整个代码,如果有人好奇的话:

import android.graphics.Color;
import com.google.devtools.simple.runtime.components.android.Button;
import com.google.devtools.simple.runtime.components.Component;
import com.google.devtools.simple.runtime.components.HandlesEventDispatching;
import com.google.devtools.simple.runtime.components.android.Canvas;
import com.google.devtools.simple.runtime.components.android.Form;
import com.google.devtools.simple.runtime.components.android.HorizontalArrangement;
import com.google.devtools.simple.runtime.components.android.Label;
import com.google.devtools.simple.runtime.events.EventDispatcher;


import java.util.Random;




public class PaintPotActivity extends Form implements HandlesEventDispatching {


private Canvas myCanvas; //creates a canvas object
private Label lblStatus; //creates a label that discusses the status of the program
private Button btnRed; //creates a button for red paint
private Button btnBlue; // "" for blue paint
private Button btnGreen; // "" for green paint


private Button btnWipe; //creates a button that wipes the screen clean
private Button btnDotSize; // creates a button that changes the dot size




// Variable (field) used to for displaying number of touches
int numTouches; //declares an integer that lists out the number of touches a user made


// The equivalent to a "main" method for App Inventor apps is the $define method.
void $define() { 


    //We are going to place the color buttons in a HorizontalArrangement
    HorizontalArrangement hr = new HorizontalArrangement(this);
    btnRed = new Button(hr); 
    btnBlue = new Button(hr);
    btnGreen = new Button(hr);


    //set their color
    btnRed.BackgroundColor(Color.RED);
    btnBlue.BackgroundColor(Color.BLUE);
    btnGreen.BackgroundColor(Color.GREEN);

    //set the button text
    btnRed.Text("Red");
    btnBlue.Text("Blue");
    btnGreen.Text("Green");


    //canvas into its own HorizontalArrangement
    hr = new HorizontalArrangement(this);
    myCanvas = new Canvas(hr);
    myCanvas.Width(400);
    myCanvas.Height(400);
    myCanvas.LineWidth(10);


    //Wipe and a label into its own HorizontalArrangement
    hr = new HorizontalArrangement(this);
    btnWipe = new Button(hr);
    btnWipe.Text("Wipe");


    lblStatus = new Label(hr);
    lblStatus.Text("  touchX/touchY:");


    // Register for events.  By the second argument can be any string.    The third argument must
    // exactly match the name of the event that you want to handle for that component.  When the event
    // happens, dispatchEvent will be called with these arguments.
    EventDispatcher.registerEventForDelegation(this, "JavaBridge", "Touched");
    EventDispatcher.registerEventForDelegation(this, "JavaBridge", "Click");
    EventDispatcher.registerEventForDelegation(this, "JavaBridge", "Dragged");
}


// Here is the event dispatcher for our app.  We need to Override the method for the Form
// superclass
@Override
public boolean dispatchEvent(Component component, String id, String eventName,
                             Object[] args) {

    //if the canvas is touched by a tapping finger
    if (component.equals(myCanvas) && eventName.equals("Touched")) {
        canvasTouced(((Float) args[0]).intValue(), ((Float) args[1]).intValue());
        return true;

        //if the canvas is touched by a dragging finger, paint the line this way
    } else if (component.equals(myCanvas) && eventName.equals("Dragged")) {
        drawLine(((Float) args[2]).intValue(),
                ((Float) args[3]).intValue(),
                ((Float) args[4]).intValue(),
                ((Float) args[5]).intValue());
        return true;

        //if the canvas is touched while the blue button is selected
    } else if (component.equals(btnBlue) && eventName.equals("Click")) {
        myCanvas.PaintColor(COLOR_BLUE);
        return true;

        //if the canvas is touched while the green button is selected
    } else if (component.equals(btnGreen) && eventName.equals("Click")) {
        myCanvas.PaintColor(COLOR_GREEN);
        return true;

        //if the canvas is touched while the red button is selected
    } else if (component.equals(btnRed) && eventName.equals("Click")) {
        myCanvas.PaintColor(COLOR_RED);
        return true;

        //if the wipe button is selected
    } else if (component.equals(btnWipe) && eventName.equals("Click")) {
        myCanvas.Clear();
        return true;
    }


    return false;
}




/**
 * This method will get the touched touchX, touchY coordinates and will then create a circle
 * of random radius (between 1 to 33) with the color that was selected (RED, BLUE or GREEN).
 * It will also display the touched touchX,touchY coordinates.
 * @param x current x
 * @param y current y
 */
private void canvasTouced(int x, int y) {


    myCanvas.DrawCircle(x, y, new Random().nextInt(33));
    lblStatus.Text("  touchX/touchY:" + x + "/" + y + " touches: " + ++numTouches);


}


/**
 * Method to draw line
 * @param prevX last touch x
 * @param prevY last touch y
 * @param touchX current x
 * @param touchY current y
 */
private void drawLine(int prevX, int prevY, int touchX, int touchY) {
    myCanvas.DrawLine(prevX, prevY, touchX, touchY);
 }


}
4

3 回答 3

3

dispatchEvent在应用程序触发事件时调用。每次按钮单击和与应用程序的交互都会引发单独的事件。每个 if 语句都在确定如何处理不同类型的事件。

不应该是当用户在屏幕上点击或拖动他的手指时,确定是否制作的线或点的相同代码还应该询问线或点应该是什么颜色,而不仅仅是大小点,取决于选择的颜色按钮

在这种情况下,事件之间的状态被保存在myCanvas- 单击btnBlue将画布上的绘制颜色设置为蓝色 ( myCanvas.PaintColor(COLOR_BLUE);) 然后等待另一个事件发生。如果用户随后在画布上拖动,则会绘制一条线(最终myCanvas.DrawLine(prevX, prevY, touchX, touchY);)。由于myCanvas记住了状态,它用蓝色画了线。

于 2013-02-21T18:24:42.000 回答
1

如果用户在画布上点击或拖动手指的 if 语句处于同一级别

好吧,我可能是错的,但如果the same level你的意思是对用户的行为有某种同等权重,那么答案是肯定的。

不应该是当用户在屏幕上点击或拖动他的手指时,确定是否制作的线或点的相同代码还应该询问线或点应该是什么颜色,而不仅仅是大小点,取决于选择的颜色按钮

是的,它应该。为此,您必须进行相应的编程。

于 2013-02-21T18:08:29.920 回答
1

这是事件处理的常见构造。将“相同级别”的 IF 视为用户可以选择的选项可能会有所帮助。当您在同一个对话框上有两个按钮时,通常不会嵌套它们,因为您不知道将单击哪个按钮。

方法的名称,'dispatchEvent' 是一个很大的线索,它所做的只是接收事件并选择适当的代码路径。通常每个 IF 只是转身并调用一个函数或方法,否则调度方法可能会变得很大。

如果您对所有可能的消息都有足够大的枚举(Win32 会这样做),您可以使用“switch/case”语句而不是所有 IF 来做同样的事情。

于 2013-02-21T18:23:46.190 回答