Android手势识别ViewFlipper触摸动画
16lz
2021-01-23
Android手势识别ViewFlipper触摸动画
2010-11-01 09:46 ideasandroid ideasandroid 我要评论(0) 字号: T | T 今天给大家介绍一下如何实现Android主页面的左右拖动效果。使用ViewFlipper来将您要来回拖动的View装在一起,然后与GestureDetector手势识别类来联动,确定要显示哪个View,加上一点点动画效果即可。 标签: View Android AD: 限时报名参加“甲骨文全球大会・2010・北京”及“JavaOne和甲骨文开发者大会2010”- 索 引
- [显示]
Android View 首先来看看我们的layout,如下所示:
如上所示,在ViewFlipper中放置多个layout(接下来会在不同的layout中来回滑动),ViewFlipper在同一个页面就显示其中一个layout。 ViewFlipper中的四个layout很简单,我们就放置一张图片,如下所示:
- <linearlayoutandroidandroid:layout_height="fill_parent"android:layout_width="fill_parent"android:orientation="vertical"xmlns:android="http://schemas.android.com/apk/res/android">
- <viewflipperandroidandroid:id="@+id/flipper"android:layout_below="@+id/CockpitLayout"android:layout_height="fill_parent"android:layout_width="fill_parent">
- <includeandroid:id="@+id/firstlayout"layout="@layout/first">
- <includeandroid:id="@+id/secondlayout"layout="@layout/second">
- <includeandroid:id="@+id/thirdlayout"layout="@layout/third">
- <includeandroid:id="@+id/fourthlayout"layout="@layout/fourth">
- </include></include></include></include></viewflipper>
- </linearlayout>
接下来我们来看看Activity,我们的Activity需要实现两个接口OnGestureListener,OnTouchListener。具体的代码如下所示,代码中都有相应的注释,这里就不再详述。
- <linearlayoutandroidandroid:gravity="center_vertical"android:layout_height="fill_parent"android:layout_width="fill_parent"xmlns:android="http://schemas.android.com/apk/res/android">
- <imageviewandroidandroid:layout_height="wrap_content"android:layout_width="wrap_content"android:src="@drawable/v1">
- </imageview></linearlayout>
希望本文对您有所帮助! 【编辑推荐】
- packagecom.ideasandroid.demo;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.view.GestureDetector;
- importandroid.view.MotionEvent;
- importandroid.view.View;
- importandroid.view.GestureDetector.OnGestureListener;
- importandroid.view.View.OnTouchListener;
- importandroid.view.animation.AccelerateInterpolator;
- importandroid.view.animation.Animation;
- importandroid.view.animation.TranslateAnimation;
- importandroid.widget.ViewFlipper;
- publicclassViewFlipperDemoextendsActivityimplementsOnGestureListener,OnTouchListener{
- privateViewFlippermFlipper;
- GestureDetectormGestureDetector;
- privateintmCurrentLayoutState;
- privatestaticfinalintFLING_MIN_DISTANCE=100;
- privatestaticfinalintFLING_MIN_VELOCITY=200;
- @Override
- publicvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mFlipper=(ViewFlipper)findViewById(R.id.flipper);
- //注册一个用于手势识别的类
- mGestureDetector=newGestureDetector(this);
- //给mFlipper设置一个listener
- mFlipper.setOnTouchListener(this);
- mCurrentLayoutState=0;
- //允许长按住ViewFlipper,这样才能识别拖动等手势
- mFlipper.setLongClickable(true);
- }
- /**
- *此方法在本例中未用到,可以指定跳转到某个页面
- *@paramswitchTo
- */
- publicvoidswitchLayoutStateTo(intswitchTo){
- while(mCurrentLayoutState!=switchTo){
- if(mCurrentLayoutState>switchTo){
- mCurrentLayoutState--;
- mFlipper.setInAnimation(inFromLeftAnimation());
- mFlipper.setOutAnimation(outToRightAnimation());
- mFlipper.showPrevious();
- }else{
- mCurrentLayoutState++;
- mFlipper.setInAnimation(inFromRightAnimation());
- mFlipper.setOutAnimation(outToLeftAnimation());
- mFlipper.showNext();
- }
- }
- ;
- }
- /**
- *定义从右侧进入的动画效果
- *@return
- */
- protectedAnimationinFromRightAnimation(){
- AnimationinFromRight=newTranslateAnimation(
- 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(newAccelerateInterpolator());
- returninFromRight;
- }
- /**
- *定义从左侧退出的动画效果
- *@return
- */
- protectedAnimationoutToLeftAnimation(){
- AnimationouttoLeft=newTranslateAnimation(
- 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(newAccelerateInterpolator());
- returnouttoLeft;
- }
- /**
- *定义从左侧进入的动画效果
- *@return
- */
- protectedAnimationinFromLeftAnimation(){
- AnimationinFromLeft=newTranslateAnimation(
- 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(newAccelerateInterpolator());
- returninFromLeft;
- }
- /**
- *定义从右侧退出时的动画效果
- *@return
- */
- protectedAnimationoutToRightAnimation(){
- AnimationouttoRight=newTranslateAnimation(
- 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(newAccelerateInterpolator());
- returnouttoRight;
- }
- publicbooleanonDown(MotionEvente){
- //TODOAuto-generatedmethodstub
- returnfalse;
- }
- /*
- *用户按下触摸屏、快速移动后松开即触发这个事件
- *e1:第1个ACTION_DOWNMotionEvent
- *e2:最后一个ACTION_MOVEMotionEvent
- *velocityX:X轴上的移动速度,像素/秒
- *velocityY:Y轴上的移动速度,像素/秒
- *触发条件:
- *X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒
- */
- publicbooleanonFling(MotionEvente1,MotionEvente2,floatvelocityX,
- floatvelocityY){
- if(e1.getX()-e2.getX()>FLING_MIN_DISTANCE
- &&Math.abs(velocityX)>FLING_MIN_VELOCITY){
- //当像左侧滑动的时候
- //设置View进入屏幕时候使用的动画
- mFlipper.setInAnimation(inFromRightAnimation());
- //设置View退出屏幕时候使用的动画
- mFlipper.setOutAnimation(outToLeftAnimation());
- mFlipper.showNext();
- }elseif(e2.getX()-e1.getX()>FLING_MIN_DISTANCE
- &&Math.abs(velocityX)>FLING_MIN_VELOCITY){
- //当像右侧滑动的时候
- mFlipper.setInAnimation(inFromLeftAnimation());
- mFlipper.setOutAnimation(outToRightAnimation());
- mFlipper.showPrevious();
- }
- returnfalse;
- }
- publicvoidonLongPress(MotionEvente){
- //TODOAuto-generatedmethodstub
- }
- publicbooleanonScroll(MotionEvente1,MotionEvente2,floatdistanceX,
- floatdistanceY){
- returnfalse;
- }
- publicvoidonShowPress(MotionEvente){
- //TODOAuto-generatedmethodstub
- }
- publicbooleanonSingleTapUp(MotionEvente){
- //TODOAuto-generatedmethodstub
- returnfalse;
- }
- publicbooleanonTouch(Viewv,MotionEventevent){
- //一定要将触屏事件交给手势识别类去处理(自己处理会很麻烦的)
- returnmGestureDetector.onTouchEvent(event);
- }
- }
- 在Android开发中使用Gallery实现“多级联动”
- 在Android中实现service动态更新UI界面
- Android数据库事务浅析
- Android的UI设计与后台线程交互
- Android触屏textview及listview对比验证
0人 | 了这篇文章 |
更多相关文章
- Android中使用ViewPager制作广告栏滚屏效果
- Android之UI学习篇二:TextVeiw显示表情和跑马灯效果
- Android CountDownTimer带有动画的倒计时
- Android 自定义阴影效果详解及实例
- Android取消RecyclerView、ListView、ScrollView、HorizontalScr
- 图文详解Android属性动画
- Android Activity切换动画效果详解(附源代码)
- Android基础控件——ProgressBar自定义的介绍、动画效果实现、附
- Android 之手势识别篇-GestureDetector