Android Animation之ViewAnimator\ViewFlipper

选自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,可以设置进出动画

<?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

<?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

<?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

<?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

<?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

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;
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值