OnTouchListerner无法正常工作

时间:2014-06-06 11:40:26

标签: java android ontouchlistener

package com.manish;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;

public class GFXSurface1 extends Activity implements OnTouchListener{


    MyGameSurface ourSurfaceView;
    float x,y;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        ourSurfaceView = new MyGameSurface(this);
        ourSurfaceView.setOnTouchListener(this);
        x=0;
        y=0;
        setContentView(ourSurfaceView);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        ourSurfaceView.pause();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        ourSurfaceView.resume();
    }

    @Override
    public boolean onTouch(View arg0, MotionEvent event) {
        // TODO Auto-generated method stub
        x = event.getX();
        y = event.getY();

        return true;
    }

    public class MyGameSurface1 extends SurfaceView implements Runnable {

        SurfaceHolder ourHolder;
        Thread ourThread = null;
        boolean isRunning= false;

        public MyGameSurface1(Context context) {
            // TODO Auto-generated constructor stub
            super(context);
            ourHolder = getHolder();


        }

        public void pause(){
            isRunning= false;
            while(true){
                try {
                    ourThread.join();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            }
            ourThread= null;
        }
        public void resume(){
            isRunning= true;
            ourThread = new Thread(this);
            ourThread.start();

        }
        @Override
        public void run() {
            // TODO Auto-generated method stub
         while(isRunning){
             if(!ourHolder.getSurface().isValid())
                 continue;

             Canvas canvas = ourHolder.lockCanvas();
             canvas.drawRGB(5,5,100);
             if(x != 0 && y !=  0){
                 Bitmap test = BitmapFactory.decodeResource(getResources(), R.drawable.selected);
                 canvas.drawBitmap(test, x-(test.getWidth()/2), y-(test.getHeight()/2), null);
             }
             ourHolder.unlockCanvasAndPost(canvas);

         }
        }

    }

}

OnTouchListen无效 根据代码, 如果我触摸屏幕,图像应该在那里(显然是我选择的图像),但是当我触摸手机屏幕时会有响应。 eclipse没有显示错误

3 个答案:

答案 0 :(得分:0)

在放置布局之前,您正在设置触摸屏:

ourSurfaceView = new MyGameSurface(this);
    ourSurfaceView.setOnTouchListener(this);
    x=0;
    y=0;

我猜你正在开发一款游戏,但它可能应该是在setcontentview之后。

答案 1 :(得分:0)

由于您只在onTouch()方法中获取x和y值,这就是为什么它没有响应。

尝试在run()方法

中使用onTouch()方法代码

答案 2 :(得分:0)

试试onTouch()

的代码
@Override
    public boolean onTouch(View arg0, MotionEvent event) {
        // TODO Auto-generated method stub
        x = event.getX();
        y = event.getY();

        super.onTouchEvent(event);
        return true;
    }