滑动切换Activity有动画效果,我也知道添加一个滑动的监听,切换的时候加动作。但是我新手入门请问滑动的监听代码怎么写?还有动作怎么实现,在XML里边写呢,还是用代码好点。有没有具体动作样式的例子。求高手,求一些动作代码

解决方案 »

  1.   

    package com.emsandroid_1;import android.app.Activity;
    import android.os.Bundle;
    import android.util.DisplayMetrics;
    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.TextView;
    import android.widget.Toast;public  class EmsAndriod_main_Activity extends Activity implements OnTouchListener, OnGestureListener   {
        
    GestureDetector mGestureDetector;

    /** Called when the activity is first created. */
        
    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            mGestureDetector = new GestureDetector((OnGestureListener) this);     
            LinearLayout viewSnsLayout = (LinearLayout)findViewById(R.id.textView1);     
            viewSnsLayout.setOnTouchListener(this);     
            viewSnsLayout.setLongClickable(true);
            
        }
    private int verticalMinDistance = 20;   
    private int minVelocity         = 0;   
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {   

        if (e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {   
      
            // 切换Activity   
            // Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);   
            // startActivity(intent);   
            Toast.makeText(this, "向左手势", Toast.LENGTH_SHORT).show();   
        } else if (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {   
      
            // 切换Activity   
            // Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);   
            // startActivity(intent);   
           Toast.makeText(this, "向右手势", Toast.LENGTH_SHORT).show();   
        }   
      
        return false;   
    }
    @Override
    public boolean onTouch(View v, MotionEvent event) {   
       return mGestureDetector.onTouchEvent(event);   
    }
        @Override
    public boolean onDown(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
    }
    @Override
    public void onLongPress(MotionEvent e) {
    // TODO Auto-generated method stub

    }
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
    float distanceY) {
    // TODO Auto-generated method stub
      
      
        return false;

    }
    @Override
    public void onShowPress(MotionEvent e) {
    // TODO Auto-generated method stub

    }
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
    // TODO Auto-generated method stub
    return false;
    }
    }
    这段代码哪里有错误 ~ 怎么没有过来看看呢 
      

  2.   

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  float velocityY) {  if (e1.getX() - e2.getX() > 120) {//如果是从右向左滑动  //注册flipper的进出效果  this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.left_in));  this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.left_out));  this.flipper.showNext();  return true;
      } else if (e1.getX() - e2.getX() < -120) {//如果是从左向右滑动  this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.right_in));  this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.right_out));  this.flipper.showPrevious();  return true;
      }