Android的SurfaceView不响应触摸事件 [英] Android SurfaceView not responding to touch events
问题描述
我试图得到一个简单的表面,以响应触摸事件。下面启动,但应用程序不响应触摸事件。我有一个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;
}}
- 删除
实现OnTouchListener
。 - 更改
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;
}
}
- Remove
implements OnTouchListener
. - Change
onTouch(View v, MotionEvent event)
toonTouchEvent(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屋!