一、结构
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
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <ViewFlipper
- android:id="@+id/pages"
- android:layout_width="match_parent"
- android:layout_height="0dip"
- android:layout_weight="1" >
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/text_1_1" />
- <ImageView
- android:id="@+id/rollingball"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingLeft="60dp"
- android:src="@drawable/ball"
- android:contentDescription="@string/app_name" />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/text_1_2" />
- </LinearLayout>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical" >
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/text_2_1" />
- <ImageView
- android:id="@+id/bouncingball"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingLeft="60dp"
- android:src="@drawable/ball"
- android:contentDescription="@string/app_name" />
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/text_2_2" />
- </LinearLayout>
- <TextView
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="@string/text_3_1" />
- </ViewFlipper>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:gravity="center" >
- <Button
- android:id="@+id/prev"
- style="?android:attr/buttonBarButtonStyle"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:drawableLeft="@android:drawable/ic_media_previous"
- android:text="@string/button_prev" />
- <Button
- android:id="@+id/next"
- style="?android:attr/buttonBarButtonStyle"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"
- android:drawableRight="@android:drawable/ic_media_next"
- android:text="@string/button_next" />
- </LinearLayout>
- </LinearLayout>
slidein_to_left.xml //in from right to left used by next button
[html] view plaincopy
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/decelerate_interpolator" >
- <translate
- android:duration="500"
- android:fromXDelta="100%p"
- android:toXDelta="0" />
- </set>
slideout_to_left.xml //out from right to left used by next button
[html] view plaincopy
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator" >
- <translate
- android:duration="500"
- android:fromXDelta="0"
- android:toXDelta="-100%p" />
- </set>
slidein_to_right.xml //in from left to right used by previous button
[html] view plaincopy
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/decelerate_interpolator" >
- <translate
- android:duration="500"
- android:fromXDelta="-100%p"
- android:toXDelta="0" />
- </set>
slideout_to_right.xml //out from left to right used by previous button
[html] view plaincopy
- <?xml version="1.0" encoding="utf-8"?>
- <set xmlns:android="http://schemas.android.com/apk/res/android"
- android:interpolator="@android:anim/accelerate_interpolator" >
- <translate
- android:duration="500"
- android:fromXDelta="0"
- android:toXDelta="100%p" />
- </set>
MainActivity.java
[java] view plaincopy
- package com.animation.interactivebook;
- import android.os.Bundle;
- import android.animation.ObjectAnimator;
- import android.animation.ValueAnimator;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.View;
- import android.view.animation.Animation;
- import android.view.animation.AnimationSet;
- //import android.view.animation.AnimationUtils;
- import android.view.animation.ScaleAnimation;
- import android.view.animation.TranslateAnimation;
- import android.widget.Button;
- import android.widget.ViewAnimator;
- public class InteractiveBook extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- final View rollingBall = findViewById(R.id.rollingball);
- ObjectAnimator ballRoller = ObjectAnimator.ofFloat(rollingBall,
- "TranslationX", 0, 400);
- ballRoller.setDuration(2000);
- ballRoller.setRepeatMode(ObjectAnimator.REVERSE);
- ballRoller.setRepeatCount(ObjectAnimator.INFINITE);
- ballRoller.start();
- final View bouncingBall = findViewById(R.id.bouncingball);
- ValueAnimator ballBouncer = ValueAnimator.ofInt(0, 40);
- ValueAnimator.setFrameDelay(50);
- ballBouncer
- .addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
- public void onAnimationUpdate(ValueAnimator ballBouncer) {
- // We'll fill this out in a minute
- final int animatedValue = (Integer) ballBouncer
- .getAnimatedValue();
- bouncingBall.post(new Runnable() {
- public void run() {
- bouncingBall.setPadding(
- bouncingBall.getPaddingLeft(),
- 40 - animatedValue,
- bouncingBall.getPaddingRight(),
- animatedValue);
- bouncingBall.invalidate();
- }
- });
- }
- });
- ballBouncer.setDuration(2000);
- ballBouncer.setRepeatMode(ValueAnimator.REVERSE);
- ballBouncer.setRepeatCount(ValueAnimator.INFINITE);
- ballBouncer.start();
- final AnimationSet slideinToLeft = new AnimationSet(true);
- TranslateAnimation slide1 = new TranslateAnimation(
- Animation.RELATIVE_TO_PARENT, 1f, Animation.RELATIVE_TO_PARENT,
- 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0);
- ScaleAnimation scale1 = new ScaleAnimation(10, 1, 10, 1);
- slideinToLeft.addAnimation(slide1);
- slideinToLeft.addAnimation(scale1);
- slideinToLeft.setDuration(1000);
- final AnimationSet slideoutToLeft = new AnimationSet(true);
- TranslateAnimation slide2 = new TranslateAnimation(
- Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT,
- -1f, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
- 0);
- ScaleAnimation scale2 = new ScaleAnimation(1, 10, 1, 10);
- slideoutToLeft.addAnimation(slide2);
- slideoutToLeft.addAnimation(scale2);
- slideoutToLeft.setDuration(1000);
- final AnimationSet slideinToRight = new AnimationSet(true);
- TranslateAnimation slide3 = new TranslateAnimation(
- Animation.RELATIVE_TO_PARENT, -1f,
- Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_SELF, 0,
- Animation.RELATIVE_TO_SELF, 0);
- ScaleAnimation scale3 = new ScaleAnimation(10, 1, 10, 1);
- slideinToRight.addAnimation(slide3);
- slideinToRight.addAnimation(scale3);
- slideinToRight.setDuration(1000);
- final AnimationSet slideoutToRight = new AnimationSet(true);
- TranslateAnimation slide4 = new TranslateAnimation(
- Animation.RELATIVE_TO_PARENT, 0, Animation.RELATIVE_TO_PARENT,
- 1f, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF,
- 0);
- ScaleAnimation scale4 = new ScaleAnimation(1, 10, 1, 10);
- slideoutToRight.addAnimation(slide4);
- slideoutToRight.addAnimation(scale4);
- slideoutToRight.setDuration(1000);
- final ViewAnimator pages = (ViewAnimator) findViewById(R.id.pages);
- Button prev = (Button) findViewById(R.id.prev);
- Button next = (Button) findViewById(R.id.next);
- prev.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- // pages.clearAnimation();
- // Animation inAnimation =
- // AnimationUtils.loadAnimation(InteractiveBook.this,
- // R.anim.slidein_to_right);
- // Animation outAnimation =
- // AnimationUtils.loadAnimation(InteractiveBook.this,
- // R.anim.slideout_to_right);
- // pages.setInAnimation(inAnimation);
- // pages.setOutAnimation(outAnimation);
- pages.clearAnimation();
- pages.setInAnimation(slideinToRight);
- pages.setOutAnimation(slideoutToRight);
- pages.showPrevious();
- }
- });
- next.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- // pages.clearAnimation();
- // Animation inAnimation =
- // AnimationUtils.loadAnimation(InteractiveBook.this,
- // R.anim.slidein_to_left);
- // Animation outAnimation =
- // AnimationUtils.loadAnimation(InteractiveBook.this,
- // R.anim.slideout_to_left);
- // pages.setInAnimation(inAnimation);
- // pages.setOutAnimation(outAnimation);
- pages.clearAnimation();
- pages.setInAnimation(slideinToLeft);
- pages.setOutAnimation(slideoutToLeft);
- pages.showNext();
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.interactive_book, menu);
- return true;
- }
- }