实现滑动切换效果:ViewFlipper、OnGestureListener

例子中用到的三个主要控件:ViewFlipper、OnGestureListener、OnDoubleTapListener


GestureSlide_act.java文件

/**
 * 实现滑动切换效果:ViewFlipper、OnGestureListener
 * 
 * 
 * OnGestureListener和OnDoubleTapListener接口定义:
public interface OnGestureListener {  
                // Touch down时触发, e为down时的MotionEvent  
                boolean onDown(MotionEvent e); 
                 
                // 在Touch down之后一定时间(115ms)触发,e为down时的MotionEvent  
                void onShowPress(MotionEvent e);  
                
                // Touch up时触发,e为up时的MotionEvent  
                boolean onSingleTapUp(MotionEvent e);  
                
                // 滑动时触发,e1为down时的MotionEvent,e2为move时的MotionEvent  
                boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);  
                
                // 在Touch down之后一定时间(500ms)触发,e为down时的MotionEvent  
                void onLongPress(MotionEvent e);  
                
                // 滑动一段距离,up时触发,e1为down时的MotionEvent,e2为up时的MotionEvent  
                boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY);  
                }  
   
   
public interface OnDoubleTapListener {  
                // 完成一次单击,并确定没有二击事件后触发(300ms),e为down时的MotionEvent  
                boolean onSingleTapConfirmed(MotionEvent e);  
                
                // 第二次单击down时触发,e为第一次down时的MotionEvent  
                boolean onDoubleTap(MotionEvent e);  
                
                // 第二次单击down,move和up时都触发,e为不同时机下的MotionEvent  
                boolean onDoubleTapEvent(MotionEvent e);  
                }
                
 * 
 * 
 */

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.example.R;

public class GestureSlide_Act extends Activity implements OnGestureListener, OnDoubleTapListener {

	private ViewFlipper mViewFlipper;  
    private GestureDetector mGestureDetector; 
    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.gestureslide_act);
		
		 mViewFlipper = (ViewFlipper) findViewById(R.id.flipper);  
	        Button button1 = (Button) findViewById(R.id.Button1);     
	        button1.setOnClickListener(new View.OnClickListener() {     
	            public void onClick(View view) {  
	                mViewFlipper.showNext();     
	            }     
	        });  
	        Button button2 = (Button) findViewById(R.id.Button2);     
	        button2.setOnClickListener(new View.OnClickListener() {     
	            public void onClick(View view) {  
	                mViewFlipper.showNext();     
	            }     
	        });        
	        Button button3 = (Button) findViewById(R.id.Button3);     
	        button3.setOnClickListener(new View.OnClickListener() {     
	            public void onClick(View view) {  
	                mViewFlipper.showNext();     
	            }  
	        });  
	        
	        mGestureDetector = new GestureDetector(this);
	}

    //别忘了覆盖onTouchEvent方法  
    @Override  
    public boolean onTouchEvent(MotionEvent event) {  
        return mGestureDetector.onTouchEvent(event);  
    }  
    
    
  //以下是OnGestureListener需要实现的方法  
    public boolean onDown(MotionEvent e) {  
        return false;  
    }  
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
            float velocityY) {  
        if(e1.getX() > e2.getX()) {//向左滑动  
            mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_left_in);     
            mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_left_out);     
           mViewFlipper.showNext();     
       }else if(e1.getX() < e2.getX()) {//向右滑动  
           mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_right_in);     
           mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_right_out);     
           mViewFlipper.showPrevious();     
       }else {     
           return false;     
       }     
       return true;  
    }  
  
    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;  
    }  
    
    
    //以下是OnDoubleTapListener需要实现的方法  
    public boolean onDoubleTap(MotionEvent e) {  
           mViewFlipper.startFlipping(); //双击自动切换界面  
           Toast.makeText(GestureSlide_Act.this, "自动切换界面", Toast.LENGTH_SHORT).show();
           return true;  
    }  
    public boolean onDoubleTapEvent(MotionEvent e) {  
        return false;  
    }  
    public boolean onSingleTapConfirmed(MotionEvent e) {  
        if(mViewFlipper.isFlipping()){ //单击结束自动切换  
            mViewFlipper.stopFlipping(); 
            Toast.makeText(GestureSlide_Act.this, "结束自动切换", Toast.LENGTH_SHORT).show();
        }  
        return false;  
    }  
}

gestureslide_act.xml文件

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

    <ViewFlipper
        android:id="@+id/flipper"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:flipInterval="3000"
        android:inAnimation="@anim/push_left_in"
        android:outAnimation="@anim/push_left_out"
        android:persistentDrawingCache="animation" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <Button
                android:id="@+id/Button1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Next1" >
            </Button>

            <ImageView
                android:id="@+id/image1"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" >
            </ImageView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <Button
                android:id="@+id/Button2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Next2" >
            </Button>

            <ImageView
                android:id="@+id/image2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" >
            </ImageView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical" >

            <Button
                android:id="@+id/Button3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Next3" >
            </Button>

            <ImageView
                android:id="@+id/image3"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_launcher" >
            </ImageView>
        </LinearLayout>
    </ViewFlipper>

</LinearLayout>





参考文章:http://gundumw100.iteye.com/blog/905033

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值