玩转Android---2D图形及动画---动画分析(Tween详细分析)
来源:互联网 发布:java接口与安卓对接 编辑:程序博客网 时间:2024/05/11 05:09
原址:http://hualang.iteye.com/category/143855
在Android系统中提供了两种动画实现方式:一种是Tween动画,这种实现方式可以使视图组件移动、放大、缩小以及产生透明度的变化;另一种方式是Frame动画,这是逐帧动画,通过顺序播放排列好的图片来实现的,类似电影。
Tween动画:
Tween动画能完成一系列简单的变化(如位置、尺寸、透明度和旋转等)。例如,在你的程序中有一个ImageView组件,我们通过Tween动画可以使该视图实现放大、缩小、旋转、渐变等。Tween动画类位于android.view.animation包中,该包中包含了一些常用的动画实现类。
- Animation:动画抽象类,其他几个实现类继承它
- ScaleAnimation:控制尺寸变化的动画类
- AlphaAnimation:控制透明度变化的动画类
- RotateAnimation:控制旋转变化的动画类
- TranslateAnimation:控制位置变化的动画类
- AnimationSet:定义动画属性集合类
- AnimationUtils:动画工具类
常用构造方法
参数说明
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坐标上的伸缩尺寸
pivotXType:X坐标伸缩模式(取值Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF、Animation.RELATIVE_TO_PARENT)
pivotXValue:相当于X坐标伸缩值
pivotYType:Y坐标伸缩模式(取值
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指的是相对于自身或父控件,
对应值应该理解为相对于自身或者父控件的几倍或百分之多少。
- <?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>
- 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);
- }
- });
- }
- }
- <?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>
- <?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>
- <?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>
- <?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>
- <?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>
- 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);
- }
- });
- }
- }
- 玩转Android---2D图形及动画---动画分析(Tween详细分析)
- 玩转Android---2D图形及动画---Tween动画
- 玩转Android---2D图形及动画---Frame动画
- 玩转Android---2D图形及动画---Gif动画
- 玩转Android---2D图形及动画---图片处理
- 分析android动画模块之Tween动画(转)
- Android 动画分析之Tween动画分析
- Android 动画分析之Tween动画分析
- 玩转Android---2D图形及动画---View类使用
- 玩转Android---2D图形及动画---View类使用
- Android:动画:tween动画
- android 动画 -- tween动画
- 分析了 Tween 动画的实现原理
- android 动画实现(一)Tween动画
- Android tween动画(补间动画)
- android 动画 -- tween动画(续)
- android Tween动画 (一)
- android Tween动画 (二)
- 分析Linux内核创建一个新进程的过程
- 玩转Android---2D图形及动画---Gif动画
- 玩转Android---2D图形及动画---图片处理
- raw_input() 与 input() __ Python
- HDU 1070 - Select the cheapest product
- 玩转Android---2D图形及动画---动画分析(Tween详细分析)
- Python 多线程 multithread
- #111 (div.2) A. Twins
- Leetcode NO.19 Remove Nth Node From End of List
- 黑马程序员--java--多态和内部类
- hdu 多校
- SEL数据类型的简单知识点——和方法有关
- 实现简单HTTP服务器-图片与CGI
- IOS 值得注意的地方