关于滑动效果,在Android中用得比较多,本示例实现的滑动效果是使用ViewFlipper来实现的,当然也可以使用其它的View来实现。接下来就让我们开始实现这种效果。为了方便大家理解,我们先来看一下效果图:

 

  主要效果图如下图:

 

    

 

  接下来我们看一下程序结构图:

 

  

 

  MainActivity文件中代码:

复制代码
package com.android.flip;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;

/**
 * Android实现左右滑动效果
 * @Description: Android实现左右滑动效果

 * @File: MainActivity.java

 * @Package com.android.flip

 * @Author Hanyonglu

 * @Date 2012-02-12 上午10:44:04

 * @Version V1.0
 */
public class MainActivity extends Activity implements OnGestureListener {
    private ViewFlipper flipper;
    private GestureDetector detector;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        detector = new GestureDetector(this);
        flipper = (ViewFlipper) this.findViewById(R.id.ViewFlipper1);

        flipper.addView(addImageView(R.drawable.one));
        flipper.addView(addImageView(R.drawable.two));
        flipper.addView(addImageView(R.drawable.three));
        flipper.addView(addImageView(R.drawable.four));
        flipper.addView(addImageView(R.drawable.five));
    }
    
    private View addImageView(int id) {
        ImageView iv = new ImageView(this);
        iv.setImageResource(id);
        return iv;
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // TODO Auto-generated method stub
        return this.detector.onTouchEvent(event);
    }
    
    @Override
    public boolean onDown(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }
    
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        if (e1.getX() - e2.getX() > 120) {
            this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));
            this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));
            this.flipper.showNext();
            return true;
        } else if (e1.getX() - e2.getX() < -120) {
            this.flipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));
            this.flipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));
            this.flipper.showPrevious();
            return true;
        }
        
        return false;
    }
    
    @Override
    public void onLongPress(MotionEvent e) {
        // TODO Auto-generated method stub
        
    }
    
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        // TODO Auto-generated method stub
        return false;
    }
    
    @Override
    public void onShowPress(MotionEvent e) {
        // TODO Auto-generated method stub
        
    }
    
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        // TODO Auto-generated method stub
        return false;
    }
}
复制代码

 

  布局界面相对比较简单,我们只需要加入ViewFlipper就可以,代码如下: 

复制代码
<?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"
    
>

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

</LinearLayout> 

复制代码

 

  为了使其滑动时有一定的特效,我们需要加入Animation效果,说到Animation,我们先看下如何在Android中实现自定义Animation。自定义的Animation是以XML格式定义的,定义好的XML文件存放在res/anim中。

 

  一般的Animation有以下四种类型:

  1. Alpha:渐变透明度动画效果

  2. Scale:渐变尺寸伸缩动画效果

  3. Translate:画面转换位置移动动画效果

  4. Rotate:画面转换位置移动动画效果

 

  push_left_in.xml文件中代码:

复制代码
<?xml version="1.0" encoding="utf-8"?>  

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="100%p" android:toXDelta="0"
        android:duration="500" />
    <alpha android:fromAlpha="0.1" android:toAlpha="1.0"
        android:duration="500" />
</set>

复制代码

 

 

  push_left_out.xml文件中代码: 

复制代码
 <?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="-100%p"
        android:duration
="500" />
    <alpha android:fromAlpha="1.0" android:toAlpha="0.1"
        android:duration
="500" />

</set>  

复制代码

 

  push_right_in.xml文件中代码:

复制代码
<?xml version="1.0" encoding="utf-8"?> 

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="-100%p" android:toXDelta="0"
        android:duration="500" />
    <alpha android:fromAlpha="0.1" android:toAlpha="1.0"
        android:duration="500" />
</set>

复制代码

 

   push_right_out.xml文件中代码:

复制代码
<?xml version="1.0" encoding="utf-8"?> 

<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="100%p"
        android:duration="500" />
    <alpha android:fromAlpha="1.0" android:toAlpha="0.1"
        android:duration="500" />

</set>  

复制代码

 

  如果大家想实现带指引效果的左右滑动,请访问博文:http://www.cnblogs.com/hanyonglu/archive/2012/04/07/2435589.html 

 

  如果想实现带渐显按钮的左右滑动效果,请访问博文:http://www.cnblogs.com/hanyonglu/archive/2012/02/13/2350171.html

 

  下载地址:点击下载

 

  最后,希望转载的朋友能够尊重作者的劳动成果,加上转载地址:http://www.cnblogs.com/hanyonglu/archive/2012/02/13/2349827.html 谢谢。 

  

  结束。^_^

posted on 2012-10-04 17:25  aparche  阅读(446)  评论(0编辑  收藏  举报