手势检测

activity_main.xml界面代码如下
xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.g160628_20_viewfilpper.MainActivity">

    <ViewFlipper
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/vf_main_image"
        >
    ViewFlipper>

LinearLayout>
 
  
在res下创建切换效果的xml
 
  
left_in.xml代码如下:
 
  
xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fillAfter="true"
    >

    
    <translate
        android:fromXDelta="-100%p"
        android:toXDelta="0"
        >translate>

    <rotate
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotY="50%"
        android:pivotX="50%"
        >rotate>

set>
 
  
left_out.xml代码如下
 
  
xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fillAfter="true"
    >
    
    <translate
        android:fromXDelta="0"
        android:toXDelta="-100%p"
        >translate>

    <rotate
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        >rotate>

set>

right_in.xml代码如下
 
  
xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fillAfter="true"
    >

    
    <translate
        android:fromXDelta="100%p"
        android:toXDelta="0"
        >translate>

    <rotate
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotX="50%"
        android:pivotY="50%"
        >rotate>

set>

right_out.xml代码如下
 
  
xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fillAfter="true">

    
    <translate
        android:fromXDelta="0"
        android:toXDelta="100%p"
        >
    translate>
    <rotate
        android:fromDegrees="0"
        android:toDegrees="360"
        android:pivotY="50%"
        android:pivotX="50%"
        >rotate>


set>

当然重要的MainActivity.java界面代码如下:
package com.example.g160628_20_viewfilpper;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewFlipper;

public class MainActivity extends AppCompatActivity {

    private int images[]={R.drawable.s2,R.drawable.s3};
    private ViewFlipper vf_main_image;
    private GestureDetector gd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        vf_main_image = (ViewFlipper) findViewById(R.id.vf_main_image);
        for (int i = 0; i <images.length ; i++) {
            ImageView iv=new ImageView(this);
            iv.setImageResource(images[i]);
            vf_main_image.addView(iv);
        }

        //实例化手势检测器类
        gd = new GestureDetector(this, new GestureDetector.OnGestureListener() {
            @Override
            public boolean onDown(MotionEvent e) {
                Log.i("test","onDown(按下)");
                return false;
            }

            @Override
            public void onShowPress(MotionEvent e) {
                Log.i("test","onShowPress(按下,但是还未抬起)");
            }

            @Override
            public boolean onSingleTapUp(MotionEvent e) {
                Log.i("test","onSingleTapUp(轻按,按一下,立刻抬起)");
                return false;
            }

            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                Log.i("test","onScroll(滚动)");
                return false;
            }

            @Override
            public void onLongPress(MotionEvent e) {
                Log.i("test","onLongPress(长按)");
            }

            //拖动
            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                Log.i("test","onFling(拖动)");

                if(e2.getX()-e1.getX()>200){
                    Toast.makeText(MainActivity.this, "右滑,上一张", Toast.LENGTH_SHORT).show();
                    vf_main_image.showPrevious();
                    //左进右出
                    vf_main_image.setInAnimation(MainActivity.this,R.anim.left_in);
                    vf_main_image.setOutAnimation(MainActivity.this,R.anim.right_out);
                }
                if(e1.getX()-e2.getX()>200){
                    Toast.makeText(MainActivity.this, "左滑,下一张", Toast.LENGTH_SHORT).show();
                    vf_main_image.showNext();
                    //左出右进
                    vf_main_image.setInAnimation(MainActivity.this,R.anim.left_out);
                    vf_main_image.setOutAnimation(MainActivity.this,R.anim.right_in);
                }

                return false;
            }
        });

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        return gd.onTouchEvent(event);
    }
}

最后当然MainActivity.java界面中的图片你可以自己放喜欢的

 
 
 

你可能感兴趣的:(手势检测)