我目前有一些工作,我可以在屏幕上水平拖動一個盒子(我想要它)。但是,當您單擊屏幕上的任意位置時,它就會起作用,並且我希望它僅在單擊該框時才能使用。我試圖用不同的方式來實現這一點,而且我已經看遍了各個地方,並仍然迷失方向。任何人都可以幫忙嗎?我也無法弄清楚位圖是如何放置的(我現在正在使用位圖,因爲我無法在我的生活中弄清楚如何在SurfaceView中實現ImageView)。如果我說我的位圖放置在0,0,那麼根據它的左上角將位圖放置在0,0?我還有一個算法,可以在盒子到達邊緣時停止,但我必須重寫,因爲我必須刪除它。請,如果你可以提供你的知識,我將不勝感激拖動ImageView或點擊位圖
public class BoardView extends SurfaceView implements SurfaceHolder.Callback{
Context mContext;
private BoardThread thread;
private float box_x = 140;
private float box_y = 378;
ImageView i = (ImageView) findViewById(R.id.box_view);
Bitmap box =
(BitmapFactory.decodeResource
(getResources(), R.drawable.box));
private float boxWidth = box.getWidth();
private float boxHeight = box.getHeight();
public BoardView(Context context){
super(context);
//surfaceHolder provides canvas that we draw on
getHolder().addCallback(this);
// controls drawings
thread = new BoardThread(getHolder(),this);
//intercepts touch events
setFocusable(true);
}
@Override
public void onDraw(Canvas canvas){
canvas.drawColor(Color.WHITE);
//draw box and set start location
canvas.drawBitmap(box, box_x - (boxWidth/2),
box_y - (boxHeight/2), null);
}
@Override
public boolean onTouchEvent(MotionEvent event){
//boolean mode = false;
if(event.getAction() == MotionEvent.ACTION_DOWN){
//int x = (int)event.getX();
//int y = (int)event.getY();
//if (x > box_x && x < box_x + 29 && y > box_y && y < box_y + 30){
//mode = true;
box_x = (int)event.getX();
//}
}
if(event.getAction() == MotionEvent.ACTION_MOVE) {
//int x = (int)event.getX();
//int y = (int)event.getY();
//if (mode == true){
box_x = (int)event.getX();
//}
}
invalidate();
return true;
}
@Override
public void surfaceChanged(SurfaceHolder holder,
int format, int width, int height){
}
@Override
public void surfaceCreated(SurfaceHolder holder){
thread.startRunning(true);
thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder){
thread.startRunning(false);
thread.stop();
}
}
我應該在ActionDown事件或ActionMove事件這樣的課嗎? – 2011-03-15 20:32:36
現在它真的很糟糕,我需要在它裏面找出一些問題。它的動作更流暢。 – 2011-03-15 21:01:35