`
苹果超人
  • 浏览: 198378 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Animations(一)

阅读更多
  Animation是个抽象类,他有五个子类。分别为:AlphaAnimation, AnimationSet, RotateAnimation, ScaleAnimation, TranslateAnimation 。
  1. AlphaAnimation 淡入淡出效果
  2. RotateAnimation 旋转效果
  3. ScaleAnimation 缩放效果
  4. TranslateAnimation 移动效果
  5. AnimationSet animation集合,里面可以放多个animation。
下面直接实例,相关参数说明都在代码中:
package com.kevin.animations;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class AnimationsDemo extends Activity {
	private ImageView img;
	private Button btn_alpha,btn_scale,btn_traslate,btn_rotate;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        img = (ImageView) findViewById(R.id.imageView1);
        btn_alpha = (Button) findViewById(R.id.btn_alpha);
        btn_rotate = (Button) findViewById(R.id.btn_rotate);
        btn_scale = (Button) findViewById(R.id.btn_scale);
        btn_traslate = (Button) findViewById(R.id.btn_translate);
        
        btn_alpha.setOnClickListener(new AlphaButtonListener());
        btn_rotate.setOnClickListener(new RotateButtonListener());
        btn_scale.setOnClickListener(new ScaleButtonListener());
        btn_traslate.setOnClickListener(new TranslateButtonListener());
    }
    
    // 淡入淡出效果
    class AlphaButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			// 创建AnimationSet对象
			AnimationSet animationSet = new AnimationSet(true);
			// 创建AlphaAnimation对象
			AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);
			// 设置动画持续时间
			alphaAnimation.setDuration(2000);
			// 将AlphaAnimation对象添加到AnimationSet当中
			animationSet.addAnimation(alphaAnimation);
			// 使用ImageView的startAnimation方法开始执行动画
			img.startAnimation(animationSet);
		}   	
    }
    // 旋转效果
    class RotateButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			AnimationSet animationSet = new AnimationSet(true);
			/*
			 * fromDegrees 为0,起始旋转的角度
			 * toDegrees 为360,最终旋转到的角度
			 * pivotXType 为动画在X轴相对于物件位置类型(三种1.absolute 绝对坐标, 2. RELATIVE_TO_PARENT相对父控件 3.RELATIVE_TO_SELF相对于自身)
			 * pivotXValue 1f(百分比)代表整个x轴的值,0f代表X轴的原点
			 * pivotYType,pivotYValue与上面类似
			 * pivotXType,pivotXValue,pivotYType,pivotYValue四个参数确定旋转的圆心
			 */
			RotateAnimation rotateAnimation = new RotateAnimation(0, 320, 
											Animation.RELATIVE_TO_PARENT, 1f,
											Animation.RELATIVE_TO_PARENT,0f);
			rotateAnimation.setDuration(5000);
			animationSet.setFillAfter(true);
			animationSet.setFillBefore(false);
			animationSet.addAnimation(rotateAnimation);
			img.startAnimation(animationSet);
		}   	
    }
    // 缩放效果
    class ScaleButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			AnimationSet animationSet = new AnimationSet(true);
			/*
			 * fromX为X大小
			 * toX为缩放后的X大小(>1放大,<1缩小,=1维持不变)
			 * fromY为Y轴大小
			 * toY为缩放后的Y大小
			 * pivotXType为动画在X轴相对于物件位置类型(三种1.absolute 绝对坐标, 2. RELATIVE_TO_PARENT相对父控件 3.RELATIVE_TO_SELF相对于自身)
			 * pivotXValue 1f代表整个x轴的值,0f代表X轴的原点
			 * pivotYType,pivotYValue与上面类似
			 * pivotXType,pivotXValue,pivotYType,pivotYValue四个参数确定轴心
			 */
			ScaleAnimation scaleAnimation = new ScaleAnimation(1, 2.0f, 1, 2.0f, 
					                      Animation.RELATIVE_TO_SELF, 0.5f, 
					                      Animation.RELATIVE_TO_SELF, 0.5f);
			scaleAnimation.setDuration(2000);
			animationSet.addAnimation(scaleAnimation);
			// 让img停留在最终的位置,而不恢复到初始位置
			animationSet.setFillAfter(true);
			animationSet.setFillBefore(false);
			img.startAnimation(animationSet);
		}   	
    }
    // 移动效果
    class TranslateButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			AnimationSet animationSet = new AnimationSet(true);
			/*
			 * 第一个参数为动画在X轴相对于物件位置类型
			 * fromXValue为动画起始时 X坐标上的移动位置 
			 * toXValue为动画结束时 X坐标上的移动位置  (最终的X的位置)
			 * 下面的Y类似
			 */
			TranslateAnimation translateAnimation = new TranslateAnimation(
					           Animation.RELATIVE_TO_SELF, 0f, 
					           Animation.RELATIVE_TO_SELF, 0f, 
					           Animation.RELATIVE_TO_SELF, 0f, 
					           Animation.RELATIVE_TO_SELF, -1f);
			translateAnimation.setDuration(5000);
			animationSet.addAnimation(translateAnimation);
			img.startAnimation(animationSet);
			System.out.println(img.getWidth() + " " + img.getHeight());
		}   	
    }
}
分享到:
评论

相关推荐

    常用动画效果 Animations

    总结来说,"常用动画效果 Animations"是一个涵盖广泛的主题,涉及到不同平台和框架下的动画实现技巧。通过学习和实践这两种常见的动画实现方法,开发者可以更好地掌握动画设计,为用户带来更加生动、流畅的应用体验...

    14Arrow&Animations;&1.0.rar

    标题 "14Arrow&Animations;&1.0.rar" 指示的是一个包含14种3D箭头样式和动画的资源包,版本为1.0。这个资源包是为Unity3D游戏引擎设计的,这可以从描述和标签中得到确认。Unity3D是一款广泛应用于游戏开发、虚拟现实...

    十大热门Android开源项目之 Material-Animations

    Material-Animations是专门为Android平台设计的一款开源项目,它致力于实现Material Design规范中的各种动画效果,使得开发者可以轻松地在自己的应用中添加生动、流畅的过渡动画。 项目的核心目标是提供Activity...

    Animations 动画展示

    在iOS开发中,Animations(动画)是至关重要的一个部分,它们不仅能够提升用户体验,还能让应用界面更加生动有趣。本文将深入探讨“Animations 动画展示”,特别关注轮播广告的优化以及常用动画界面的实现。 一、...

    Mega Animations Pack.unitypackage

    "Mega Animations Pack.unitypackage"是一款专为Unity设计的人形动画资源包,它提供了丰富的基本动作,帮助开发者快速构建生动的角色行为。 这个包的核心在于其“基本动作”,这意味着它包含了角色在游戏世界中进行...

    sass.animations:一堆 sass 动画、过渡和转换混合

    sass.animations 一堆 sass 动画混合安装通过凉亭$ bower install aglobof-sass.animations --save-dev 通过 npm $ npm install aglobof-sass.animations --save-dev用法在你的根 sass 文件中包含一个部分。...

    iOS_Animations_by_Tutorials_v3.0

    综上所述,“iOS_animations_by_tutorials_v3.0”是一本专注于iOS 10和Swift语言的动画教程书籍,由经验丰富的开发人员和作者Marin Todorov主笔,旨在帮助开发者学习如何使用最新的技术在iOS平台上实现创新和吸引人...

    一组 Animations动画

    在提供的压缩包文件“Animations”中,可能包含了实现“水平立方翻转”效果的示例代码、图片或者其他相关资源。通过查看和学习这些文件,开发者可以深入理解这一动画效果的实现方式,并将其应用到自己的项目中,为...

    Combat animations - Kung fu V1 v1.0

    标题“Combat animations - Kung fu V1 v1.0”揭示了一个专门针对功夫和武术设计的3D动画集合。这个版本1.0的动画库,包含了39个精心制作的第三人称功夫动作,旨在为玩家提供更加真实且沉浸式的体验。 这些动画不...

    iOS Animations by Tutorials v4.0 (Swift4)

    《iOS Animations by Tutorials v4.0》是专门针对Swift4编程语言设计的一本教程,旨在帮助开发者深入了解和掌握iOS平台上的动画技术。这本书详细介绍了如何利用Swift4来创建各种吸引人的、交互性强的动画效果,提升...

    iOS.Animations.by.Tutorials.v4.0.1

    《iOS Animations by Tutorials》第四版是由Marin Todorov编著的一本关于iOS动画开发的专业教程书籍。该书由Razeware LLC出版,并在2017年正式发布。版权所有者明确表示,未经书面许可,任何部分均不得以任何形式...

    动画animations

    综上所述,这个“动画animations”Demo是学习和实践Android动画的一个宝贵资源,涵盖了基本动画类型及其XML和Java实现方式,对于开发者来说,掌握这些知识将有助于提高应用的交互性和吸引力。通过深入理解和实践这些...

    iOS_Animations_by_Tutorials_v4.0内含所有章节的代码资源

    《iOS_Animations_by_Tutorials_v4.0内含所有章节的代码资源》是Ray Wenderlich出版的一本针对iOS动画的教程书籍,该书已经更新到了第四版,使用的是Swift4语言和Xcode9开发环境。书中涵盖了丰富的iOS动画技术,通过...

    Crowd Animations 是 GPU Instancer 的扩展工具,没有它就无法正常运作。

    Crowd Animations 是一个开箱即用的解决方案,支持在高性能场景中使用大量的动画角色。CA (Crowd Animations) 采用 GPU Instancer 核心功能,并在 GPUI 间接实例化解决方案和 GPU 剔除技术的基础上增加了 GPU 蒙皮...

    Android-animations

    "Android-animations"是一个专为Android系统设计的动画库,它包含了丰富的动画效果,能够帮助开发者轻松实现各种复杂的界面转换和元素交互。其中,"nineoldandroids"是一个非常关键的部分,它是专门为兼容Android ...

    iOS_Animations_by_Tutorials_v4.0

    《iOS_Animations_by_Tutorials_v4.0》是一本针对iOS开发者的重要教程,专注于iOS平台上的动画技术。这本书深入浅出地介绍了各种动画效果的实现方法,为开发者提供了丰富的实践案例,帮助他们提升应用程序的用户体验...

    iOS Animations by Tutorials v4.0 Source Code (Swift4)

    《iOS Animations by Tutorials v4.0》是针对苹果移动平台iOS的一本教程源代码集,使用Swift4语言编写。这个资源包包含了丰富的动画实现示例,旨在帮助开发者提升在iOS应用中创建动态效果的能力。以下是根据标题、...

    PhysicsBasedAnimation-Physics-based Animations 动画学习实例.zip

    总之,"PhysicsBasedAnimation-Physics-based Animations 动画学习实例.zip"是一个宝贵的资源,对于想要在游戏开发、虚拟现实或增强现实等领域实现物理效果的开发者来说,它提供了一个实践和探索的平台。通过深入...

    Creating Web Animations_ Bringi - Kirupa Chinnathambi

    - 在现代互联网应用中,动画已经成为用户体验不可或缺的一部分。 - 动画可以提升用户界面(UI)的吸引力,使应用程序更加生动、直观。 2. **动画的应用场景** - 加载指示器、过渡效果、按钮动画等。 - 通过动画...

    前端项目-web-animations.zip

    总的来说,“前端项目-web-animations.zip”是一个非常有价值的资源,对于想要深入理解和使用Web Animations API的前端开发者来说,这是一个很好的实践和学习平台。通过这个项目,开发者不仅可以掌握如何使用...

Global site tag (gtag.js) - Google Analytics