Android使用ViewFlipper和GestrueDetector共同实现滑屏效果实例
作者:Jacob-wj
这篇文章主要介绍了Android使用ViewFlipper和GestrueDetector共同实现滑屏效果,结合完整实例形式分析了ViewFlipper和GestrueDetector控件实现滑屏功能的布局与相关操作技巧,需要的朋友可以参考下
本文实例讲述了Android使用ViewFlipper和GestrueDetector共同实现滑屏效果。分享给大家供大家参考,具体如下:
关于GestureDetector的相关知识,前面已经介绍过了,不懂的大家可以去了解一下。
1.main.xml
<?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:background="@drawable/bg" android:orientation="vertical" > <ViewFlipper android:id="@+id/viewFlipper1" android:layout_width="fill_parent" android:layout_height="fill_parent" android:persistentDrawingCache="animation" android:flipInterval="1000" android:inAnimation="@anim/push_left_in" android:outAnimation="@anim/push_left_out" > <include android:id="@+id/lay1" layout="@layout/layout1"/> <include android:id="@+id/lay2" layout="@layout/layout2"/> </ViewFlipper> </LinearLayout>
注:layout1和layout2 布局很简单,就是有一个textview和一个button,就不在这里写出了。其中包含了两个特效xml文件,放在res/anim下
1.push_left_in
<?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.0" android:toAlpha="1.0" android:duration="500" /> </set>
2 push_left_out
<?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.0" android:duration="500" /> </set>
主类:
package com.wj.gesture; 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.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.animation.AccelerateInterpolator; import android.view.animation.Animation; import android.view.animation.TranslateAnimation; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import android.widget.ViewFlipper; public class GestureDetectorTest extends Activity implements OnClickListener, OnTouchListener,OnGestureListener,OnDoubleTapListener{ GestureDetector gestureDetector; private Button next = null,up = null; private ViewFlipper flipper = null; private static final int FLING_MIN_DISTANCE = 100; private static final int FLING_MIN_VELOCITY = 200; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initview(); setListener(); } private void initview(){ gestureDetector = new GestureDetector(this); next = (Button)this.findViewById(R.id.button1); up = (Button)this.findViewById(R.id.button2); next.setOnClickListener(this); up.setOnClickListener(this); flipper = (ViewFlipper)this.findViewById(R.id.viewFlipper1); flipper.setLongClickable(true); } private void setListener(){ flipper.setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { //Toast.makeText(this, "ontouch", 1000).show(); return gestureDetector.onTouchEvent(event); } @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) { 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) { if (e1.getX()-e2.getX()>FLING_MIN_DISTANCE&&Math.abs(velocityX)>FLING_MIN_VELOCITY) { flipper.setInAnimation(inFromRightAnimation()); flipper.setOutAnimation(outToLeftAnimation()); flipper.showNext(); }else if (e2.getX()-e1.getX()>FLING_MIN_DISTANCE&&Math.abs(velocityX) > FLING_MIN_VELOCITY) { flipper.setInAnimation(inFromLeftAnimation()); flipper.setOutAnimation(outToRightAnimation()); flipper.showPrevious(); } return false; } protected Animation inFromRightAnimation() { 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(500); inFromRight.setInterpolator(new AccelerateInterpolator()); return inFromRight; } protected Animation outToLeftAnimation() { 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(500); outtoLeft.setInterpolator(new AccelerateInterpolator()); return outtoLeft; } protected Animation inFromLeftAnimation() { 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(500); inFromLeft.setInterpolator(new AccelerateInterpolator()); return inFromLeft; } protected Animation outToRightAnimation() { 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(500); outtoRight.setInterpolator(new AccelerateInterpolator()); return outtoRight; } @Override public boolean onSingleTapConfirmed(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public boolean onDoubleTap(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public boolean onDoubleTapEvent(MotionEvent e) { // TODO Auto-generated method stub return false; } @Override public void onClick(View v) { if (v==next) { flipper.showNext(); }else{ flipper.showPrevious(); } } }
更多关于Android相关内容感兴趣的读者可查看本站专题:《Android开发入门与进阶教程》、《Android多媒体操作技巧汇总(音频,视频,录音等)》、《Android基本组件用法总结》、《Android视图View技巧总结》、《Android布局layout技巧总结》及《Android控件用法总结》
希望本文所述对大家Android程序设计有所帮助。
您可能感兴趣的文章:
- Android组件banner实现左右滑屏效果
- Android Moveview滑屏移动视图类完整实例
- Android利用ViewPager实现滑动广告板实例源码
- Android 开发之BottomBar+ViewPager+Fragment实现炫酷的底部导航效果
- Android ViewPager相册横向移动的实现方法
- Android 使用viewpager实现无限循环(定时+手动)
- Android App中使用ViewPager+Fragment实现滑动切换效果
- Android App中ViewPager与Fragment结合的一些问题解决
- Android App中ViewPager所带来的滑动冲突问题解决方法
- Android ViewPager无限循环实现底部小圆点动态滑动
- Android基础之使用Fragment控制切换多个页面
- Android基础之Fragment与Activity交互详解
- Android基于ViewPager+Fragment实现左右滑屏效果的方法