Android_ViewFlipper_手势滑动

Posted zlfxy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android_ViewFlipper_手势滑动相关的知识,希望对你有一定的参考价值。

最近看了想实现滑动屏效果,找了资料,看到ViewFlipper可以实现。便找了些资料练习了一下,发现挺简单的。

1、先说下布局文件吧。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:background="@drawable/white"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
  <TextView
    android:id="@+id/myTextView0"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:textColor="@drawable/black"
    android:gravity="center_vertical|center_horizontal"
    android:text="@string/hello"/>
  
  <ViewFlipper
    android:id="@+id/myViewFlipper1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">  
    <LinearLayout
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="@drawable/white">
      <TextView
        android:id="@+id/myTextView1"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:textColor="@drawable/blue"
        android:text="@string/str_view_flipper1"/>
      <ImageView
      android:id="@+id/myImageView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/visa_512"
      />
      
    </LinearLayout>
    
    <LinearLayout
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:background="@drawable/white">
      <TextView
        android:id="@+id/myTextView2"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:textColor="@drawable/blue"
        android:text="@string/str_view_flipper2"/>
       <ImageView
      android:id="@+id/myImageView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/mastercard_512"
      />
      
    </LinearLayout>
  </ViewFlipper>
</LinearLayout>
2、主Activity为:
package irdc.ex03_27;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ViewFlipper;

public class EX03_27 extends Activity

  public static String TAG = "HIPPO_DEBUG";
  private ViewFlipper mViewFlipper01;
  private float oldTouchValue;
  //private ImageView mImageView01;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState)
  
    super.onCreate(savedInstanceState);
    getWindow().setFlags
    (
      WindowManager.LayoutParams.FLAG_FULLSCREEN,
      WindowManager.LayoutParams.FLAG_FULLSCREEN
    );
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);
    mViewFlipper01 = (ViewFlipper)findViewById(R.id.myViewFlipper1);
    
    
    /*扩展学习
    mImageView01 = (ImageView)findViewById(R.id.myImageView1);
    // 建立动画TranslateAnimation
    Animation anim = new TranslateAnimation( 10, 200, 10, 400 );
    // 动画开始到结束的执行时间 (2000 = 2 秒)
    anim.setDuration(2000);
    // 设定动画重复次数,-1表示不断重复
    anim.setRepeatCount(-1);
    // 设定ImageView动画效果
    mImageView01.setAnimation(anim);
    // 动画开始
    anim.startNow();
    */
  
  
  @Override
  public boolean onTouchEvent(MotionEvent event)
  
    // TODO Auto-generated method stub
    switch (event.getAction())
    
      case MotionEvent.ACTION_DOWN:
        oldTouchValue = event.getX();
        break;
      case MotionEvent.ACTION_UP:
        float currentX = event.getX();
        if (oldTouchValue < currentX)//手指向右滑动
        
          mViewFlipper01.setInAnimation(AnimationHelper.inFromLeftAnimation());
          mViewFlipper01.setOutAnimation(AnimationHelper.outToRightAnimation());
          mViewFlipper01.showNext();
        
        if (oldTouchValue > currentX)//手指向左滑动
        
          mViewFlipper01.setInAnimation(AnimationHelper.inFromRightAnimation());
          mViewFlipper01.setOutAnimation(AnimationHelper.outToLeftAnimation());
          mViewFlipper01.showPrevious();
        
        break;
      case MotionEvent.ACTION_MOVE:
        // TODO: Some code to make the ViewFlipper
        // act like the home screen.
        break;
      
    
    return super.onTouchEvent(event);
  

3、动画Animation类为:
package irdc.ex03_27;

import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;

public class AnimationHelper

  public static Animation inFromRightAnimation()//由右向左滑动的Set In事件
  
    Animation inFromRight = 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);
    inFromRight.setDuration(350);
    inFromRight.setInterpolator(new AccelerateInterpolator());
    return inFromRight;
  
  
  public static Animation outToLeftAnimation()//由右向左滑动的Set Out事件
  
    Animation outtoLeft = 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);
    outtoLeft.setDuration(350);
    outtoLeft.setInterpolator(new AccelerateInterpolator());
    return outtoLeft;
  
  
  // for the next movement
  public static Animation inFromLeftAnimation()//由左向右滑动的Set In事件
  
    Animation inFromLeft = 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);
    inFromLeft.setDuration(350);
    inFromLeft.setInterpolator(new AccelerateInterpolator());
    return inFromLeft;
  
  
  public static Animation outToRightAnimation()//由左向右滑动的Set Out事件
  
    Animation outtoRight = 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);
    outtoRight.setDuration(350);
    outtoRight.setInterpolator(new AccelerateInterpolator());
    return outtoRight;
  


3、效果如图:



以上是关于Android_ViewFlipper_手势滑动的主要内容,如果未能解决你的问题,请参考以下文章

ios 滑动手势 怎么判断左右滑动

关于手机端 手势滑动的方向的判断(方式一)

Android_ViewPage_手势滑动

关于移动端滑动手势

手机左右滑动屏幕切换页面是如何实现的

平移手势干扰 UISlider