Android的SurfaceView不响应触摸事件 [英] Android SurfaceView not responding to touch events

查看:2134
本文介绍了Android的SurfaceView不响应触摸事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图得到一个简单的表面,以响应触摸事件。下面启动,但应用程序不响应触摸事件。我有一个Log.i声明确认(通过打印到控制台)的触摸事件是否正常工作。谁能告诉我什么,我做错了什么?

这是我的主要活动

 公共类MainActivity延伸活动{    公共静态INT屏幕宽度,screenHeight;
    公共静态布尔运行= TRUE;
    公共静态MainSurface mySurface;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        //这个获取屏幕的大小
        DisplayMetrics displaymetrics =新DisplayMetrics();
        。getWindowManager()getDefaultDisplay()getMetrics(displaymetrics)。
        屏幕宽度= displaymetrics.widthPixels;
        screenHeight = displaymetrics.heightPixels;
        Log.i(MainActivity,Integer.toString(屏幕宽度)++ Integer.toString(screenHeight));        mySurface =新MainSurface(本);        的setContentView(mySurface);
    }}

这是表面视图类

 公共类MainSurface扩展SurfaceView实现OnTouchListener {    公共MainSurface(上下文的背景下){
        超级(上下文);
    }    @覆盖
    公共布尔onTouch(视图V,MotionEvent事件){
        INT X =(int)的event.getX();
        INT Y =(int)的event.getY();
        INT点= event.getPointerCount();
        Log.i(MainSurface,Integer.toString(X)); //任何打印这里的控制台
        返回true;
    }}


解决方案

  1. 删除实现OnTouchListener

  2. 更改 onTouch(视图V,MotionEvent事件) onTouchEvent(MotionEvent事件)

它不工作的原因是,SurfaceView不知道它应该是它自己的OnTouchListener没有你告诉它。另外,你可以把它加入这个code你的onCreate()工作:

  mySurface =新MainSurface(本);
mySurface.setOnTouchListener(mySurface);

不过,由于SurfaceView已经有一个OnTouchEvent功能,它更简单,只是使用它。

另外,不声明你的SurfaceView为static。

I am trying to get a simple surface view to respond to touch events. The application below launches but does not respond to touch events. I have a Log.i statement to confirm (by printing to the console) whether or not the touch event is working. Can anyone tell me what I am doing wrong?

This is the my main activity

public class MainActivity extends Activity {

    public static int screenWidth, screenHeight;
    public static boolean running=true;
    public static MainSurface mySurface;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //this gets the size of the screen
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        screenWidth = displaymetrics.widthPixels;
        screenHeight = displaymetrics.heightPixels;
        Log.i("MainActivity", Integer.toString(screenWidth) + " " + Integer.toString(screenHeight));

        mySurface = new MainSurface(this);

        setContentView(mySurface);
    }

}

This is the surface view class

public class MainSurface extends SurfaceView implements OnTouchListener {

    public MainSurface(Context context) {
        super(context);
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        int x = (int)event.getX();
        int y = (int)event.getY();
        int point = event.getPointerCount();
        Log.i("MainSurface", Integer.toString(x)); //nothing prints to the console here
        return true;
    }

}

解决方案

  1. Remove implements OnTouchListener.
  2. Change onTouch(View v, MotionEvent event) to onTouchEvent(MotionEvent event).

The reason it's not working is that the SurfaceView doesn't know that it is supposed to be it's own OnTouchListener without you telling it. Alternatively, you could make it work by adding this code to your onCreate():

mySurface = new MainSurface(this);
mySurface.setOnTouchListener(mySurface);

However since SurfaceView already has an OnTouchEvent function, it's simpler to just use that.

Also, don't declare your SurfaceView as static.

这篇关于Android的SurfaceView不响应触摸事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆