我正在寫一個紙牌應用程序。我使用畫布繪製屏幕上的所有內容。有時候一堆牌會變大並延伸到屏幕底部以下。我想讓畫布滾動以便用戶可以訪問這些卡片。android畫布滾動
主要活動
package com.maulinpathare.solitaire;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class Solitaire extends Activity{
/** Called when the activity is first created. */
private Context mContext;
private GameView mG;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext = this;
setContentView(R.layout.main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
boolean result = super.onCreateOptionsMenu(menu);
menu.add(0, 1, 0, R.string.newGame);
menu.add(0, 2, 0, R.string.exitGame);
return result;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case 1:
mG = new GameView(this);
setContentView(mG);
return true;
case 2:
finish();
return true;
}
return false;
}
}//end class
GameView類 在這裏發生的一切 我已刪除大部分的遊戲邏輯...
package com.maulinpathare.solitaire;
import java.util.Stack;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ScrollView;
public class GameView extends ScrollView implements OnTouchListener{
private Deck mD;
private boolean mInit, mDone;
private Stack<Card> mPile1, mPile2, mPile3, mPile4, mPile5, mPile6, mPile7,
mOpenPile, mEndPile1, mEndPile2, mEndPile3, mEndPile4, mTemp;
private Canvas mCanvas;
private int mPile;
private boolean mPickUp, mVictory;
private Card mCard;
private final int mSpacing = 22;
private Paint rectPaint, wonPaint1, wonPaint2;
public GameView(Context context) {
super(context);
//initialize everything needed for the game
}
@Override
protected void onDraw(Canvas canvas) {
mCanvas = canvas;
if(mInit){
initialize();
mInit = false;
}
//draw stuff...
}//end onDraw
public boolean onTouch(View v, MotionEvent event){
int eventAction = event.getAction();
int x = Math.round(event.getX());
int y = Math.round(event.getY());
switch(eventAction){
case MotionEvent.ACTION_DOWN:
//check if touch has happened on one of the card piles
break; //end case action_touch
case MotionEvent.ACTION_MOVE:
//if card touched move it
break; //end case action_move
case MotionEvent.ACTION_UP:
//check if legal move has been made
break; //end case action_up
}//end switch(event)
invalidate();
return true;
}//end onTouch
}
在哪裏,我應該如何實現屏幕滾動?我希望用戶能夠沿着他們想要滾動的方向在屏幕上滑動手指。
此這裏http://stackoverflow.com/questions/2646028/android-horizontalscrollview-within-scrollview-touch-handling柱具有重寫onInterceptTouchEvent方法的一個例子。 – andig 2011-02-16 09:07:27