2

以下のコードを実行すると、すべて正常に動作します。移動できる 3 つのボールを備えたシンプルなアプリです...

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; 

        }


    }


}

しかし、メイン アクティビティから 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);
              }

        }


    }


}

理由を知っている人はいますか?

はい@bigstones onToucheventは機能しています。すべてのアクションをキャプチャします。問題は、アクティビティ内にontoucheventコードがある場合、変数X、Yが機能しないように見えることですが、値があり、印刷できます(またはログに記録できます) )私が言っていることはテストされています.if()ステートメント(getX、getY)からすべての値を整数に変更しようとしましたが、X、Yだけでは機能しませんでした.....コードをもう一度確認してくださいお願いします!ありがとう!

4

2 に答える 2

2

それを見つけた!

問題は、ある種のリスナーを登録する必要があるのはレイアウトであることがわかったので、これを行いました:

このコードを Main クラスに追加しました。

private OnClickListener previewListener = new OnClickListener() {       
    @Override
    public void onClick(View v) {
        System.err.println("I've been clicked");
    }
};

そして、この行を onCreate メソッドに追加します:

previewLayout.setOnClickListener(previewListener);

そしてそれは働いた!

于 2011-06-16T01:59:28.663 に答える
0

onTouchの外側に移動したようDrawViewです。タッチ イベントを処理しようとしているので、 を設定する必要がありますがonTouchListeneronClickListener.

于 2012-09-26T16:00:38.840 に答える