在android中触摸坐标后拖动图像

时间:2012-04-01 18:45:59

标签: java android touch drag move

我正在创建一个Android游戏,应该在我的触摸在onTouchEvent(MotionEvent事件)方法之后拖动图像。到目前为止,我已经设法通过点击移动它,但不是通过拖动,我怎么能这样做?请帮助我,并提前感谢!在这段代码中查看我的onTouchEvent(MotionEvent事件)方法:

package com.mysoftwaremobileapps.TriangleUnlocker;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.Message;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.TextView;
import android.widget.Toast;

public class GameScreenActivity extends SurfaceView implements SurfaceHolder.Callback
{
class ExampleThread extends Thread
{
    private ArrayList<Triangle> triangles;
    private Bitmap TriangleImage;
    private Paint black;

    private boolean running;

    private SurfaceHolder mSurfaceHolder;
    private Context mContext;
    private Context mContext1;
    private Handler mHandler;
    private Handler mHandler1;
    private TriangleUnlockerActivity mActivity;

    private long frameRate;
    private boolean loading;
    public float x;
    public float y;
    public float x1;
    public float y1;
    public MediaPlayer mp1;
    public int parachuterIndexToResetAndDelete;
    public int canvasGetWidth;
    public int livesLeftValue;

    public ExampleThread(SurfaceHolder sHolder, Context context, Handler handler)
    {
        mSurfaceHolder = sHolder;
        mHandler = handler;
        mHandler1 = handler;
        mContext = context;
        mActivity = (TriangleUnlockerActivity) context;

        triangles = new ArrayList<Triangle>();
        TriangleImage = BitmapFactory.decodeResource(getResources(), R.drawable.triangle);
        black = new Paint();
        black.setStyle(Paint.Style.FILL);
        black.setColor(Color.GRAY);

        running = true;

        // This equates to 26 frames per second.
        frameRate = (long) (1000 / 26);
        loading = true;
    }

    @Override
    public void run()
    {
        while (running)
        {
            Canvas c = null;
            try
            {
                c = mSurfaceHolder.lockCanvas();

                synchronized (mSurfaceHolder)
                {
                    long start = System.currentTimeMillis();
                    doDraw(c);
                    long diff = System.currentTimeMillis() - start;

                    if (diff < frameRate)
                        Thread.sleep(frameRate - diff);
                }
            } catch (InterruptedException e)
            {
            }
            finally
            {
                if (c != null)
                {
                    mSurfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }

    protected void doDraw(Canvas canvas)
    {
        canvas.drawRect(0, 0, canvas.getWidth(), canvas.getHeight(), black);
        canvasGetWidth = canvas.getWidth();

        //Draw
        for (int i = 0; i < triangles.size(); i++)
        {
            canvas.drawBitmap(TriangleImage, triangles.get(i).getX(), triangles.get(i).getY(), null);
        }
    }

    public boolean onTouchEvent(MotionEvent event)
    {
        if (event.getAction() != MotionEvent.ACTION_DOWN)
        Toast.makeText(getContext(), "onTouchEvent invoked. X= " + event.getX() + " Y= " + event.getY(), 15).show();
        x1 = event.getX();
        y1 = event.getY();

        for (Triangle p: triangles) {
                Toast.makeText(getContext(), "Moving the triangle after touch cordinates", 25).show();
                p.posX = event.getX();
                p.posY = event.getY();
            return false;
        }
        return loading;
    }

    public void displayTriangles()
    {
        //Parachuter nr.1
        x = 14;
        y = 28;

        Triangle p = new Triangle(x, y);
        triangles.add(p);
    }

    public void setRunning(boolean bRun)
    {
        running = bRun;
    }

    public boolean getRunning()
    {
        return running;
    }
}

/** Handle to the application context, used to e.g. fetch Drawables. */
private Context mContext;

/** Pointer to the text view to display "Paused.." etc. */
private TextView mStatusText;

/** The thread that actually draws the animation */
private ExampleThread eThread;

public GameScreenActivity(Context context)
{
    super(context);

    // register our interest in hearing about changes to our surface
    SurfaceHolder holder = getHolder();
    holder.addCallback(this);

    // create thread only; it's started in surfaceCreated()
    eThread = new ExampleThread(holder, context, new Handler()
    {
        @Override
        public void handleMessage(Message m)
        {
           // mStatusText.setVisibility(m.getData().getInt("viz"));
           // mStatusText.setText(m.getData().getString("text"));
        }
    });

    setFocusable(true);
}

@Override
public boolean onTouchEvent(MotionEvent event)
{
    return eThread.onTouchEvent(event);
}

public ExampleThread getThread()
{
    return eThread;
}

@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3)
{
    // TODO Auto-generated method stub
}

public void surfaceCreated(SurfaceHolder holder)
{
    if (eThread.getState() == Thread.State.TERMINATED)
    {
        eThread = new ExampleThread(getHolder(), getContext(), getHandler());
        eThread.start();
    }
    else
    {
        eThread.start();
    }
}

@Override
public void surfaceDestroyed(SurfaceHolder holder)
{
    boolean retry = true;
    eThread.setRunning(false);

    while (retry)
    {
        try
        {
            eThread.join();
            retry = false;
        }
        catch (InterruptedException e)
        {
        }
    }
}
}

2 个答案:

答案 0 :(得分:0)

if(event.getAction()== MotionEvent.ACTION_DOWN)         //当操作向下时,设置对象的x和y         p.posX = event.getX();         p.posY = event.getY();         }

答案 1 :(得分:0)

您可以使用GestureDetector http://developer.android.com/reference/android/view/GestureDetector.html

收听ACTION_MOVE事件