0
我成功地將圖像移動到畫布上並獲得圖像的座標,但是當我將座標傳遞給下一個活動以將背景圖像與可移動圖像組合起來時,圖像的x座標不會改變,但是y協調變化並向上移動,並且變化也不是靜態的,因爲我向下移動以篩選圖像移位增加。Android - 如何在畫布上移動圖像移動並獲取圖像精確座標?
下面的代碼-----
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;
}
}
}
請發表你迄今爲止所做的。 – Raghunandan 2013-03-14 07:22:49
請幫助,我會感激 – Aryan 2013-03-14 08:07:28