«

Android实现:手指触摸滑动切换Activity

时间:2024-3-2 17:56     作者:韩俊     分类: Android


安卓编码实现触摸滑动切换Activity!

实现该操作主要用到:Intent类、onTouchEvent方法;

在Activity中重写onTouchEvent方法;方法中调用Intent类对象进行两个Activity之间的切换;

切换过程用到的方法主要是overridePendingTransition();

部分代码:

public class MainActivity extends Activity {


private TextView tv ;
private VelocityTracker velocityTracker;//用于得到手势在屏幕上的滑动速度
private static final int VELOCITY = 600;
GestureDetector mGestureDetector;
@SuppressLint("ShowToast") protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView)findViewById(R.id.textView1);
tv.setOnTouchListener(new View.OnTouchListener() {

public boolean onTouch(View v, MotionEvent e) {
tv.setText("song");
return false;
}
});
Toast.makeText(this, "Hey Guy", Toast.LENGTH_SHORT).show();
}
public boolean onTouchEvent(MotionEvent event){
int action = event.getAction();//获取事件操作
String localClassName = getLocalClassName();//当前所在类名
switch(action){
case MotionEvent.ACTION_DOWN:
if(velocityTracker == null){
velocityTracker = VelocityTracker.obtain();//取得手势在屏幕上的滑动速度
velocityTracker.addMovement(event);
}
break;
case MotionEvent.ACTION_MOVE:
//int deltaX = (int) (lastMotionX - x);
if(velocityTracker != null){
velocityTracker.addMovement(event);
}
Intent intent = new Intent();
intent.setClass(MainActivity.this, SecondActivity.class);
startActivity(intent);
overridePendingTransition(R.anim.in_from_right,R.anim.out_to_left);
//lastMotionX = x;
break;


全部代码不知道怎么上传~~QAQ

标签: android

热门推荐