[英]Android onTouchListener
需要幫助,我正准備將android游戲作為我學校的項目。 我嘗試使用ontouchListener來碰碰痣。
主要活動
public class First_Stage extends Activity implements OnTouchListener{
private AllViews allViews;
private MoleView moleView;
private PointsView pointsView;
private TimerView timerView;
private StageView stageView;
private Mole mole;
private MoveMole moleMove;
private int points=0;
private StagePoints stagePoints;
private PointsSingelton poin;
private float x,y;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
mole=new Mole();
stageView=new StageView(this);
moleView=new MoleView(this,mole,x,y);
pointsView=new PointsView(this);
timerView=new TimerView(this, "3:33");
allViews=new AllViews(this);
allViews.setViews(stageView, moleView, pointsView, timerView);
setContentView(allViews);
allViews.setOnTouchListener((View.OnTouchListener)this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
x=event.getX();
y=event.getY();
return true;
}
所有意見班
public class AllViews extends SurfaceView implements SurfaceHolder.Callback, Runnable{
private MoleView moleView;
private PointsView pointsView;
private TimerView timerView;
private StageView mainView;
private float x,y;
private Paint test;
private First_Stage first;
Thread drawThread = new Thread(this);
SurfaceHolder holder;
public AllViews(Context context) {
super(context);
test=new Paint();
first=new First_Stage();
holder= getHolder();
holder.addCallback(this);
}
public void setViews(StageView mainView, MoleView moleView, PointsView pointsView,TimerView timerView)
{
this.mainView = mainView;
this.moleView = moleView;
this.pointsView = pointsView;
this.timerView = timerView;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mainView.onDraw(canvas);
moleView.onDraw(canvas);
pointsView.onDraw(canvas);
timerView.onDraw(canvas);
test.setColor(Color.BLUE);
canvas.drawCircle(first.getX(), first.getY(), 40, test);
}
@Override
public void run() {
Canvas c;
while (true) {
c = null;
try {
c = holder.lockCanvas(null);
synchronized (holder) {
onDraw(c);
}
} finally {
if (c != null) {
holder.unlockCanvasAndPost(c);
}
}
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
drawThread.start();
}
我也有班級來騷擾,點數,時間和舞台屏幕。 但我不知道用哪種視圖制作觸摸監聽器。 嘗試了一下,我做了一個藍色的圓圈,並嘗試用偵聽器事件中的新位置更改了它的位置,但並不缺少。
任何幫助都會很棒。 謝謝,
cfir。
onTouchListener應該在AllViews類中實現,因為如果在其他視圖中實現它,則將獲得相對於特定視圖的左/上位置的位置。
在AllViews的onDraw中,您可以繪制其余對象
您要在哪種視圖中創建onTouchListener
取決於您,但是onTouchListener
是需要實現的接口。 並且在實現后,您將需要正確的方法,在這種情況下,我認為是onTouch()
。 看一下文檔
在我看來,您需要讓AllViews
知道您從觸摸屏上收到了信息。
您可以在AllViews
創建幾個屬性
private int touchX;
private int touchY;
public void setTouchX(int x) { this.touchX = x; }
public void setTouchY(int y) { this.touchY = y; }
然后在您的onTouch
方法中,在return
語句之前添加以下行:
AllViews.setTouchX(x);
AllViews.setTouchY(y);
最后,在您的onDraw
方法中,進行更改
canvas.drawCircle(first.getX(), first.getY(), 40, test);
至
canvas.drawCircle(this.touchX, this.touchY, 40, test);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.