毕业设计,已经做好好几个Activity,通过点击上一步,下一步按钮进行Activity进行切换,做到中期发现需要手势滑动切换会更好。上网搜发现可以用viewpager啥的,没学。就用手势实现几个Activity切换吧。
package com.goushispwsz.mobilesafewsz;
import com.guoshispwsz.mobilesafewsz.R;
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 Setup1Activity extends Activity implements OnTouchListener, OnGestureListener{
GestureDetector mGestureDetector;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//进入到设置向导的第一个界面
setContentView(R.layout.setup1);
mGestureDetector = new GestureDetector(this,this);
//mGestureDetector = new GestureDetector((OnGestureListener) this);
LinearLayout Setup1ActivityLayout = (LinearLayout)findViewById(R.id.setup1);
Setup1ActivityLayout.setOnTouchListener(this);
Setup1ActivityLayout.setLongClickable(true);
}
/**
* 当点击设置向导的第一个界面中右下角按钮-下一步时所要执行的方法,
* 因为在该Button中设置有属性android:onClick=next,
* @param view
*/
public void next(View view){
Intent intent = new Intent(this,Setup2Activity.class);
startActivity(intent);
finish();
//Activity切换时播放动画。自定义一个透明度变化的动画效果,且该方法必须写在startActivity(intent)或者finish()方法之后才会生效。
//参数一:界面进入时的动画效果 , 参数二:界面出去时的动画效果。
overridePendingTransition(R.anim.alpha_in, R.anim.alpha_out);
}
@Override
public boolean onDown(MotionEvent e) {
// TODO 自动生成的方法存根
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO 自动生成的方法存根
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO 自动生成的方法存根
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO 自动生成的方法存根
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO 自动生成的方法存根
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO 自动生成的方法存根
if (e1.getX()-e2.getX() > 50
&& Math.abs(velocityX) > 0) {
Intent intent = new Intent(this,Setup2Activity.class);
startActivity(intent);
finish();
Toast.makeText(this, "向左手势", Toast.LENGTH_SHORT).show();
} else if (e2.getX()-e1.getX() > 50
&& Math.abs(velocityX) > 0) {
Toast.makeText(this, "向右手势", Toast.LENGTH_SHORT).show();
}
return false;
}
//*************
/*float minMove = 120; //最小滑动距离
float minVelocity = 0; //最小滑动速度
float beginX = e1.getX();
float endX = e2.getX();
float beginY = e1.getY();
float endY = e2.getY();
if(beginX-endX>minMove&&Math.abs(velocityX)>minVelocity){ //左滑
Toast.makeText(this,velocityX+"左滑",Toast.LENGTH_SHORT).show();
}else if(endX-beginX>minMove&&Math.abs(velocityX)>minVelocity){ //右滑
Intent intent = new Intent(this,Setup2Activity.class);
startActivity(intent);
finish();
Toast.makeText(this,velocityX+"右滑",Toast.LENGTH_SHORT).show();
}else if(beginY-endY>minMove&&Math.abs(velocityY)>minVelocity){ //上滑
Toast.makeText(this,velocityX+"上滑",Toast.LENGTH_SHORT).show();
}else if(endY-beginY>minMove&&Math.abs(velocityY)>minVelocity){ //下滑
Toast.makeText(this,velocityX+"下滑",Toast.LENGTH_SHORT).show();
}
return false;
}*/
/* public class SnsConstant {
private static final int FLING_MIN_DISTANCE = 50;
private static final int FLING_MIN_VELOCITY = 0;
public static int getFlingMinDistance() {
return FLING_MIN_DISTANCE;
}
public int getFlingMinVelocity() {
return FLING_MIN_VELOCITY;
}
} */
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO 自动生成的方法存根
return mGestureDetector.onTouchEvent(event);
//return false;
}
}
1.要继承两个东西implements OnTouchListener, OnGestureListener
2.要实例化一个GestureDetector mGestureDetector;
3.在oncreate写这些
mGestureDetector = new GestureDetector(this,this);
//mGestureDetector = new GestureDetector((OnGestureListener) this);
LinearLayout Setup1ActivityLayout = (LinearLayout)findViewById(R.id.setup1);
Setup1ActivityLayout.setOnTouchListener(this);
Setup1ActivityLayout.setLongClickable(true);
4.重写
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO 自动生成的方法存根
return mGestureDetector.onTouchEvent(event);
//return false;
}
5.从网上找了一个代码方法onfling
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO 自动生成的方法存根
if (e1.getX()-e2.getX() > 50
&& Math.abs(velocityX) > 0) {
Intent intent = new Intent(this,Setup2Activity.class);
startActivity(intent);
finish();
Toast.makeText(this, "向左手势", Toast.LENGTH_SHORT).show();
} else if (e2.getX()-e1.getX() > 50
&& Math.abs(velocityX) > 0) {
Toast.makeText(this, "向右手势", Toast.LENGTH_SHORT).show();
}
return false;
}
//部分内容选自网络,仅仅做个人笔记用。