简体   繁体   中英

Java Android: onTouchListener not working

I'm new to writing Apps for Java. I managed to build a little Test-App, with a SurfaceView that's constantly redrawn in a Thread based loop.

Now I managed to work all those Graphical things out, though I wanted to implement an onTouchListener. I did as I was told by Google and wrote it into my main activity implementing OnTouchListener and setting my SurfaceView to the OnTouchListener (directly: Panel.p.setOnTouchListener and indirectly: View view = findViewById(R.id.SurfaceView01);view.setOnTouchListener(this);) .

I got results in neither case though. My App is set to 'Fullscreen' with the XML only containing a reference to the Panel.

I have no idea where the error in that could be, that everything works but the onTouchListener. I'm gonna add all the Files that are relevant to the problem here in hopes someone knows what I did wrong.

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >


<com.example.test.Panel android:id="@+id/SurfaceView01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:maxHeight="40dip">
</com.example.test.Panel>

Main Activity:

//'Constructor'
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
    //View view = findViewById(R.id.SurfaceView01);
    //view.setOnTouchListener(this);
    Panel.p.setOnTouchListener(this);
}

//Called when App's being closed
@Override
public void onPause(){
    super.onPause();
    CanvasThread._run = false;
}

//Touching - Somehow doesn't seem to want to do anything :(
@Override
public boolean onTouch (View v, MotionEvent event){
    int x = (int)event.getX();
    int y = (int)event.getY();
    CanvasThread.scene.bird.setDestination(x, y);
    //Pressing
    if(event.getAction()==MotionEvent.ACTION_DOWN){}
    //Releasing
    if(event.getAction()==MotionEvent.ACTION_UP){}
    //Moving
    if(event.getAction()==MotionEvent.ACTION_MOVE){}
    return false;
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

Panel (SurfaceView):

CanvasThread canvasthread;
public static Panel p;

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
}


@Override
public void surfaceCreated(SurfaceHolder holder) {
    canvasthread.setRunning(true);
    canvasthread.start();
}


@Override
public void surfaceDestroyed(SurfaceHolder holder) {
        boolean retry = true;
        canvasthread.setRunning(false);
        while (retry) {
                try {
                        canvasthread.join();
                        retry = false;
                } catch (InterruptedException e) {}
        }

}


public Panel(Context context, AttributeSet attrs) {
    super(context, attrs);
    getHolder().addCallback(this);
    canvasthread = new CanvasThread(getHolder(), this);
    setFocusable(true);
    p = this;
}


public void onDraw(Canvas canvas, Bitmap img, int x, int y) {
    Paint paint = new Paint();
    canvas.drawBitmap(img, x, y, null);
}

public void setBlack(Canvas canvas){
    canvas.drawColor(Color.BLACK);
}


//-----------Image Management----------------------
public Bitmap chest = BitmapFactory.decodeResource(getResources(), R.drawable.chest);
public Bitmap stairs = BitmapFactory.decodeResource(getResources(), R.drawable.stairs);
public Bitmap[][] blueBird = doubleCrop(BitmapFactory.decodeResource(getResources(), R.drawable.bluebird), 4, 2);
public Bitmap[] tileset = cropIt(BitmapFactory.decodeResource(getResources(), R.drawable.tileset), 10);

public Bitmap[] cropIt(Bitmap set, int length){
    int width = set.getWidth()/length;
    int height = set.getHeight();
    Bitmap[] array = new Bitmap[length];
    for(int c=0; c<length; c++){
        array[c] = Bitmap.createBitmap(set, c*width, 0, width, height);
    }
    return array;
}

public Bitmap[][] doubleCrop(Bitmap set, int yLength, int xLength){
    int width = set.getWidth()/xLength;
    int height = set.getHeight()/yLength;
    Bitmap[][] array = new Bitmap[yLength][xLength];
    for(int cy=0; cy<yLength; cy++){
        for(int cx=0; cx<xLength; cx++){
            array[cy][cx] = Bitmap.createBitmap(set, cx*width, cy*height, width, height);
        }
    }
    return array;
}

CanvasThread:

    _surfaceHolder = surfaceHolder;
    _panel = panel;
    scene = new Scene(_panel);
}

public void setRunning(boolean run) {
    _run = run;
}

@Override
public void run() {
    Canvas c;
    while (_run) {
        try{Thread.sleep(FRAMEWAIT);}catch(Exception ex){}
        c = null;
        try {
            c = _surfaceHolder.lockCanvas(null);
            synchronized (_surfaceHolder) {
                scene.circle(_panel, c);
            }
        } finally {
            if (c != null) {
                _surfaceHolder.unlockCanvasAndPost(c);
            }
        }
    }
}

Some advice is much appreciated.

Thank you very much already.

You can declare onTouchListener directly into your Panel's constructor:

public class Panel extends SurfaceView{
      Panel(final Context context, AttributeSet attrs){

          setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                //put your logics here
            }
          }   
      }
}

Also I think that since CanvasThread taking care only about drawings on Panel - nothing except of Panel should know about CanvasThread.

By the way: in your example you just need to set return false to return true into Activity's onTouch method. I think it should solve the problem.

@Override
public boolean onTouch (View v, MotionEvent event){
    //....
    return true; //not false
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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