【发布时间】:2015-03-11 14:29:47
【问题描述】:
我试图获得一个简单的表面视图来响应触摸事件。下面的应用程序启动但不响应触摸事件。我有一个 Log.i 语句来确认(通过打印到控制台)触摸事件是否有效。谁能告诉我我做错了什么?
这是我的主要活动
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);
}
}
这是表面视图类
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;
}
}
【问题讨论】:
标签: android surfaceview