`
ipjmc
  • 浏览: 703411 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

使用ViewFlipper和Gesture实现手势切换

阅读更多
        ViewFlipper继承FrameLayout,完全可以把它当成一种FrameLayout,只是多了一种功能,很方便的实现在不同的层中实现切换,还可以设置一些动画。废话不说了,直接上代码

    main.xml,我在ViewFlipper中设置了一个Button,但是必须取消Clickable才能让ViewFlipper捕捉到onTouch事件,原因可参考文章:http://orgcent.com/android-touch-event-mechanism/


<?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" >
    
	<Button android:id="@+id/button" android:text="Click Me"
	    android:layout_width="fill_parent" android:layout_height="wrap_content" />


<!--在ViewFlipper中定义3个控件, TextView, TextView和Button-->

    <ViewFlipper android:id="@+id/flipper" android:longClickable="true"
        android:layout_width="fill_parent" android:layout_height="0px" android:layout_weight="1">
        
        <TextView android:text="00000000000"
            android:layout_width="fill_parent" android:layout_height="fill_parent"
            android:background="@android:color/background_dark" />
        
        <TextView android:text="11111111111"
            android:layout_width="fill_parent" android:layout_height="fill_parent"
            android:background="@android:color/background_light" />
        
        <Button android:text="Click Me" android:clickable="false"
	    	android:layout_width="fill_parent" android:layout_height="fill_parent" />
    </ViewFlipper>

    <TextView android:id="@+id/footer"  android:background="#0000FF"
        android:layout_width="fill_parent" android:layout_height="100dp"/>
</LinearLayout>


    切换是用的的4个动画,直接用的是网上的,感谢原作者。 http://blog.csdn.net/yzhyutian/article/details/6236504

    1.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>

    2.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>


    3.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>


    4.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>


    ViewFlipperActivity.java,需要实现两个接口OnTouchListener, OnGestureListener,首先通过OnTouchListener识别出Touch事件,并将事件交给GestureDetector,然后OnGestureListener就能判断出是哪一种手势了。
package com.ipjmc.viewflipper;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class ViewFlipperActivity extends Activity implements OnTouchListener, OnGestureListener {
    /** Called when the activity is first created. */
	
	public static final String TAG = "ViewFlipper";
	Button mButton;
	ViewFlipper mFlipper;
	TextView mFooter;
	GestureDetector mDetector;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

		Log.i(TAG, "onCreate");
        mFlipper = (ViewFlipper) findViewById(R.id.flipper);
        mFlipper.setOnTouchListener(this); //注册OnTouch监听器
        
        //本来需要设置LongClickable,否则无法捕捉到touch事件,因为在xml中已经设置了,这里就不需要了。
        //mFlipper.setLayoutParams(true);
        
        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mFlipper.showNext();
			}
		});
        
		//mFooter只是用来做一些事件捕获的测试用的,无视好了
        mFooter = (TextView) findViewById(R.id.footer);
        mFooter.setOnTouchListener(this);
        
		//用OnGestureListener初始化一个Detector,用于识别手势,OnGestureListener的每一个方法代表识别出的每一个手势
        mDetector = new GestureDetector(this); 
    }

	@Override
	public boolean onDown(MotionEvent e) {
		// 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;
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	/*
	*识别出了手指在屏幕上的快速滑动事件
	*/
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		Log.i(TAG, "Fling Happened!");  
        if (e1.getX() - e2.getX() > 120) {  
            mFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in));  
            mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_out));  
            mFlipper.showNext();  
            return true;  
        } else if (e1.getX() - e2.getX() < -120) {  
            mFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in));  
            mFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out));  
            mFlipper.showPrevious();  
            return true;  
        }  
		return false;
	}

	/*
	 * 在OnTouchListener.onTouch(),将事件交给GestureDetector.onTouchEvent()处理,由GestureDetector识别是哪一种手势
	 * @see android.view.View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent)
	 */
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		if (v.getId() == R.id.flipper) {
			return mDetector.onTouchEvent(event); //将事件交给GestureDetector.onTouchEvent()
		}
		return false;
	}
	
	/** 
	 *Called when a touch screen event was not handled by any of the views under it. This is most useful to process touch events that happen outside of your window bounds, where there is no view to receive it.
	 *
	 *官方文档说当一个Touch事件,没有被任何子View处理时,这个方法会被调用。
	 *
	 *把它放到这里当然没有用了,只是为了防止和OnTouchListener.onTouch(View v, MotionEvent event)混淆
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		Log.i(TAG, "onTouchEvent()"); 	//触摸footer时,会打印日志
		return super.onTouchEvent(event);
	}
}


    参考文章:http://blog.csdn.net/yzhyutian/article/details/6236504
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics