1.5.29 Android Animation 关于 viewAnimator详解

       一、结构
       public class ViewAnimator extends FrameLayout
       java.lang.Object
       android.view.View
       android.view.ViewGroup
       android.widget.FrameLayout
       android.widget.ViewAnimator

       二、概述
       FrameLayout容器的基类,当进行视图切换时显示动画效果。
(此类不常用,常用其直接子类ViewFlipper, ViewSwitcher或间接子类ImageSwitcher, TextSwitcher)

       三、XML属性
       属性名称
      描述android:inAnimation(设置View进入屏幕时候使用的动画)
      android:outAnimation(设置View离开屏幕时候使用的动画)

      四、公共方法
      public void addView (View child, int index, ViewGroup.LayoutParams params)

      添加一个具有指定布局参数子视图。
      参数
      child 添加的子视图
      index 添加的子视图所在的位置
       params 设置子视图的布局参数
       public int getBaseline ()
       返回widget的文本基线到widget上边界的偏移量,如果当前widget不支持基线对齐,此方法返回-1

       返回值
       在widget界限内基线的偏移量,如果不支持基线对齐,返回-1
       public View getCurrentView ()
       返回与当前显示的子元素相应的视图
       返回值
       当前显示的视图
       参见
       getDisplayedChild()
       public int getDisplayedChild ()
       返回当前显示的子视图的索引
       public Animation getInAnimation ()
       返回被用来显示视图进入屏幕的动画
       返回值
       一个动画对象animation,如果没有设置的话,返回一个null
       参见
       setInAnimation(android.view.animation.Animation)
       setInAnimation(android.content.Context, int)
       public Animation getOutAnimation ()
       返回被用来显示视图离开屏幕的动画
       返回值
       一个动画对象animation,如果没有设置的话,返回一个null
       参见
       setOutAnimation(android.view.animation.Animation)
       setOutAnimation(android.content.Context, int)
       public void removeAllViews ()
       调用此方法从ViewGroup中移除所有的子视图
       public void removeViewAt (int index)
       移除组(group)中指定位置上的视图
       参数
       index 需要移除的视图在组中的位置
       public void removeViewInLayout (View view)
       在布局时移除一个视图。当你需要在onLayout()中移除一个视图时,此方法很有用。
        参数
        view 要从组中移除的视图
        public void removeViews (int start, int count)
        从组中移除指定范围的视图
        参数
        start 要移除的多个视图在组中的开始位置
        count 移除视图的数量
        public void removeViewsInLayout (int start, int count)
        在布局时移除一组视图。当你需要在onLayout()中移除很多视图时,此方法很有用。
        参数
         start 移除的多个视图在组中第一个视图的索引
         count 移除视图的数量
         public void setAnimateFirstView (boolean animate)
         设置当前视图在首次加载时是否动画显示。
         参数
        animate 在第一次显示当前视图时,是否动画显示
        public void setDisplayedChild (int whichChild)
        设置哪个子视图将被显示出来
        参数
        whichChild 将要显示的子视图的索引
        public void setInAnimation (Animation inAnimation)
       设置视图进入屏幕时使用的动画。
       参数
       inAnimation 视图进入屏幕时使用的动画
       参见
       getInAnimation()
       setInAnimation(android.content.Context, int)
       public void setInAnimation (Context context, int resourceID)
       设置视图进入屏幕时使用的动画。
       参数
       context 上下文
       resourceID 动画的资源id
       参见
       getInAnimation()
       setInAnimation(android.view.animation.Animation)
        public void setOutAnimation (Animation outAnimation)
       设置视图退出屏幕时使用的动画
       参数
       outAnimation 当视图退出屏幕时开始的动画

以上转载:http://blog.163.com/promise_wg/blog/static/18912001420115247115355/

 

有个列子

选自Android 3.0 Animations Beginners Guide Bring Your Android Applications To Lefe With Stunning Animations

动画http://stackoverflow.com/questions/5151591/android-left-to-right-slide-animation

Viewpper是一个为了翻页动画而封装的小类。它使用tween动画类,并将它们扩展到xml文件。

main.xml

ViewFlipper有两个属性,android:inAnimation和android.outAnimation,可以设置进出动画

 

[html] view plaincopy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <ViewFlipper  
  8.         android:id="@+id/pages"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="0dip"  
  11.         android:layout_weight="1" >  
  12.   
  13.         <LinearLayout  
  14.             android:layout_width="match_parent"  
  15.             android:layout_height="wrap_content"  
  16.             android:orientation="vertical" >  
  17.   
  18.             <TextView  
  19.                 android:layout_width="match_parent"  
  20.                 android:layout_height="wrap_content"  
  21.                 android:text="@string/text_1_1" />  
  22.   
  23.             <ImageView  
  24.                 android:id="@+id/rollingball"  
  25.                 android:layout_width="wrap_content"  
  26.                 android:layout_height="wrap_content"  
  27.                 android:paddingLeft="60dp"  
  28.                 android:src="@drawable/ball"  
  29.                 android:contentDescription="@string/app_name" />  
  30.   
  31.             <TextView  
  32.                 android:layout_width="match_parent"  
  33.                 android:layout_height="wrap_content"  
  34.                 android:text="@string/text_1_2" />  
  35.         </LinearLayout>  
  36.   
  37.         <LinearLayout  
  38.             android:layout_width="match_parent"  
  39.             android:layout_height="wrap_content"  
  40.             android:orientation="vertical" >  
  41.   
  42.             <TextView  
  43.                 android:layout_width="match_parent"  
  44.                 android:layout_height="wrap_content"  
  45.                 android:text="@string/text_2_1" />  
  46.   
  47.             <ImageView  
  48.                 android:id="@+id/bouncingball"  
  49.                 android:layout_width="wrap_content"  
  50.                 android:layout_height="wrap_content"  
  51.                 android:paddingLeft="60dp"  
  52.                 android:src="@drawable/ball"  
  53.                 android:contentDescription="@string/app_name" />  
  54.   
  55.             <TextView  
  56.                 android:layout_width="match_parent"  
  57.                 android:layout_height="wrap_content"  
  58.                 android:text="@string/text_2_2" />  
  59.         </LinearLayout>  
  60.   
  61.         <TextView  
  62.             android:layout_width="match_parent"  
  63.             android:layout_height="wrap_content"  
  64.             android:text="@string/text_3_1" />  
  65.     </ViewFlipper>  
  66.   
  67.     <LinearLayout  
  68.         android:layout_width="match_parent"  
  69.         android:layout_height="wrap_content"  
  70.         android:gravity="center" >  
  71.   
  72.         <Button  
  73.             android:id="@+id/prev"  
  74.             style="?android:attr/buttonBarButtonStyle"  
  75.             android:layout_width="match_parent"  
  76.             android:layout_height="wrap_content"  
  77.             android:layout_weight="1"  
  78.             android:drawableLeft="@android:drawable/ic_media_previous"  
  79.             android:text="@string/button_prev" />  
  80.   
  81.         <Button  
  82.             android:id="@+id/next"  
  83.             style="?android:attr/buttonBarButtonStyle"  
  84.             android:layout_width="match_parent"  
  85.             android:layout_height="wrap_content"  
  86.             android:layout_weight="1"  
  87.             android:drawableRight="@android:drawable/ic_media_next"  
  88.             android:text="@string/button_next" />  
  89.     </LinearLayout>  
  90.   
  91. </LinearLayout>  


slidein_to_left.xml     //in from right to left   used by next button

 

 

[html] view plaincopy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:interpolator="@android:anim/decelerate_interpolator" >  
  4.   
  5.     <translate  
  6.         android:duration="500"  
  7.         android:fromXDelta="100%p"  
  8.         android:toXDelta="0" />  
  9.   
  10. </set>  


slideout_to_left.xml     //out from right to left used by next button

[html] view plaincopy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:interpolator="@android:anim/accelerate_interpolator" >  
  4.   
  5.     <translate  
  6.         android:duration="500"  
  7.         android:fromXDelta="0"  
  8.         android:toXDelta="-100%p" />  
  9.   
  10. </set>  


slidein_to_right.xml     //in from left to right   used by previous button

[html] view plaincopy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:interpolator="@android:anim/decelerate_interpolator" >  
  4.   
  5.     <translate  
  6.         android:duration="500"  
  7.         android:fromXDelta="-100%p"  
  8.         android:toXDelta="0" />  
  9.   
  10. </set>  


slideout_to_right.xml     //out from left to right   used by previous button

[html] view plaincopy

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:interpolator="@android:anim/accelerate_interpolator" >  
  4.   
  5.     <translate  
  6.         android:duration="500"  
  7.         android:fromXDelta="0"  
  8.         android:toXDelta="100%p" />  
  9.   
  10. </set>  


MainActivity.java

 

[java] view plaincopy

  1. package com.animation.interactivebook;  
  2.   
  3. import android.os.Bundle;  
  4. import android.animation.ObjectAnimator;  
  5. import android.animation.ValueAnimator;  
  6. import android.app.Activity;  
  7. import android.view.Menu;  
  8. import android.view.View;  
  9. import android.view.animation.Animation;  
  10. import android.view.animation.AnimationSet;  
  11. //import android.view.animation.AnimationUtils;  
  12. import android.view.animation.ScaleAnimation;  
  13. import android.view.animation.TranslateAnimation;  
  14. import android.widget.Button;  
  15. import android.widget.ViewAnimator;  
  16.   
  17. public class InteractiveBook extends Activity {  
  18.   
  19.     @Override  
  20.     protected void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.   
  24.         final View rollingBall = findViewById(R.id.rollingball);  
  25.         ObjectAnimator ballRoller = ObjectAnimator.ofFloat(rollingBall,  
  26.                 "TranslationX"0400);  
  27.         ballRoller.setDuration(2000);  
  28.         ballRoller.setRepeatMode(ObjectAnimator.REVERSE);  
  29.         ballRoller.setRepeatCount(ObjectAnimator.INFINITE);  
  30.         ballRoller.start();  
  31.   
  32.         final View bouncingBall = findViewById(R.id.bouncingball);  
  33.         ValueAnimator ballBouncer = ValueAnimator.ofInt(040);  
  34.         ValueAnimator.setFrameDelay(50);  
  35.         ballBouncer  
  36.                 .addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {  
  37.                     public void onAnimationUpdate(ValueAnimator ballBouncer) {  
  38.                         // We'll fill this out in a minute  
  39.                         final int animatedValue = (Integer) ballBouncer  
  40.                                 .getAnimatedValue();  
  41.                         bouncingBall.post(new Runnable() {  
  42.                             public void run() {  
  43.                                 bouncingBall.setPadding(  
  44.                                         bouncingBall.getPaddingLeft(),  
  45.                                         40 - animatedValue,  
  46.                                         bouncingBall.getPaddingRight(),  
  47.                                         animatedValue);  
  48.                                 bouncingBall.invalidate();  
  49.                             }  
  50.                         });  
  51.                     }  
  52.                 });  
  53.         ballBouncer.setDuration(2000);  
  54.         ballBouncer.setRepeatMode(ValueAnimator.REVERSE);  
  55.         ballBouncer.setRepeatCount(ValueAnimator.INFINITE);  
  56.         ballBouncer.start();  
  57.   
  58.         final AnimationSet slideinToLeft = new AnimationSet(true);  
  59.         TranslateAnimation slide1 = new TranslateAnimation(  
  60.                 Animation.RELATIVE_TO_PARENT, 1f, Animation.RELATIVE_TO_PARENT,  
  61.                 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);  
  62.         ScaleAnimation scale1 = new ScaleAnimation(101101);  
  63.         slideinToLeft.addAnimation(slide1);  
  64.         slideinToLeft.addAnimation(scale1);  
  65.         slideinToLeft.setDuration(1000);  
  66.   
  67.         final AnimationSet slideoutToLeft = new AnimationSet(true);  
  68.         TranslateAnimation slide2 = new TranslateAnimation(  
  69.                 Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT,  
  70.                 -1f, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,  
  71.                 0);  
  72.         ScaleAnimation scale2 = new ScaleAnimation(110110);  
  73.         slideoutToLeft.addAnimation(slide2);  
  74.         slideoutToLeft.addAnimation(scale2);  
  75.         slideoutToLeft.setDuration(1000);  
  76.   
  77.         final AnimationSet slideinToRight = new AnimationSet(true);  
  78.         TranslateAnimation slide3 = new TranslateAnimation(  
  79.                 Animation.RELATIVE_TO_PARENT, -1f,  
  80.                 Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_SELF, 0,  
  81.                 Animation.RELATIVE_TO_SELF, 0);  
  82.         ScaleAnimation scale3 = new ScaleAnimation(101101);  
  83.         slideinToRight.addAnimation(slide3);  
  84.         slideinToRight.addAnimation(scale3);  
  85.         slideinToRight.setDuration(1000);  
  86.   
  87.         final AnimationSet slideoutToRight = new AnimationSet(true);  
  88.         TranslateAnimation slide4 = new TranslateAnimation(  
  89.                 Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT,  
  90.                 1f, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,  
  91.                 0);  
  92.         ScaleAnimation scale4 = new ScaleAnimation(110110);  
  93.         slideoutToRight.addAnimation(slide4);  
  94.         slideoutToRight.addAnimation(scale4);  
  95.         slideoutToRight.setDuration(1000);  
  96.   
  97.         final ViewAnimator pages = (ViewAnimator) findViewById(R.id.pages);  
  98.         Button prev = (Button) findViewById(R.id.prev);  
  99.         Button next = (Button) findViewById(R.id.next);  
  100.         prev.setOnClickListener(new View.OnClickListener() {  
  101.             public void onClick(View v) {  
  102.                 // pages.clearAnimation();  
  103.                 // Animation inAnimation =  
  104.                 // AnimationUtils.loadAnimation(InteractiveBook.this,  
  105.                 // R.anim.slidein_to_right);  
  106.                 // Animation outAnimation =  
  107.                 // AnimationUtils.loadAnimation(InteractiveBook.this,  
  108.                 // R.anim.slideout_to_right);  
  109.                 // pages.setInAnimation(inAnimation);  
  110.                 // pages.setOutAnimation(outAnimation);  
  111.                 pages.clearAnimation();  
  112.                 pages.setInAnimation(slideinToRight);  
  113.                 pages.setOutAnimation(slideoutToRight);  
  114.                 pages.showPrevious();  
  115.             }  
  116.         });  
  117.         next.setOnClickListener(new View.OnClickListener() {  
  118.             public void onClick(View v) {  
  119.                 // pages.clearAnimation();  
  120.                 // Animation inAnimation =  
  121.                 // AnimationUtils.loadAnimation(InteractiveBook.this,  
  122.                 // R.anim.slidein_to_left);  
  123.                 // Animation outAnimation =  
  124.                 // AnimationUtils.loadAnimation(InteractiveBook.this,  
  125.                 // R.anim.slideout_to_left);  
  126.                 // pages.setInAnimation(inAnimation);  
  127.                 // pages.setOutAnimation(outAnimation);  
  128.                 pages.clearAnimation();  
  129.                 pages.setInAnimation(slideinToLeft);  
  130.                 pages.setOutAnimation(slideoutToLeft);  
  131.                 pages.showNext();  
  132.             }  
  133.         });  
  134.     }  
  135.   
  136.     @Override  
  137.     public boolean onCreateOptionsMenu(Menu menu) {  
  138.         // Inflate the menu; this adds items to the action bar if it is present.  
  139.         getMenuInflater().inflate(R.menu.interactive_book, menu);  
  140.         return true;  
  141.     }  
  142.   
  143. }  

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值