Android 用手势切换activity
fmms
13年前
package rw.gest; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.LinearLayout; import android.widget.Toast; public class Gest01Activity extends Activity{ GestureDetector detector; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); detector=new GestureDetector(new GestureListener()); LinearLayout layout=(LinearLayout)findViewById(R.id.linearlayout); layout.setOnTouchListener(new TouhListener()); layout.setLongClickable(true); } //触摸屏幕监听 class TouhListener implements OnTouchListener{ @Override public boolean onTouch(View v, MotionEvent event) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "----?", event.getAction()).show(); return detector.onTouchEvent(event); } } //手势滑动监听 class GestureListener implements OnGestureListener{ @Override public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub /* * 当触摸屏幕是使用这个函数 */ Toast.makeText(getApplicationContext(), "-----------> onDown", Toast.LENGTH_LONG).show(); return false; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // e1 触摸的起始位置,e2 触摸的结束位置,velocityX X轴每一秒移动的像素速度(大概这个意思) velocityY 就是Y咯 //手势左,上为正 ——,右,下为负正 if (e2.getX()-e1.getX()>50) { //为什么是50? 这个根据你的模拟器大小来定,看看模拟器宽度,e2.getX()-e1.getX()<屏幕宽度就OK Toast.makeText(getApplicationContext(), "向右滑动", Toast.LENGTH_LONG).show(); //要触发什么事件都在这里写就OK //如果要跳转到另外一个activity Intent intent=new Intent(Gest01Activity.this, toActivity.class); startActivity(intent); } if (Math.abs(e2.getX()-e1.getX())>50) { Toast.makeText(getApplicationContext(), "向左滑动", Toast.LENGTH_LONG).show(); } if (Math.abs(e2.getY()-e1.getY())>50) { Toast.makeText(getApplicationContext(), "向上滑动", Toast.LENGTH_LONG).show(); } if (e2.getY()-e1.getY()>50) { Toast.makeText(getApplicationContext(), "向下滑动", Toast.LENGTH_LONG).show(); } return false; } @Override public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub\ /* * 长按事件 一切长事件按屏幕想要触发的事件都在这里写 */ Toast.makeText(getApplicationContext(), "------------> onLongPress", Toast.LENGTH_LONG).show(); } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub /* * 这个函数大概是这样,英语差了,有误差。distanceX 是X轴路径长度,distanceY 是Y轴路径长度(注意:是路径,不是位移); */ Toast.makeText(getApplicationContext(), "------------> onScroll", Toast.LENGTH_LONG).show(); return false; } @Override public void onShowPress(MotionEvent e) { // TODO Auto-generated method stub /* * */ Toast.makeText(getApplicationContext(), "------------> onShowPress", Toast.LENGTH_LONG).show(); } @Override public boolean onSingleTapUp(MotionEvent e) { // TODO Auto-generated method stub Toast.makeText(getApplicationContext(), "------------> onSingleTapUp", Toast.LENGTH_LONG).show(); return false; } } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/linearlayout" > <ImageView android:src="@drawable/eight" android:id="@+id/imageView1" android:layout_height="match_parent" android:layout_width="match_parent"></ImageView> </LinearLayout>
先上代码再看图,解释在代码中.............
转自:http://blog.csdn.net/rwyz1314/article/details/6684660