android는 좌우로 미끄러지는 제스처를 이용하여 avtivity의 점프를 실현한다

10879 단어 android 학습
졸업 디자인은 이미 여러 개의Activity를 완성했다. 이전 단계, 다음 단추를 누르면Activity를 전환한다. 중기에 제스처 슬라이딩 전환이 필요하다는 것을 발견하면 더욱 좋다.인터넷에 검색해보니viewpager 같은 걸 쓸 수 있어서 배우지 못했어요.제스처로 액티비티 전환 몇 개만 해주세요.
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; 
    }

//일부 내용은 인터넷에서 골라 개인 필기용으로만 한다.

좋은 웹페이지 즐겨찾기