繁体   English   中英

Android onTouchListener

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM