zoukankan      html  css  js  c++  java
  • android开发(8) 使用ViewFlipper来用手势切换画面

    使用android手机肯定很喜欢用手指把画面拖来拖去的感觉。这样的切换画面让人非常舒服。我们来尝试做个这样的例子吧。

    本文中我们初次接触到了下面个东西:

    ViewFlipper 视图的切换容器视图,它有很多子视图,可以使用showPrevious,showNext来向前或者向后切换视图,不过是没有动画效果的

    Animation 这个才是动画

    GestureDetector 手势侦查器,他提供了手势的一些事件,它封装了一些手指在屏幕的移动方向的处理,转换成相应的事件

    我们看看实现步骤:

    1。写一个窗体,放置一个ViewFlipper 在视图里。并为ViewFlipper 添加子视图。

     

    <ViewFlipper android:layout_width="fill_parent" android:id="@+id/viewFlipper2" 
    android:layout_height
    ="fill_parent">
        
    <LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content">
            
    <TextView android:text="第一" android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
        
    </LinearLayout>
        
    <LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:layout_height="wrap_content">
            
    <TextView android:text="第二" android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
        
    </LinearLayout>
           
    <LinearLayout android:id="@+id/linearLayout3" android:layout_width="wrap_content" android:layout_height="wrap_content">
            
    <TextView android:text="第三" android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
        
    </LinearLayout>
    </ViewFlipper>

    2注册窗体的 onTouchEvent事件,这个事件会在窗体被触摸时触发。在这个事件触发后,将事件触发后的参数扔给一个GestureDetector对象来处理。

    3。准备一个GestureDetector对象,为第一步来使用。GestureDetector对象将用户的,时候触摸动作转换成相应的手势事件。这些事件有:

          onDown,onFling,onLongPress,onScroll,onShowPress,onSingleTapUp。本文我们只用到onFling手势

    4.处理onFling手势的操作。onFling事件触发时,由操作系统传入的参数有MotionEvent e1, MotionEvent e2, float velocityX,  float velocityY。 参数e1,和e2,是手势触发  的 开始位置和结束位置。就是你的手指第一次点击,和最后离开的屏幕坐标位置。我们用e1,和e2,来判断用户是从左到友移动了手指或者从友到左移动了手指。判断的方法为:

    float x1 = e1.getX();
     float x2 = e2.getX();

    if (x1 - x2 < -100) //从左往右拖动,100代表长度。

    {

       ....

    } else if (x1 - x2 > 100) {//从右往左拖动,100代表长度

     ... 

    }

    5.由于判断了手势,那么我们可以对ViewFlipper的子视图进行切换了,方法如下

        //让flipper 前移
        this.ViewFlipper1.showPrevious();

    6。如何处理动画呢?

      为flipper(ViewFlipper )指定一个animation 对象就可以了。

          Animation animation = 
                        AnimationUtils.loadAnimation(getApplicationContext(), R.anim.filp_l2r);
                    
    //指定一个动画
          this.flipper.setAnimation(animation);

    如上代码所示AnimationUtils.loadAnimation指定一个动画描述的资源文件。我们的动画效果是在这里的资源文件里描述的。

    从左往右的动画

    <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android">  
    <translate  
      
    android:fromXDelta="0%p"  
      android:toXDelta
    ="100%p"  
      android:duration
    ="1000" />  
    </set> 

    从右往左的效果

    <?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%p"  
      android:duration
    ="1000" />  
    </set> 

    fromXDelta是开始的x坐标,是相对于屏幕窗体的坐标位置。toXDelta是结束位置。duration是延迟时间。

    完整的代码下载

  • 相关阅读:
    输入一个1-9的数i,再输入一个数字n,表示 i 出现的次数,输入的2个数字 i 和 n 组合成如下表达式:如i=2,n=4,2+22+222+2222=?,计算结果是多少?
    现有数列1/2;2/3;3/5;5/8······第十次出现的是什么?
    猜数游戏:范围时1-100,若错误就提示大了还是小了,猜对则结束,允许猜10次,游戏结束后对玩家评价:1次猜对;5次内猜对;10次内猜对;没有猜对
    登录模拟,用户名和密码输入错误后给出相关错误提示,并告知还有多少次错误机会,如果5次验证失败将冻结账户
    30人围坐轮流表演节目,按顺序数1-3,每次数到3的人就表演节目,表演过的人不再参加报数,那么在仅剩一个人没有表演的时候,共报数多少人次?
    docker 自定义镜像
    php 镜像richarvey/nginx-php-fpm的ngnix配置
    php tp5常用小知识
    php Tp5下mysql的增删改查
    php 面试常问问题
  • 原文地址:https://www.cnblogs.com/vir56k/p/2093668.html
Copyright © 2011-2022 走看看