I'm trying to move two views (more later on) around the screen by
touching and dragging them. Unfortunately only one of them moves when
I touch the screen. Have any ideas?

The Ball class is the class representing the View I want to move.



package test.com;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.FrameLayout;

public class test extends Activity {

        public int index = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        FrameLayout main = (FrameLayout) findViewById(R.id.main_view);

        Ball ball1 = new Ball(this,50,50,25, 0xFFFF0000);
        ball1.setId(1);
        main.addView(ball1);

        Ball ball2 = new Ball(this,75,75,25, Color.BLUE);
        ball2.setId(2);
        main.addView(ball2);
    }
}







package test.com;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

public class Ball extends View {
    private float x;
    private float y;
    private int r;
    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

    public Ball(Context context, float x, float y, int r, int color) {
        super(context);
        mPaint.setColor(color);
        this.x = x;
        this.y = y;
        this.r = r;

        this.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent e)
                        {
                                float x = e.getX();
                                                        float y = e.getY();
                                                        int eventaction = 
e.getAction
();

                                                        Ball aa = (Ball)v;

                                                        switch (eventaction)
                                                        {
                                                    case 
MotionEvent.ACTION_MOVE:
                                                   aa.setLocation(x, y);
                                                      break;

                                                         }

                                return true;
                        }
        });

    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawCircle(x, y, r, mPaint);
    }

    public void setLocation(float newX, float newY)
    {
        this.x = newX;
        this.y = newY;
        this.invalidate();
    }
}


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to