首页 > 代码库 > android-GestureDetector 滑屏手势方式实现
android-GestureDetector 滑屏手势方式实现
今天做的项目中,需要使用滑屏来调出一个界面,经过自己的尝试,结合网上的方法,成功实现了。
代码如下
package com.example.text;import android.app.Activity;import android.os.Bundle;import android.view.GestureDetector;import android.view.GestureDetector.OnGestureListener;import android.view.Menu;import android.view.MenuItem;import android.view.MotionEvent;import android.widget.TextView;public class MainActivity extends Activity implements OnGestureListener { private GestureDetector mGestureDetector; private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv=(TextView) findViewById(R.id.tv);//这个如果直接写一个this,会提示过时。另外,如果两个参数都写成this,会报错。原因不知
mGestureDetector = new GestureDetector(MainActivity.this, this); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub //通过event自动判断手势,回调相应的方法 return mGestureDetector.onTouchEvent(event); }
//按下屏幕触发,打印的是坐标
@Override public boolean onDown(MotionEvent arg0) { // TODO Auto-generated method stub tv.setText(arg0.getX()+" "+arg0.getY()); return false; }
//滑动屏幕的时候触发,打印的分别是起点跟终点的坐标
@Override public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) { // TODO Auto-generated method stub tv.setText("arg1.getX()="+arg1.getX()+" arg0.getX()="+arg1.getY()+" arg2="+arg2+" arg3="+arg3); return true; } @Override public void onLongPress(MotionEvent arg0) { // TODO Auto-generated method stub } @Override public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) { // TODO Auto-generated method stub return false; } @Override public void onShowPress(MotionEvent arg0) { // TODO Auto-generated method stub } @Override public boolean onSingleTapUp(MotionEvent arg0) { // TODO Auto-generated method stub return false; }}
这个类的使用步骤如下
1.定义手势类:private GestureDetector mGestureDetector;
2.初始化类:这个是跟其他控件设置监听一样的,有多种方法
1)匿名内部类:
mGestureDetector = new GestureDetector(MainActivity.this, new OnGestureListener() { @Override public boolean onSingleTapUp(MotionEvent arg0) { // TODO Auto-generated method stub return false; } @Override public void onShowPress(MotionEvent arg0) { // TODO Auto-generated method stub } @Override public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) { // TODO Auto-generated method stub return false; } @Override public void onLongPress(MotionEvent arg0) { // TODO Auto-generated method stub } @Override public boolean onFling(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) { // TODO Auto-generated method stub return false; } @Override public boolean onDown(MotionEvent arg0) { // TODO Auto-generated method stub return false; } });
2).直接让主类继承:
public class MainActivity extends Activity implements OnGestureListener{}
3).内部类
class MyOnGestureListener implements OnGestureListener{}
3.在onTouchEvent()事件中使用这个类
@Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub //通过event自动判断手势,回调相应的方法 return mGestureDetector.onTouchEvent(event); }
不过,现在还有一点不是很明白,就是方法中的return false跟return true有什么区别
android-GestureDetector 滑屏手势方式实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。