现在的位置: 首页 > 综合 > 正文

坑爹的ViewFlipper,花了我好几个小时

2014年03月19日 ⁄ 综合 ⁄ 共 4484字 ⁄ 字号 评论关闭

ViewFlipper类是用来手势屏幕切换效果的,屏幕切换指的是在同一个Activity内屏幕见的切换,最长见的情况就是在一个FrameLayout内有多个页面,比如一个系统设置页面;一个个性化设置页面。

主程序在onCreat()处让程序全屏幕运行,以显示类似于全屏幕滑动的面貌。而关键程序在已重写的onTouchEvent()事件,扑捉MotionEvent.ACTION_DOWN事件;将按下时的x坐标记录为起点坐标,接下来扑捉MotionEvent.ACTION_UP事件;将手离开屏幕记录为末坐标,然后判断两者之间的位移,再通过动画的方式显示画面的滑动效果。

java代码如下:

package com.xixi;

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 ViewFlipperActivity extends Activity {
    /** Called when the activity is first created. */
	private ViewFlipper viewFlipper;
	private Float float1;
	private Float current;
    @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);
        viewFlipper=(ViewFlipper) findViewById(R.id.myViewFlipper1);
    }
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			float1=event.getX();
			break;
		case MotionEvent.ACTION_UP:
			current=event.getX();
			/*手指向右滑动*/
			if(float1<current){
				viewFlipper.setInAnimation(AnimationHelper.infromleft());
				viewFlipper.setOutAnimation(AnimationHelper.outtoright());
				/*坑爹啊,忘记下面一句了*/
				viewFlipper.showNext();
			}
			/*手指向左滑动*/
			if(float1>current){
				viewFlipper.setInAnimation(AnimationHelper.infromright());
				viewFlipper.setOutAnimation(AnimationHelper.outtoleft());
				/*坑爹啊,忘记下面一句了*/
				viewFlipper.showPrevious();
			}
			break;
		}
		return super.onTouchEvent(event);
	}
    
}

还有一个AnimationHelper类,在类中创建的AnimationTranslateAnimation对象,设置在多短的时间内完成滑动以及每格画间的延迟效果。

package com.xixi;

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

public class AnimationHelper {
	public static Animation infromleft(){
		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 outtoright(){
		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;
	}
	public static Animation outtoleft(){
		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;
	}
	public static Animation infromright(){
		Animation infronright=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);
		infronright.setDuration(350);
		infronright.setInterpolator(new AccelerateInterpolator());
		return infronright;
	}
}

main.xml文件如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  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: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"
      >
      <TextView
        android:id="@+id/myTextView1"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:text="11111111"/>
      <ImageView
      android:id="@+id/myImageView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/img1"
      />
      
    </LinearLayout>
    
    <LinearLayout
      android:orientation="vertical"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      >
      <TextView
        android:id="@+id/myTextView2"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"
        android:text="222222222222"/>
       <ImageView
      android:id="@+id/myImageView2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/img2"
      />
      
    </LinearLayout>
  </ViewFlipper>
</LinearLayout>

抱歉!评论已关闭.