Android onTouchevent()问题

Android onTouchevent()问题,android,view,touch-event,Android,View,Touch Event,当我运行下面的代码时,一切正常,这是一个简单的应用程序,有三个球,你可以移动 public class dragndrop extends Activity { /** Called when the activity is first created. */ private ColorBall[] colorballs = new ColorBall[3]; // array that holds the balls private static final

当我运行下面的代码时,一切正常,这是一个简单的应用程序,有三个球,你可以移动

public class dragndrop extends Activity {
    /** Called when the activity is first created. */
       private ColorBall[] colorballs = new ColorBall[3]; // array that holds the balls
       private static final String TAG="MyTAG";
       DrawView myView;
       private int balID = 0; // variable to know what ball is being dragged
       int X;
       int Y;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Point point1 = new Point();
            point1.x = 50;
            point1.y = 20;
            Point point2 = new Point();
            point2.x = 100;
            point2.y = 20;
            Point point3 = new Point();
            point3.x = 150;
            point3.y = 20;


            // declare each ball with the ColorBall class
            colorballs[0] = new ColorBall(this,R.drawable.bol_groen, point1);
            colorballs[1] = new ColorBall(this,R.drawable.bol_rood, point2);
            colorballs[2] = new ColorBall(this,R.drawable.bol_blauw, point3);
            myView = new DrawView(this);
        setContentView(myView);
    }


    public class DrawView extends View {


        public DrawView(Context context) {
            super(context);
            setFocusable(true); //necessary for getting the touch events

            // setting the start point for the balls


        }

        // the method that draws the balls
        @Override protected void onDraw(Canvas canvas) {

            //draw the balls on the canvas
            for (ColorBall ball : colorballs) {
                canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);
              }

        }



       // events when touching the screen
        public boolean onTouchEvent(MotionEvent event) {
            int eventaction = event.getAction(); 

            X = (int)event.getX(); 
            Y = (int)event.getY(); 

            switch (eventaction ) { 

            case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball
                balID = 0;
                for (ColorBall ball : colorballs) {
                    Log.d(TAG,"inside action down inside for coords:"+X+" coords: "+Y);
                    Log.d(TAG,"ball coords:"+ball.getX()+" coords: "+ball.getY());

                    int x =X;
                    int y =Y;
                    Log.d(TAG,"lalalalalala"+x+" coords: "+y);



                    if (x > ball.getX() && x < ball.getX()+50 && y > ball.getY() && y < ball.getY()+50){//if (X > ball.getX() && X < ball.getX()+50 && Y > ball.getY() && Y < ball.getY()+50){
                        Log.d(TAG,"inside ball coords!!!!!!!!!!!!!!!!!!!!!!!!:"+ball.getX()+" coords: "+ball.getY());
                        balID = ball.getID();


                        break;
                    }
                  }

                 break; 


            case MotionEvent.ACTION_MOVE:   // touch drag with the ball
                // move the balls the same as the finger
                if (balID > 0) {
                    colorballs[balID-1].setX(X-25);
                    colorballs[balID-1].setY(Y-25);
                }

                break; 

            case MotionEvent.ACTION_UP: 
                // touch drop - just do things here after dropping

                 break; 
            } 
            // redraw the canvas
            myView.invalidate(); 
            return true; 

        }


    }


}
公共类dragndrop扩展活动{
/**在首次创建活动时调用*/
private ColorBall[]colorbolls=new colorboll[3];//保存球的数组
私有静态最终字符串TAG=“MyTAG”;
DrawView-myView;
private int balID=0;//用于知道正在拖动哪个球的变量
int X;
int-Y;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
点1=新点();
点1.x=50;
点1.y=20;
点2=新点();
点2.x=100;
点2.y=20;
点3=新点();
点3.x=150;
点3.y=20;
//使用ColorBall类声明每个球
colorballs[0]=新的ColorBall(this,R.drawable.bol_groen,point1);
colorballs[1]=新的ColorBall(this,R.drawable.bol_rood,point2);
colorballs[2]=新的ColorBall(这个,R.drawable.bol_blauw,第3点);
myView=新的DrawView(此);
setContentView(myView);
}
公共类DrawView扩展视图{
公共绘图视图(上下文){
超级(上下文);
setFocusable(true);//获取触摸事件所必需的
//设置球的起点
}
//画球的方法
@覆盖受保护的void onDraw(画布){
//在画布上画球
用于(彩色球:彩色球){
drawBitmap(ball.getBitmap(),ball.getX(),ball.getY(),null);
}
}
//触摸屏幕时发生的事件
公共布尔onTouchEvent(运动事件){
int eventaction=event.getAction();
X=(int)event.getX();
Y=(int)event.getY();
开关(事件操作){
case MotionEvent.ACTION_DOWN://触地,检查手指是否在球上
balID=0;
用于(彩色球:彩色球){
Log.d(标签,“内部动作向下,内部为坐标:+X+”坐标:+Y);
Log.d(标记“ball coords:+ball.getX()+”coords:+ball.getY());
int x=x;
int y=y;
Log.d(标签“lalala”+x+“coords:+y”);
如果(x>ball.getX()&&xball.getY()&&yball.getX()&&xball.getY()&&y0){
彩色球[balID-1].setX(X-25);
彩色球[balID-1].setY(Y-25);
}
打破
case MotionEvent.ACTION\u UP:
//触摸下降-下降后在这里做一些事情
打破
} 
//重新绘制画布
myView.invalidate();
返回true;
}
}
}
但是当我试图从主活动处理onTouchevent时,它不起作用,奇怪的是它不能读取简单的变量(x,y)!!! 我不明白为什么会发生这样的事,似乎只有在视线范围内,它才能使他们发红

public class dragndrop extends Activity {
    /** Called when the activity is first created. */
       private ColorBall[] colorballs = new ColorBall[3]; // array that holds the balls
       private static final String TAG="MyTAG";
       DrawView myView;
       private int balID = 0; // variable to know what ball is being dragged
       int X;
       int Y;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Point point1 = new Point();
            point1.x = 50;
            point1.y = 20;
            Point point2 = new Point();
            point2.x = 100;
            point2.y = 20;
            Point point3 = new Point();
            point3.x = 150;
            point3.y = 20;


            // declare each ball with the ColorBall class
            colorballs[0] = new ColorBall(this,R.drawable.bol_groen, point1);
            colorballs[1] = new ColorBall(this,R.drawable.bol_rood, point2);
            colorballs[2] = new ColorBall(this,R.drawable.bol_blauw, point3);
            myView = new DrawView(this);
        setContentView(myView);
    }


 // events when touching the screen
    public boolean onTouchEvent(MotionEvent event) {
        int eventaction = event.getAction(); 

        X = (int)event.getX(); 
        Y = (int)event.getY(); 

        switch (eventaction ) { 

        case MotionEvent.ACTION_DOWN: // touch down so check if the finger is on a ball
            balID = 0;
            for (ColorBall ball : colorballs) {
                Log.d(TAG,"inside action down inside for coords:"+X+" coords: "+Y);
                Log.d(TAG,"ball coords:"+ball.getX()+" coords: "+ball.getY());

                int x =X;
                int y =Y;
                Log.d(TAG,"lalalalalala"+x+" coords: "+y);



                if (x > ball.getX() && x < ball.getX()+50 && y > ball.getY() && y < ball.getY()+50){//if (X > ball.getX() && X < ball.getX()+50 && Y > ball.getY() && Y < ball.getY()+50){
            Log.d(TAG,"inside ball coords!!:"+ball.getX()+" coords: "+ball.getY());
                    balID = ball.getID();


                    break;
                }
              }

             break; 


        case MotionEvent.ACTION_MOVE:   // touch drag with the ball
            // move the balls the same as the finger
            if (balID > 0) {
                colorballs[balID-1].setX(X-25);
                colorballs[balID-1].setY(Y-25);
            }

            break; 

        case MotionEvent.ACTION_UP: 
            // touch drop - just do things here after dropping

             break; 
        } 
        // redraw the canvas
        myView.invalidate(); 
        return true; 

    }




    public class DrawView extends View {


        public DrawView(Context context) {
            super(context);
            setFocusable(true); //necessary for getting the touch events

            // setting the start point for the balls


        }

        // the method that draws the balls
        @Override protected void onDraw(Canvas canvas) {
            //canvas.drawColor(0xFFCCCCCC);     //if you want another background color       

            //draw the balls on the canvas
            for (ColorBall ball : colorballs) {
                canvas.drawBitmap(ball.getBitmap(), ball.getX(), ball.getY(), null);
              }

        }


    }


}
公共类dragndrop扩展活动{
/**在首次创建活动时调用*/
private ColorBall[]colorbolls=new colorboll[3];//保存球的数组
私有静态最终字符串TAG=“MyTAG”;
DrawView-myView;
private int balID=0;//用于知道正在拖动哪个球的变量
int X;
int-Y;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
点1=新点();
点1.x=50;
点1.y=20;
点2=新点();
点2.x=100;
点2.y=20;
点3=新点();
点3.x=150;
点3.y=20;
//使用ColorBall类声明每个球
colorballs[0]=新的ColorBall(this,R.drawable.bol_groen,point1);
colorballs[1]=新的ColorBall(this,R.drawable.bol_rood,point2);
colorballs[2]=新的ColorBall(这个,R.drawable.bol_blauw,第3点);
myView=新的DrawView(此);
setContentView(myView);
}
//触摸屏幕时发生的事件
公共布尔onTouchEvent(运动事件){
int eventaction=event.getAction();
X=(int)event.getX();
Y=(int)event.getY();
开关(事件操作){
case MotionEvent.ACTION_DOWN://触地,检查手指是否在球上
balID=0;
用于(彩色球:彩色球){
Log.d(标签,“内部动作向下,内部为坐标:+X+”坐标:+Y);
Log.d(标记“ball coords:+ball.getX()+”coords:+ball.getY());
int x=x;
int y=y;
Log.d(标签“lalala”+x+“coords:+y”);
如果(x>ball.getX()&&xball.getY()&&yball.getX()&&xball.getY()&&yprivate OnClickListener previewListener = new OnClickListener() {       
    @Override
    public void onClick(View v) {
        System.err.println("I've been clicked");
    }
};
previewLayout.setOnClickListener(previewListener);