`

玩转Android---2D图形及动画---动画分析(Tween详细分析)

 
阅读更多

在Android系统中提供了两种动画实现方式:一种是Tween动画,这种实现方式可以使视图组件移动、放大、缩小以及产生透明度的变化;另一种方式是Frame动画,这是逐帧动画,通过顺序播放排列好的图片来实现的,类似电影。

 

Tween动画:

Tween动画能完成一系列简单的变化(如位置、尺寸、透明度和旋转等)。例如,在你的程序中有一个ImageView组件,我们通过Tween动画可以使该视图实现放大、缩小、旋转、渐变等。Tween动画类位于android.view.animation包中,该包中包含了一些常用的动画实现类。

 

  • Animation:动画抽象类,其他几个实现类继承它
  • ScaleAnimation:控制尺寸变化的动画类
  • AlphaAnimation:控制透明度变化的动画类
  • RotateAnimation:控制旋转变化的动画类
  • TranslateAnimation:控制位置变化的动画类
  • AnimationSet:定义动画属性集合类
  • AnimationUtils:动画工具类
总的来说,Android系统Tween动画提供了四种实现方式。Tween动画的实现凡是有两种:一种是通过硬编码的凡是在程序代码中实现;另一种是在配置文件中定义。Android系统推荐使用配置文件的方法,这种方式可口占星较好。

常用构造方法

参数说明

AlphaAnimation(float fromAlpha, float toAlpha)

fromAlpha:动画开始透明度

toAlpha:动画结束透明度(取值范围0.0~1.0)

1.0表示完全不透明

0.0表示完全透明

ScaleAnimation(float fromX, float toX,float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

fromX:起始X坐标上的伸缩尺寸

toX:结束X坐标上的伸缩尺寸

fromY:其实Y坐标上的伸缩尺寸

toY:结束Y坐标上的伸缩尺寸

pivotXTypeX坐标伸缩模式(取值Animation.ABSOLUTEAnimation.RELATIVE_TO_SELFAnimation.RELATIVE_TO_PARENT)

pivotXValue:相当于X坐标伸缩值

pivotYTypeY坐标伸缩模式(取值

Animation.ABSOLUTE

Animation.RELATIVE_TO_SELF

Animation.RELATIVE_TO_PARENT)

pivotYValue:相当于Y坐标伸缩值

TranslateAnimation(float fromXDelta, float toXDelta,

float fromYDelta, float toYDelta)

fromXDelta:其实X坐标

toXDelta:结束X坐标

fromYDelta:起始Y坐标

toYDelta:结束Y坐标

RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, pivotYType, float pivotYValue)

fromDegrees:旋转开始角度

toDegrees:旋转结束角度

pivotXType:X坐标伸缩模式(取值Animation.ABSOLUTE,

Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_PARENT

pivotXValue:相当于X坐标伸缩值

pivotYType:Y坐标伸缩模式(取值Animation.ABSULUTE,

Animation.RELATIVE_TO_SELF,Animation.RELATIVE_TO_PARENT

pivotYValue:相当于Y坐标伸缩值

如果全部选择Animation.ABSOLUTE,其实就是第二个构造函数。

以x轴为例介绍参照与对应值的关系

 如果选择参照为Animation.ABSOLUTE,那么对应的值应该是具体的坐标值,比如100到300,指绝对的屏幕像素单位

 如果选择参照为Animation.RELATIVE_TO_SELF或者 Animation.RELATIVE_TO_PARENT指的是相对于自身或父控件,

 对应值应该理解为相对于自身或者父控件的几倍或百分之多少。


下面是个例子,看看硬编码是如何实现的
布局文件main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<LinearLayout
		android:orientation="horizontal"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
		<Button
			android:id="@+id/ScaleBtn"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="Scale"
		/>
		<Button
			android:id="@+id/AlphaBtn"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="Alpha"
		/>
		<Button
			android:id="@+id/TranslateBtn"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="Translate"
		/>
		<Button
			android:id="@+id/RotateBtn"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="Rotate"
		/>
	</LinearLayout>
	<ImageView
		android:id="@+id/Imageview01"
		android:layout_gravity="center"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:src="@drawable/ni_png_0707"
	/>
</LinearLayout>
 MainActivity.java
package com.loulijun.AnimaitonTest;

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.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private Button scaleBtn,rotateBtn,alphaBtn,translateBtn;
    private ImageView img;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        img = (ImageView)findViewById(R.id.Imageview01);
        scaleBtn = (Button)findViewById(R.id.ScaleBtn);
        rotateBtn = (Button)findViewById(R.id.RotateBtn);
        alphaBtn = (Button)findViewById(R.id.AlphaBtn);
        translateBtn = (Button)findViewById(R.id.TranslateBtn);
        
        scaleBtn.setOnClickListener(new Button.OnClickListener()
        {

			@Override
			public void onClick(View arg0) {
				//创建伸缩动画
				Animation scaleAnimation = new ScaleAnimation(0f,1f,0f,1f,
						Animation.RELATIVE_TO_SELF, 0.5f,
						Animation.RELATIVE_TO_SELF, 0.5f);
				//设置动画持续时间
				scaleAnimation.setDuration(2000);
				//开始动画
				img.startAnimation(scaleAnimation);
			}
        	
        });
        
        rotateBtn.setOnClickListener(new OnClickListener()
        {

			@Override
			public void onClick(View v) {
				//创建rotate旋转动画
				Animation rotateAnimation = new RotateAnimation(0f, +36f, 
						Animation.RELATIVE_TO_SELF, 0.5f,
						Animation.RELATIVE_TO_SELF, 0.5f);
				//设置动画持续时间
				rotateAnimation.setDuration(2000);
				//开始动画
				img.startAnimation(rotateAnimation);
			}
        	
        });
        
        translateBtn.setOnClickListener(new OnClickListener()
        {

			@Override
			public void onClick(View v) {
				// 创建位置变化动画
				Animation translateAnimation = new TranslateAnimation(10, 100, 10, 100);
				//设置动画持续时间
				translateAnimation.setDuration(2000);
				//开始动画
				img.startAnimation(translateAnimation);
			}
        	
        });
        
        alphaBtn.setOnClickListener(new OnClickListener()
        {

			@Override
			public void onClick(View v) {
				//创建渐变动画
				Animation alphaAnimation = new AlphaAnimation(0.1f,1.0f);
				//设置动画持续时间
				alphaAnimation.setDuration(2000);
				//开始动画
				img.startAnimation(alphaAnimation);
			}
        	
        });
    }
}
 运行效果(四个效果自己尝试下就可以了)


 
当然,最好是在配置文件中来实现动画
在res/anim目录下定义不同的动画的配置文件,一般要有个set根元素,根元素里面定义不同的动画了,然后通过调用
AnimationUtils.loadAnimation()方法获取动画实例,调用视图组件的startAnimation()方法开启动画即可

主布局文件main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
	<LinearLayout
		android:orientation="horizontal"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content">
		<Button
			android:id="@+id/ScaleBtn"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="Scale"
		/>
		<Button
			android:id="@+id/AlphaBtn"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="Alpha"
		/>
		<Button
			android:id="@+id/TranslateBtn"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="Translate"
		/>
		<Button
			android:id="@+id/RotateBtn"
			android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="Rotate"
		/>
	</LinearLayout>
	<ImageView
		android:id="@+id/Imageview01"
		android:layout_gravity="center"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:src="@drawable/ni_png_0707"
	/>
</LinearLayout>
 然后在res/anim/目录下创建四个布局文件,分别对应四个动画效果的配置
myalpha.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
	<alpha
		android:fromAlpha="0.1"
		android:toAlpha="1.0"
		android:duration="2000"
	/>
</set>
 myrotate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
	<rotate
		android:fromDegrees="0"
		android:toDegrees="-180"
		android:pivotX="50%"
		android:pivotY="50%"
		android:duration="2000"
	/>
</set>
 mytranslate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
	<translate
		android:fromXDelta="10"
		android:toXDelta="100"
		android:fromYDelta="10"
		android:toYDelta="100"
		android:duration="2000"
	/>
</set>
 myscale.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
	<scale
		android:fromXScale="0.0"
		android:toXScale="1.0"
		android:fromYScale="0.0"
		android:toYScale="1.0"
		android:pivotX="50%"
		android:pivotY="50%"
		android:duration="2000"
	/>
</set>
 下面这个是主程序了,在里面主要是通过AnimationUtils.loadAnimaiton()将上面的动画配置文件加载后,开始动画即可
MainActivity.java
package com.loulijun.animationtest2;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private Button alphaBtn,rotateBtn,translateBtn,scaleBtn;
    private ImageView img;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        img = (ImageView)findViewById(R.id.Imageview01);
        alphaBtn = (Button)findViewById(R.id.AlphaBtn);
        rotateBtn = (Button)findViewById(R.id.RotateBtn);
        translateBtn = (Button)findViewById(R.id.TranslateBtn);
        scaleBtn = (Button)findViewById(R.id.ScaleBtn);
        alphaBtn.setOnClickListener(new Button.OnClickListener()
        {

			@Override
			public void onClick(View v) {
				//设置渐变动画
				Animation alphaAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.myalpha);
				//开始动画
				img.startAnimation(alphaAnimation);
			}
        	
        });
        
        rotateBtn.setOnClickListener(new Button.OnClickListener()
        {

			@Override
			public void onClick(View v) {
				//设置旋转动画
				Animation rotateAnimation = AnimationUtils.loadAnimation(MainActivity.this,R.anim.myrotate);
				//开始动画
				img.startAnimation(rotateAnimation);
			}
        	
        });
        
        translateBtn.setOnClickListener(new Button.OnClickListener()
        {

			@Override
			public void onClick(View v) {
				//设置位置变化动画
				Animation translateAnimation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.mytranslate);
				//开始动画
				img.startAnimation(translateAnimation);
			}
        	
        });
        
        scaleBtn.setOnClickListener(new Button.OnClickListener()
        {

			@Override
			public void onClick(View v) {
				//创建伸缩动画
				Animation scaleAnimation = AnimationUtils.loadAnimation(MainActivity.this, 
						R.anim.myscale);
				//开始动画
				img.startAnimation(scaleAnimation);
			}
        	
        });
    }
}
 
效果如下:


 

 

  • 大小: 22.7 KB
分享到:
评论

相关推荐

    Android---Tween动画

    Android---Tween动画 运用了4种Tween的动画效果,包括: rotate(旋转),scale(伸缩), alpha(透明度),translate(平移) 以及tween(综合4种效果)

    Android-Animation动画之Tween Animation补间动画

    补间动画(Tween Animation)是Android提供的基本动画类型,它通过对对象的透明度、大小、位置或旋转等属性进行平滑过渡来实现视觉效果。本篇文章将深入探讨Android中的Tween Animation,包括其原理、使用方法以及...

    android tween动画代码示例及详解

    在`TestAnimation`这个项目中,你可能找到了更多关于这些动画的示例和详细注释,这将有助于你更好地理解和应用Android Tween动画。通过实践和调试这些代码,你可以掌握Android动画系统的核心概念,从而在开发中创造...

    android Tween动画的xml实现

    Tween动画,又称补间动画,是Android提供的基础动画类型,主要用于对象的平移、旋转、缩放和透明度变化等效果。本篇文章将深入探讨如何通过XML在Android中实现Tween动画。 首先,让我们了解Tween动画的基本原理。...

    Android Tween动画源码

    Tween动画,又称平移动画,是Android提供的基础动画类型,主要用于对象的透明度、缩放、平移和旋转等属性的变化。让我们深入探讨Android Tween动画的源码,理解其工作原理。 首先,`alpha`动画涉及到的是对象的透明...

    Android动画之Tween动画实现

    Android 平台提供了两类动画。 一类是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果(旋转、平移、放缩和渐变);第二类就是 Frame动画,即顺序的播放事先做好的图像,与gif图片原理类似。 本例子...

    安卓动画效果相关-android开发中的Tween动画动画演示demo。此demo为androidstudio2.0所写.rar

    本篇将详细探讨Android Tween动画的基本原理、实现方式以及如何在Android Studio 2.0中创建和运行演示demo。 一、Tween动画简介 Tween动画,又称为“补间动画”,它通过在两个关键帧之间插值计算,使对象在一定时间...

    Android-Anim-Playground.zip

    《Android 动画实战:Android-Anim-Playground 深入解析》 在 Android 开发中,动画是提升用户体验的重要一环。Android-Anim-Playground 是一个专门用于研究和学习 Android 动画的项目,它包含了丰富的动画示例,...

    java-universal-tween-engine,一个动画系统库

    Java Universal Tween Engine(UTEE)是一个轻量级且强大的动画系统库,专为Java和Android开发者设计,用于创建流畅、动态的2D动画效果。这个库由Aurelien Ribon开发,它允许开发者轻松地在游戏、应用或其他任何需要...

    Android 游戏开发之Tween动画的实现

    Tween动画,源自于图形设计领域,意为“时间插值”,在Android中,它是通过`android.animation.ObjectAnimator`类来实现的。下面,我们将深入探讨如何在Android游戏中实现Tween动画,以及相关的技术细节。 首先,...

    Android-Android密码动画

    2. **动画框架**:Android提供了多种动画框架,如Tween动画、帧动画以及属性动画。在密码动画中,通常会用到属性动画(Property Animation),通过改变密码输入框中字符的状态(如透明度、大小、位置等)来实现动画...

    Tween 动画的旋转缩放透明度和平移 demo

    Tween动画在IT行业中,特别是在游戏开发、用户界面设计和移动应用中扮演着重要角色,它是一种平滑地改变对象属性的动画技术。Tween一词来源于英语“in-between”,意为“两者之间”,在动画领域中,它指的是在两个...

    Tween动画完全解析

    在博客《Tween动画完全解析》中,作者详细讲解了如何在Android平台上创建和使用Tween动画。首先,你需要创建一个动画对象,比如`TranslateAnimation`,然后设置它的起始和结束坐标,以及持续时间和重复模式。接着,...

    android-flip切换界面的翻转动画效.zip

    在Android中,动画主要分为两种类型:补间动画(Tween Animation)和帧动画(Frame Animation)。补间动画用于改变对象的属性,如位置、大小、透明度等,而帧动画则类似于播放一系列图片来创建连续的动画效果。在这...

    tween-one:动画化一个React元素

    rc-tween-one ReactTweenOne组件 浏览器支持 IE 10以上 :check_mark: Chrome31.0+ :check_mark: Firefox 31.0+ :check_mark: Opera 30.0+ :check_mark: Safari 7.0以上 :check_mark: 发展 npm install npm ...

    swift-tween-controller用于创建交互式菜单和指南的Swift工具包

    Swift-Tween-Controller是一款专为iOS开发者设计的Swift工具包,它主要用来创建动态、交互式的菜单和指导性界面,从而提升用户在应用程序中的体验。这个工具包利用了动画技术,使得过渡效果更加平滑自然,增加了应用...

    Android基础——Tween动画、Drawable动画、Property动画、MaterialDesign动画、Trasition动画

    本文将深入探讨Android中的五种主要动画类型:Tween动画、Drawable动画、Property动画、Material Design动画以及Transition动画。 1. Tween动画(补间动画) Tween动画是Android中最基础的动画形式,它通过改变对象...

    Android-一个App的Logo的动画效果实现

    本文将深入探讨如何在Android平台上实现一个App的Logo动画效果,结合提供的"QccLogo-Animation-master"项目,我们将分析关键的技术点和步骤。 1. **动画类型选择** Android支持多种动画类型,包括属性动画...

    tween动画动作集

    这个“tween-engine-demo.jar”文件很可能是一个Tween动画引擎的示例库,它包含了实现Tween动画所需的各种类和方法。开发者可以导入这个库,然后根据需求创建和控制动画。"data"可能包含了一些配置文件或者资源,...

    Android-一个精致的打钩小动画模仿轻芒杂志标记已读的动画

    - Android中的动画主要有两种类型:补间动画(Tween Animation)和帧动画(Frame Animation)。由于我们要实现的是一个交互式的打钩过程,所以更适合使用属性动画(Property Animation),它能更好地控制动画的细节...

Global site tag (gtag.js) - Google Analytics