文档章节

Android Animation之ViewAnimator\ViewFlipper

yolinfeng
 yolinfeng
发布于 2015/06/06 17:07
字数 824
阅读 20
收藏 0

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

}


本文转载自:http://blog.csdn.net/lihenair/article/details/18310909

yolinfeng
粉丝 12
博文 196
码字总数 11946
作品 0
珠海
架构师
私信 提问
android ViewFlipper

结构 继承关系 public class ViewFlipperextends ViewAnimator java.lang.Object android.view.View android.view.ViewGroup android.widget.FrameLayout android.widget.ViewAnimator andr......

amigos_wu
2012/07/04
0
0
Android利用ViewFlipper实现屏幕切换动画效果

1、屏幕切换指的是在同一个Activity内屏幕见的切换,最长见的情况就是在一个FrameLayout内有多个页面,比如一个系统设置页面;一个个性化设置页面。 2、介绍ViewFilpper类 ViewFlipper exte...

长平狐
2012/08/28
695
0
android TextSwitcher

public class TextSwitcher extends ViewSwitcher java.lang.Object android.view.View android.view.ViewGroup android.widget.FrameLayout android.widget.ViewAnimator android.widget.Vi......

amigos_wu
2012/07/09
0
0
android 三种实现水平向滑动方式(ViewPager、ViewFilpper、ViewF...

ViewPager ViewPager类提供了多界面切换的新效果。新效果有如下特征: [1] 当前显示一组界面中的其中一个界面。 [2] 当用户通过左右滑动界面时,当前的屏幕显示当前界面和下一个界面的一部分...

带梦想一7飞
2013/08/01
0
1
android viewFlipper 使用

android.widger.viewAnimator类继承至FrameLayout 作用:为FrameLayout里面的View切换提供动画效果。 1.setInAnimation 设置View进入屏幕时候使用动画 2.setOutAnimation 设置View退出屏幕时...

谷飞
2013/03/13
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Guava RateLimiter + AOP注解实现单机限流、统计QPS

1、基于springboot项目pom.xml添加如下依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId></dependency><d......

铁骨铮铮
22分钟前
3
0
龙芯版办公软件下载

金山wps office   rpm包:http://ftp.loongnix.org/os/loongnix/1.0/os/Packages/w/wps-office-10.8.0.6472-1.a20p1.mips64el.rpm   deb包:http://packages.deepin.com/loongson/pool/......

gugudu
28分钟前
2
0
BI报表分析和数据可视化,推荐这三个开源工具!

开源篇 一、Superset 1、技术架构:Python + Flask + React + Redux + SQLAlchemy 2、使用人群: (1)开发/分析人员做好看板,业务人员浏览看板数据 (2)业务人员可自行编辑图表,查看满足...

飓风2000
34分钟前
1
0
CountDownLatch

CountDownLatch的概念 CountDownLatch是一个同步工具类,用来协调多个线程之间的同步,或者说起到线程之间的通信(而不是用作互斥的作用)。 CountDownLatch能够使一个线程在等待另外一些线程...

少年已不再年少
43分钟前
1
0
centos7 新手阿里云服务器安装mongodb

简介 MongoDB 是一个基于分布式 文件存储的NoSQL数据库 由C++语言编写,运行稳定,性能高 旨在为 WEB 应用提供可扩展的高性能数据存储解决方案 MongoDB特点 模式自由 :可以把不同结构的文档存...

醉雨
54分钟前
3
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部