Android-如何通过触摸移动图像移动并获取图像精确坐标?

时间:2013-03-14 06:20:41

标签: android image canvas

我成功地将图像移动到画布上并获得图像的坐标但是当我将坐标传递给下一个活动以将背景图像与可移动图像组合时,图像的x坐标不会改变,但是y坐标变化并向上移动,变化也不是静止的,因为我向下移动以筛选图像移位增加。

这是守则-----

   class ImageMove extends SurfaceView implements SurfaceHolder.Callback {


private int mActivePointerId = INVALID_POINTER_ID;
private static final int INVALID_POINTER_ID = -1;
private ScaleGestureDetector mScaleDetector;
private float mScaleFactor = 1.f;

public float mPosX;
public float mPosY;
public float mLastTouchX;
public float mLastTouchY;

//For Fetching image
private Bitmap bmp;
private Drawable image;
private byte[] byteArray;
private ByteArrayBuffer baf = null;

//For Rotation
int direction = 0;
int degree = 0;
private float centerX ;
private float centerY ;
private float newX ;
public float newY ;
private float rotateX;
private float rotateY;
private float imgX;
private float imgY;
GameThread thread;
int screenW; //Device's screen width.
int screenH; //Devices's screen height.

int initialY ;
float dY; //Image vertical speed.
int ImageW;
int ImageH;
int bgrW;
int bgrH;
int angle;
int bgrScroll;
int dBgrY; //Background scroll speed.
float acc;
Bitmap image, bgr;



//Measure frames per second.
long now;
int framesCount=0;
int framesCountAvg=0;
long framesTimer=0;
Paint fpsPaint=new Paint();

//Frame speed
long timeNow;
long timePrev = 0;
long timePrevFrame = 0;
long timeDelta;




public ImageMove(Context context) {
    super(context);
    image = BitmapFactory.decodeResource(getResources(),R.drawable.bubble); //Load a  image.
   bgr = BitmapFactory.decodeResource(getResources(),R.drawable.grd); //Load a background.

   bgrW = bgr.getWidth();
   bgrH = bgr.getHeight();

   imageW = image.getWidth();
    imageH = image.getHeight();


    mScaleDetector = new ScaleGestureDetector(context, new ScaleListener()); 
    //Set thread
    getHolder().addCallback(this);

    setFocusable(true);
}

@Override
public void onSizeChanged (int w, int h, int oldw, int oldh) {
    super.onSizeChanged(w, h, oldw, oldh);
    //This event-method provides the real dimensions of this custom view.
    screenW = w;
    screenH = h;
    System.out.println(w+"  X  "+h);
    bgr = Bitmap.createScaledBitmap(bgr, w, h, true); //Scale background to fit the screen.
    bgrW = bgr.getWidth();
    bgrH = bgr.getHeight();


}

//***************************************
//*************  TOUCH  *****************
//***************************************
@Override
public synchronized boolean onTouchEvent(MotionEvent ev) {

     mScaleDetector.onTouchEvent(ev);

        final int action = ev.getAction();

        if (action == MotionEvent.ACTION_DOWN) {
            final float x = ev.getX();
            final float y = ev.getY();

            mLastTouchX = x;
            mLastTouchY = y;
            mActivePointerId = ev.getPointerId(0); 
        }

        if (action == MotionEvent.ACTION_MOVE) {

            System.out.println("MOVE");
            final int pointerIndex = ev.findPointerIndex(mActivePointerId);
            final float x = ev.getX(pointerIndex);
            final float y = ev.getY(pointerIndex);

            // Only move if the ScaleGestureDetector isn't processing a gesture.
            if (!mScaleDetector.isInProgress()) {
                final float dx = x - mLastTouchX;
                final float dy = y - mLastTouchY;

                mPosX += dx;
                mPosY += dy;


                invalidate();
            }

            mLastTouchX = x;
            mLastTouchY = y;


        }

        if (action == MotionEvent.ACTION_UP) {
            System.out.println("UP");
            mActivePointerId = INVALID_POINTER_ID;


        }

        if (action == MotionEvent.ACTION_CANCEL) {
            System.out.println("CANCEL");
            mActivePointerId = INVALID_POINTER_ID;
        }

        if (action == MotionEvent.ACTION_POINTER_UP) {
            System.out.println("ACTN_POINTER_UP");
            final int pointerIndex = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) 
            >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
            final int pointerId = ev.getPointerId(pointerIndex);
            if (pointerId == mActivePointerId) {
                // This was our active pointer going up. Choose a new
                // active pointer and adjust accordingly.
                final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
                mLastTouchX = ev.getX(newPointerIndex);
                mLastTouchY = ev.getY(newPointerIndex);
                mActivePointerId = ev.getPointerId(newPointerIndex);
            }

        }

        return true;
}

@Override
public void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Rect fromRect1 = new Rect(0, 0, bgrW - bgrScroll, bgrH);
    Rect toRect1 = new Rect(bgrScroll, 0, bgrW, bgrH);

    Rect fromRect2 = new Rect(bgrW - bgrScroll, 0, bgrW, bgrH);
    Rect toRect2 = new Rect(0, 0, bgrScroll, bgrH);

    if (!reverseBackroundFirst) {
        canvas.drawBitmap(bgr, fromRect1, toRect1, null);
        //canvas.drawBitmap(bgrReverse, fromRect2, toRect2, null);
    }
    else{
        canvas.drawBitmap(bgr, fromRect2, toRect2, null);
        //canvas.drawBitmap(bgrReverse, fromRect1, toRect1, null);
    }



     canvas.drawText(mPosX+ "  X  "+mPosY, 40, 70, fpsPaint);
     canvas.drawText(imageW+ "  X  "+imageH, 40, 100, fpsPaint); 

     canvas.drawText(screenW+ "  X  "+screenH, 40, 150, fpsPaint);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}



@Override
public void surfaceCreated(SurfaceHolder holder) {
    thread = new GameThread(getHolder(), this);
    thread.setRunning(true);
    thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    boolean retry = true;
    thread.setRunning(false);
    while (retry) {
        try {
            thread.join();
            retry = false;
        } catch (InterruptedException e) {

        }
    }
}


class GameThread extends Thread {
    private SurfaceHolder surfaceHolder;
    private ImageMove imageView;
    private boolean run = false;

    public GameThread(SurfaceHolder surfaceHolder, ImageMove imageView) {
        this.surfaceHolder = surfaceHolder;
        this.imageView = imageView;
    }

    public void setRunning(boolean run) {
        this.run = run;
    }

    public SurfaceHolder getSurfaceHolder() {
        return surfaceHolder;
    }

    @Override
    public void run() {
        Canvas c;
        while (run) {
            c = null;

            //limit frame rate to max 60fps
            timeNow = System.currentTimeMillis();
            timeDelta = timeNow - timePrevFrame;
            if ( timeDelta < 16) {
                try {
                    Thread.sleep(16 - timeDelta);
                }
                catch(InterruptedException e) {

                }
            }
            timePrevFrame = System.currentTimeMillis();

            try {
                c = surfaceHolder.lockCanvas(null);
                synchronized (surfaceHolder) {
                   //call methods to draw and process next fame
                    gameView.onDraw(c);
                }
            } finally {
                if (c != null) {
                    surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }
    }
}



private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
    @Override
    public boolean onScale(ScaleGestureDetector detector) {
        mScaleFactor *= detector.getScaleFactor();

        // Don't let the object get too small or too large.
        mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 5.0f));

        invalidate();
        return true;
    }
}

}

1 个答案:

答案 0 :(得分:0)

我得到的答案实际上我是在没有全屏的情况下拍摄坐标而我接收的坐标实际上是根据全屏显示的