如何编辑TouchEvent

时间:2014-03-09 13:25:03

标签: android canvas touch motion

我不知道我应该如何编辑球速度为每秒2的球,因为球的速度为空。

简而言之:当球速度为0时,球的速度应该在2秒时失去,当经过1秒时,经过4秒时经过2秒,在6点经过3秒时到目前为止。

第二个问题是,当我在模拟器上启动它时,球没有运行,我是怎么写的。

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.text.format.Time;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;

public class BouncingBallView extends View {
 private Ball ball;
 private Box box;
 private StatusMessage statusMsg;

 // For touch inputs - previous touch (x, y)
 private float previousX;
 private float previousY;

 // Constructor
 public BouncingBallView(Context context) {
    super(context);

    box = new Box(0xff00003f);  // ARGB
    ball = new Ball(Color.GREEN);
    statusMsg = new StatusMessage(Color.CYAN);

    // To enable keypad
    this.setFocusable(true);
    this.requestFocus();
    // To enable touch mode
    this.setFocusableInTouchMode(true);
 }

 // Called back to draw the view. Also called after invalidate().
 @Override
 protected void onDraw(Canvas canvas) {
    // Draw the components
    box.draw(canvas);
    ball.draw(canvas);
    statusMsg.draw(canvas);

    // Update the position of the ball, including collision detection and reaction.
    ball.moveWithCollisionDetection(box);
    statusMsg.update(ball);

    // Delay
    try {  
       Thread.sleep(30);  
    } catch (InterruptedException e) { }

    invalidate();  // Force a re-draw
 }

 // Called back when the view is first created or its size changes.
 @Override
 public void onSizeChanged(int w, int h, int oldW, int oldH) {
    // Set the movement bounds for the ball
    box.set(0, 0, w, h);
 }

 // Key-up event handler
 @Override
 public boolean onKeyUp(int keyCode, KeyEvent event) {
    switch (keyCode) {
       case KeyEvent.KEYCODE_DPAD_RIGHT: // Increase rightward speed
          ball.speedX++;
          break;
       case KeyEvent.KEYCODE_DPAD_LEFT:  // Increase leftward speed
          ball.speedX--;
          break;
       case KeyEvent.KEYCODE_DPAD_UP:    // Increase upward speed
          ball.speedY--;
          break;
       case KeyEvent.KEYCODE_DPAD_DOWN:  // Increase downward speed
          ball.speedY++;
          break;
       case KeyEvent.KEYCODE_DPAD_CENTER: // Stop
          ball.speedX = 0;
          ball.speedY = 0;
          break;
       case KeyEvent.KEYCODE_A:    // Zoom in
          // Max radius is about 90% of half of the smaller dimension
          float maxRadius = (box.xMax > box.yMax) ? box.yMax / 2 * 0.9f  : box.xMax / 2 * 0.9f;
          if (ball.radius < maxRadius) {
             ball.radius *= 1.05;   // Increase radius by 5%
          }
          break;
       case KeyEvent.KEYCODE_Z:    // Zoom out
          if (ball.radius > 20) {  // Minimum radius
             ball.radius *= 0.95;  // Decrease radius by 5%
          }
          break;
    }
    return true;  // Event handled
 }

 boolean touchCounter = false;

 @Override
  public boolean onTouchEvent(MotionEvent event) {
     float currentX = event.getX();
     float currentY = event.getY();
     float deltaX, deltaY;
     float scalingFactor = 1.0f / ((box.xMax > box.yMax) ? box.yMax : box.xMax);

     if (touchCounter == false) {

         switch (event.getAction()) {
         case MotionEvent.ACTION_DOWN:

              touchCounter = true;
    if (currentX > previousX) {
    deltaX = currentX - previousX;
    ball.speedX += deltaX * scalingFactor;

        }
    else if (previousX > currentX){
    deltaX = previousX - currentX;
    ball.speedX += deltaX * scalingFactor;

    }
    if (currentY > previousY) {
    deltaY = currentY - previousY;
    ball.speedX += deltaY * scalingFactor;

        }
    else if (previousY > currentY){
    deltaY = previousY - currentY;

        ball.speedY += deltaY * scalingFactor;
    }

    deltaX = currentX - previousX; //vorherig(previous)  - aktuell
    deltaY = currentY - previousY;
    ball.speedX += deltaX * scalingFactor;
    ball.speedY += deltaY * scalingFactor;
               //vorherig(previous)  - aktuell            

              break;              
         case MotionEvent.ACTION_UP:


              touchCounter = true;

              // Modify rotational angles according to movement
              break;

         }
     }

     // Save current x, y
     previousX = currentX;
     previousY = currentY;
     return true;  // Event handled
  }
}

1 个答案:

答案 0 :(得分:0)