手势的学习实践

来源:互联网 发布:2014知乎热门精选 编辑:程序博客网 时间:2024/05/16 11:31



主要就是在一个ViewFlipper中,有几个图片,通过左右话动屏幕,就可以是图片左右的切换。


这个比较简单,直接上代码咯:


布局文件guestures.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >


    <ViewFlipper
        android:id="@+id/viewFilpper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >


        <ImageView
            android:id="@+id/imageV1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/guide1" />


        <ImageView
            android:id="@+id/imageV2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/guide2" />


        <ImageView
            android:id="@+id/imageV3"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:src="@drawable/guide3" />
    </ViewFlipper>


</LinearLayout>


界面类:UseGuideActivity

package com.ljz.ps.activity;


import com.ljz.ps.tools.frame.AbstractActivity;
import com.ljz.ps.tools.frame.MyActitvityManager;
import com.ljz.ps.view.R;


import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ViewFlipper;


/**
 * @description 使用向导,当用户第一次使用这个软件的时候调用
 * @version 1.0
 * @author ljz
 * @update 2012-4-29 下午6:06:08
 */
public class UseGuideActivity extends AbstractActivity implements OnTouchListener , android.view.GestureDetector.OnGestureListener
{
//容器
private ViewFlipper vFlipper;

//手势
private GestureDetector mDetector;

//用户设置编号,来判断到最后一个的时候。自动跳转到主界面
private int pageNumber;

private static final int FLING_MIN_DISTANCE = 80;
private static final int FLING_MIN_VELOCITY = 100;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        //使用使用向导的界面
        setContentView(R.layout.guestures);
        
        vFlipper = (ViewFlipper)findViewById(R.id.viewFilpper);
        //设置屏幕触摸响应
        vFlipper.setOnTouchListener(this);
        //设置长时间响应界面
        vFlipper.setLongClickable(true);
        
        //获得手势响应
        mDetector = new GestureDetector(this);
        
        //位置计数器
        pageNumber = 1;
        
        //加入ACTIVITY管理器
        MyActitvityManager.getInstance().addActitvity(this);
    }


    
    //屏幕触摸的响应,与手势相结合
public boolean onTouch(View v, MotionEvent event)
{
return mDetector.onTouchEvent(event);
}


//手势的判断
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY)
{
//右进左出
if( e1.getX() - e2.getX() > FLING_MIN_DISTANCE && Math.abs(velocityX) > FLING_MIN_VELOCITY )
{
pageNumber++;
//判断位置,当看到最后一个的时候,跳转到主界面去
if( pageNumber == 4 )
{
Intent intent = new Intent( UseGuideActivity.this,MainFrameActivity.class );
startActivity(intent);
}
else 
{
vFlipper.setInAnimation( inFromRightAnimation() );
vFlipper.setOutAnimation( outToLeftAnimation() );
vFlipper.showNext();
}

}
//右出左进
else if( e2.getX() - e1.getX() > FLING_MIN_DISTANCE &&  Math.abs(velocityY) > FLING_MIN_VELOCITY )
{
if( pageNumber == 1 )
{
return false;
}
vFlipper.setInAnimation( inFromLeftAnimation() );
vFlipper.setOutAnimation( outToRightAnimation() );
vFlipper.showPrevious();
pageNumber--;
}
return false;
}



/**
* @return
* @description 定义从右边进入的动画效果
* @version 1.0
* @author ljz
* @update 2012-5-1 下午3:43:18
* @throws
*/
protected Animation inFromRightAnimation()
{
Animation animation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT ,1.0f, 
Animation.RELATIVE_TO_PARENT ,0.0f,
Animation.RELATIVE_TO_PARENT ,0.0f,
Animation.RELATIVE_TO_PARENT ,0.0f);
animation.setDuration(500);
animation.setInterpolator( new AccelerateInterpolator() );
return animation;
}


/**
* @return
* @description 定义从左边退出的动画效果
* @version 1.0
* @author ljz
* @update 2012-5-1 下午3:44:12
* @throws
*/
protected Animation outToLeftAnimation()
{
Animation animation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,0.0f,
Animation.RELATIVE_TO_PARENT,-1.0f,
Animation.RELATIVE_TO_PARENT,0.0f,
Animation.RELATIVE_TO_PARENT,0.0f);
animation.setDuration(500);
animation.setInterpolator( new AccelerateInterpolator() );
return animation;
}


/**
* @return
* @description 定义从左边进入的动画效果
* @version 1.0
* @author ljz
* @update 2012-5-1 下午3:49:50
* @throws
*/
protected Animation inFromLeftAnimation()
{
Animation animation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,-1.0f,
Animation.RELATIVE_TO_PARENT,0.0f,
Animation.RELATIVE_TO_PARENT,0.0f,
Animation.RELATIVE_TO_PARENT,0.0f);
animation.setDuration(500);
animation.setInterpolator( new AccelerateInterpolator() );
return animation;
}

/**
* @return
* @description 定义从右边出去的动画效果
* @version 1.0
* @author ljz
* @update 2012-5-1 下午3:50:14
* @throws
*/
protected Animation outToRightAnimation()
{
Animation animation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT,0.0f,
Animation.RELATIVE_TO_PARENT,+1.0f,
Animation.RELATIVE_TO_PARENT,0.0f,
Animation.RELATIVE_TO_PARENT,0.0f);
animation.setDuration(500);
animation.setInterpolator( new AccelerateInterpolator() );
return animation;
}


public void onLongPress(MotionEvent e)
{

}


public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY)
{
return false;
}


public void onShowPress(MotionEvent e)
{
}


public boolean onSingleTapUp(MotionEvent e)
{
return false;
}




public boolean onDown(MotionEvent e)
{
return false;
}
}



原创粉丝点击