0

所以我按照本教程进行了操作,代码运行良好。但是,我在理解 OnTouchListener 和 OnTouch 如何协同工作时遇到了一些麻烦。我花了很长时间浏览这个论坛、网站和文档来理解,但我仍然没有。

在这段代码中,为ourSurfaceView设置了一个OnTouchListener,然后为activity调用了onTouch?!

有人可以解释一下不同活动和视图中 OnTouchListener 和 OnTouch 的关系吗?非常感谢!

package com.games.michael.waterproofme;

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 MainActivity extends Activity implements OnTouchListener{

    MySurface ourSurfaceView;
    float x,y,sX, sY, fX, fY;
    Bitmap test, plus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ourSurfaceView = new MySurface(this);
        ourSurfaceView.setOnTouchListener(this);
        x = 0;
        y = 0;
        sX = 0;
        sY = 0;
        fX = 0;
        fY = 0;
        test = BitmapFactory.decodeResource(getResources(), R.drawable.sportsball);//draw ball
        plus = BitmapFactory.decodeResource(getResources(), R.drawable.plus);
        setContentView(ourSurfaceView);
    }

@Override
protected  void onPause() {
    super.onPause();
    ourSurfaceView.pause();
}

@Override
protected  void onResume() {
    super.onResume();
    ourSurfaceView.resume();
}

@Override
public boolean onTouch(View v, MotionEvent event) {
    x = event.getX();
    y = event.getY();

    switch(event.getAction()){
        case MotionEvent.ACTION_DOWN:
            sX = event.getX();
            sY = event.getY();
            break;
        case MotionEvent.ACTION_UP:
            fX = event.getX();
            fY = event.getY();
            break;
    }

    return true;//false = finished dont loop through. true = loop through
}

public class MySurface extends SurfaceView implements Runnable{

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

    public MySurface(Context context){
        super(context);
        ourHolder = getHolder();
        ourThread = new Thread(this);
        ourThread.start();
    }

    public void pause(){
        isRunning = false;
        while(true){
            try {
                ourThread.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            break;
        }
        ourThread = null;
    }

    public void resume(){
        isRunning = true;
        ourThread = new Thread(this);
        ourThread.start();
    }

    public void run() {
        while(isRunning){
            if(!ourHolder.getSurface().isValid()) {/
                continue;}
            Canvas canvas = ourHolder.lockCanvas();
            canvas.drawRGB(02, 02, 150);
            if (x != 0 && y != 0){
                canvas.drawBitmap(test, x-(test.getWidth()/2), y-(test.getHeight()/2), null);//bitmap, left, top, paint
            }
            if (sX != 0 && sY != 0){
                canvas.drawBitmap(plus, sX-(plus.getWidth()/2), sY-(plus.getHeight()/2), null);//bitmap, left, top, paint
            }
            if (fX != 0 && fY != 0){
                canvas.drawBitmap(plus, fX-(plus.getWidth()/2), fY-(plus.getHeight()/2), null);//bitmap, left, top, paint
            }
            ourHolder.unlockCanvasAndPost(canvas);
        }
    }

}
}
4

2 回答 2

1

OnTouchListener是接口 - 实现它的类必须覆盖它的方法。这android.view.View.OnTouchListener是一种方法:boolean onTouch(View v, MotionEvent event)

当您的触摸事件发生时,SurfaceView检查是否设置了 onTouchListener,如果是,onTouch则调用其方法

于 2015-08-13T00:03:09.213 回答
0

有人可以解释一下不同活动和视图中 OnTouchListener 和 OnTouch 的关系吗?

如果您向视图注册任何回调,则会调用视图上的触摸事件。SurfaceView 扩展了 View 类。

我们的SurfaceView.setOnTouchListener(this);

设置内容视图(我们的表面视图);

因此,您只需将ourSurfaceView设置为活动作为他们的内容视图并注册View.OnTouchListener。这意味着在ourSurfaceView实例而不是活动上调用抽象方法 onTouch。

在这段代码中,为ourSurfaceView设置了一个OnTouchListener,然后为activity调用了onTouch?!

不,它会将来自ourSurfaceView实例的任何触摸事件作为它们的内容视图委托给您的MainActivity类,因为您注册了View.OnTouchListener

只是一个简单的 Java 示例:

public class Main {

public static void main(String[] args){
    SurfaceClass surfaceClass = new SurfaceClass();
    ActivityClass activityClass = new ActivityClass();
    surfaceClass.setOnFartListener(activityClass);

    //Touch event :D
    surfaceClass.fart();
}

public static class ActivityClass implements SurfaceClass.OnFartListener{

    @Override
    public void onFart(String kindOfFart) {
        System.out.println(kindOfFart);
    }
}

public static class SurfaceClass{

    private SurfaceClass.OnFartListener onFartListener;

    public void fart(){
        if(onFartListener != null){
            onFartListener.onFart("Huge One!!");
        }
    }

    public void setOnFartListener(SurfaceClass.OnFartListener onFartListener){
        this.onFartListener = onFartListener;
    }

    public interface OnFartListener{

        void onFart(String kindOfFart);

    }
}}

请参阅下面的触摸事件:D

javac Main.java && java Main
于 2015-08-13T00:04:43.033 回答