Android动画 很详细

编程入门 行业动态 更新时间:2024-10-15 22:26:17

Android<a href=https://www.elefans.com/category/jswz/34/1769013.html style=动画 很详细"/>

Android动画 很详细

转自.html

Android动画
内容:
动画

补间动画

AnimationSet

插补

逐帧动画

LayoutAnimationsController

AnimationListener

动画

一,动画介绍
Animations是一个实现android UI界面动画效果的API,动画提供了一系列的动画效果,可以进行旋转,缩放,淡入淡出等,这些效果可以应用在绝大多数的控件中。
二,动画的
动画从总体上可以分为两大类:
1.Tweened Animations:该类动画提供了旋转,移动,伸展和淡出等效果.Alpha--淡入淡出,Scale--缩放效果,旋转 - 旋转,翻译 - 移动效果
.2 。逐帧动画:这一类动画可以创建一个Drawable序列,这些Drawable可以按照指定的时间间歇一个个个的显示。

三,动画的使用方法(代码中使用)
动画扩展对象实现Cloneable 
使用TweenedAnimations的步骤:
1。创建一个AnimationSet对象(动子子类); 
2.增加需要创建相应的动画对象; 
3.更加项目的需求,为动画对象设置相应的数据; 
4.将Animatin 对象添加到AnimationSet对象当中; 
5.使用控件对象开始执行AnimationSet。

  Tweened Animations的分类
  1,Alpha:淡入淡出效果
  2,Scale:缩放效果
  3,旋转:旋转效果
  4,翻译:移动效果

动画的四个子类:
  AlphaAnimation,TranslateAnimation,ScaleAnimation,RotateAnimation 
四,具体实现
1,main.xml

<!--?xml version="1.0" encoding="utf-8"?-->
<linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android=""><font></font>
<font></font><linearlayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal"><button android:id="@+id/rotateButton" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="旋转"></button><button android:id="@+id/scaleButton" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="缩放"></button><button android:id="@+id/alphaButton" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="淡入淡出"></button><button android:id="@+id/translateButton" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="移动"><font></font><font></font><linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical"><imageview android:id="@+id/image" android:layout_centerinparent="true" android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/an">

2,的.java文件

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;">importandroid.app.Activity;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
importandroid.os.Bundle;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
importandroid.view.View;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
importandroid.view.View.OnClickListener;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
import android.view.animation.AlphaAnimation;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
import android.view.animation.Animation;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
importandroid.view.animation.AnimationSet;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
importandroid.view.animation.RotateAnimation;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
importandroid.view.animation.ScaleAnimation;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
import android.view.animation.TranslateAnimation;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
importandroid.widget.Button;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
importandroid.widget.ImageView;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
public class Animation1Activity extends Activity {</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">private Button rotateButton = null;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">private Button scaleButton = null;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">private Button alphaButton = null;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">private Button translateButton = null;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">private ImageView image = null;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">@覆盖</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">public void onCreate(Bundle savedInstanceState){</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">super.onCreate(savedInstanceState);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">的setContentView(R.layout.main);</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">rotateButton =(Button)findViewById(R.id.rotateButton);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">scaleButton =(Button)findViewById(R.id.scaleButton);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">alphaButton =(Button)findViewById(R.id.alphaButton);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">translateButton =(Button)findViewById(R.id.translateButton);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">image =(ImageView)findViewById(R.id.image);</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">rotateButton.setOnClickListener(newRotateButtonListener());</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">scaleButton.setOnClickListener(newScaleButtonListener());</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">alphaButton.setOnClickListener(newAlphaButtonListener());</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">translateButton.setOnClickListener(</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">new TranslateButtonListener());</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">}</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">class AlphaButtonListener implementsOnClickListener {</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">public void onClick(查看v){</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//创建一个AnimationSet对象,参数为布尔型,</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//真表示使用动画的内插器,假则是使用自己的</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">AnimationSet animationSet = new AnimationSet(true);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//创建一个AlphaAnimation对象,参数从完全的透明度,到完全的不透明</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">AlphaAnimation alphaAnimation =新的AlphaAnimation(1,0);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//设置动画执行的时间</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">alphaAnimation.setDuration(500);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//将alphaAnimation对象添加到AnimationSet当中</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">animationSet.addAnimation(alphaAnimation);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//使用的ImageView的startAnimation方法执行动画</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">image.startAnimation(animationSet);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">}</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">}</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">class RotateButtonListener implementsOnClickListener {</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">public void onClick(查看v){</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">AnimationSet animationSet = new AnimationSet(true);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//参数1:从哪个旋转角度开始</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//参数2:转到什么角度</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//后4个参数用于设置围绕着旋转的圆的圆心在哪里</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//参数3:确定X轴坐标的类型,有ABSOLUT绝对坐标,RELATIVE_TO_SELF相对于自身坐标,RELATIVE_TO_PARENT相对于父控件的坐标</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//参数4:X轴的值,0.5F表明是以自身这个控件的一半长度为X轴</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//参数5:确定ý轴坐标的类型</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//参数6:Y轴的值,0.5F表明是以自身这个控件的一半长度为X轴</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">RotateAnimation rotateAnimation = new RotateAnimation(0,360,</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">Animation.RELATIVE_TO_SELF,0.5F,</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">Animation.RELATIVE_TO_SELF,0.5F);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">rotateAnimation.setDuration(1000);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">animationSet.addAnimation(rotateAnimation);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">image.startAnimation(animationSet);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">}</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">}</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">class ScaleButtonListener implementsOnClickListener {</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">public void onClick(查看v){</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">AnimationSet animationSet = new AnimationSet(true);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//参数1:X轴的初始值</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//参数2:X轴收缩后的值</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//参数3:Y轴的初始值</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//参数4:Y轴收缩后的值</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//参数5:确定X轴坐标的类型</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//参数6:X轴的值,0.5F表明是以自身这个控件的一半长度为X轴</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//参数7:确定ý轴坐标的类型</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//参数8:Y轴的值,0.5F表明是以自身这个控件的一半长度为X轴</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">ScaleAnimation scaleAnimation = new ScaleAnimation(</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">0,0.1f,0,0.1f,</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">Animation.RELATIVE_TO_SELF,0.5F,</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">Animation.RELATIVE_TO_SELF,0.5F);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">scaleAnimation.setDuration(1000);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">animationSet.addAnimation(scaleAnimation);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">image.startAnimation(animationSet);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">}</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">}</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">class TranslateButtonListener implementsOnClickListener {</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">public void onClick(查看v){</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">AnimationSet animationSet = new AnimationSet(true);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//参数1〜2:X轴的开始位置</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//参数3〜4:Y轴的开始位置</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//参数5〜6:X轴的结束位置</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//参数7〜8:X轴的结束位置</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">TranslateAnimation translateAnimation =</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">新的TranslateAnimation(</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">Animation.RELATIVE_TO_SELF,0F,</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">Animation.RELATIVE_TO_SELF,0.5F,</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">Animation.RELATIVE_TO_SELF,0F,</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">Animation.RELATIVE_TO_SELF,0.5F);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">translateAnimation.setDuration(1000);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">animationSet.addAnimation(translateAnimation);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">image.startAnimation(animationSet);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">}</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">}</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

Tween Animations的通用方法
  1,setDuration(long durationMills)
  设置动画持续时间(单位:毫秒)
  2,setFillAfter(Boolean fillAfter)
  如果fillAfter的值为true,则动画执行后,控件将停留在执行结束的状态
  3, setFillBefore(Boolean fillBefore)
  如果fillBefore的值为true,则动画执行后,控件将回到动画执行之前的状态
  4,setStartOffSet(long startOffSet)
  设置动画执行之前的等待时间
  5,setRepeatCount(int repeatCount)
  设置动画重复执行的次数

在代码中使用动画可以很方便的调试,运行,但是代码的可重用性差,重复代码多。同样可以在XML文件中配置的动画,这样做可维护性变高了,只不过不容易进行调试。
一,在xml中使用动画步骤
1.在res文件夹下建立一个anim文件夹; 
2.创建xml文件,并首先加入set标签,更改标签如下:

<!--?xml version="1.0" encoding="utf-8"?-->
<set android:interpolator="@android:anim/accelerate_interpolator" xmlns:android="">
</set>

 3.在该标签当中加入旋转,α,规模或者翻译标签;

</alpha>

4.在代码当中使用AnimationUtils当中装载XML文件,并生成动画对象。因为动画是AnimationSet的子类,所以向上转型,用动画对象接收。

二,具体实现
1,alpha.xml

<!--?xml version="1.0" encoding="utf-8"?-->
<set android:interpolator="@android:anim/accelerate_interpolator" xmlns:android=""><!-- fromAlpha和toAlpha是起始透明度和结束时透明度 --></alpha></set>

2,rotate.xml

<set android:interpolator="@android:anim/accelerate_interpolator" xmlns:android=""><!--fromDegrees:开始的角度toDegrees:结束的角度,+表示是正的pivotX:用于设置旋转时的x轴坐标例1)当值为"50",表示使用绝对位置定位2)当值为"50%",表示使用相对于控件本身定位3)当值为"50%p",表示使用相对于控件的父控件定位pivotY:用于设置旋转时的y轴坐标--><rotate android:duration="1000" android:fromdegrees="0" android:pivotx="50%" android:pivoty="50%" android:todegrees="+360">
</rotate></set>

3,scale.xml

<!--?xml version="1.0" encoding="utf-8"?-->
<set android:interpolator="@android:anim/accelerate_interpolator" xmlns:android=""><!--起始x轴坐标止x轴坐标始y轴坐标止y轴坐标轴的坐标轴的坐标--><scale android:duration="1000" android:fromxscale="1.0" android:fromyscale="1.0" android:pivotx="50%" android:pivoty="50%" android:toxscale="0.0" android:toyscale="0.0">
</scale></set>

4,translate.xml

<!--?xml version="1.0" encoding="utf-8"?-->
<set android:interpolator="@android:anim/accelerate_interpolator" xmlns:android=""><!--始x轴坐标止x轴坐标始y轴坐标止y轴坐标--><translate android:duration="2000" android:fromxdelta="0%" android:fromydelta="0%" android:toxdelta="100%" android:toydelta="100%">

5,.java文件

<font style="vertical-align: inherit;"><font style="vertical-align: inherit;">importandroid.app.Activity;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
importandroid.os.Bundle;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
importandroid.view.View;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
importandroid.view.View.OnClickListener;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
import android.view.animation.Animation;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
importandroid.view.animation.AnimationUtils;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
import android.widget.Button;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
import android.widget.ImageView;</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">
public class Animation1Activity extends Activity {</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">private Button rotateButton = null;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">private Button scaleButton = null;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">private Button alphaButton = null;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">private Button translateButton = null;</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">private ImageView image = null;</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">@覆盖</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">public void onCreate(Bundle savedInstanceState){</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">super.onCreate(savedInstanceState);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">的setContentView(R.layout.main);</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">rotateButton =(Button)findViewById(R.id.rotateButton);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">scaleButton =(Button)findViewById(R.id.scaleButton);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">alphaButton =(Button)findViewById(R.id.alphaButton);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">translateButton =(Button)findViewById(R.id.translateButton);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">image =(ImageView)findViewById(R.id.image);</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">rotateButton.setOnClickListener(newRotateButtonListener());</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">scaleButton.setOnClickListener(newScaleButtonListener());</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">alphaButton.setOnClickListener(newAlphaButtonListener());</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">translateButton.setOnClickListener(newTranslateButtonListener());</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">}</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">class AlphaButtonListener implementsOnClickListener {</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">public void onClick(查看v){</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//使用AnimationUtils装载动画配置文件</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">动画动画= AnimationUtils.loadAnimation(</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">Animation1Activity.this,R.anim.alpha);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">//启动动画</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">image.startAnimation(动画);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">}</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">}</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">class RotateButtonListener implementsOnClickListener {</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">public void onClick(查看v){</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">动画动画= AnimationUtils.loadAnimation(</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">Animation1Activity.this,R.anim.rotate);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">image.startAnimation(动画);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">}</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">}</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">class ScaleButtonListener implementsOnClickListener {</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">public void onClick(查看v){</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">动画动画= AnimationUtils.loadAnimation(</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">Animation1Activity.this,R.anim.scale);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">image.startAnimation(动画);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">}</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">}</font></font><font></font>
<font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">class TranslateButtonListener implementsOnClickListener {</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">public void onClick(查看v){</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">动画动画= AnimationUtils.loadAnimation(Animation1Activity.this,R.anim.translate);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">image.startAnimation(动画);</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">}</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">}</font></font><font></font><font style="vertical-align: inherit;"><font style="vertical-align: inherit;">

AnimationSet的具体使用方法

<code>   1.AnimationSet是Animation的子类;<font></font>
<font></font>2.一个AnimationSet包含了一系列的Animation;<font></font>
<font></font>3.针对AnimationSet设置一些Animation的常见属性(如startOffset,duration等),可以被包含在AnimationSet当中的Animation集成;<font></font>
</code>

例:一个AnimationSet中有两个Animation,效果叠加

第一种方法:

doubleani.xml

<code><!--?xml version="1.0" encoding="utf-8"?-->
<set android:interpolator="@android:anim/accelerate_interpolator" android:shareinterpolator="true" xmlns:android=""><!-- fromAlpha和toAlpha是起始透明度和结束时透明度 --><translate android:duration="2000" android:fromxdelta="0%" android:fromydelta="0%" android:toxdelta="100%" android:toydelta="100%">
</translate></alpha></set></code>

java文件

<code>classDoubleButtonListener implements OnClickListener {<font></font>public void onClick(View v) {<font></font>// 使用AnimationUtils装载动画配置文件<font></font>Animation animation = AnimationUtils.loadAnimation(<font></font>Animation2Activity.this, R.anim. doubleani);<font></font>// 启动动画<font></font>image.startAnimation(animation);<font></font>}<font></font>}</code>

第二种方法:

.java文件中

<code>classDoubleButtonListener implements OnClickListener {<font></font>public void onClick(View v) {<font></font>AnimationSet animationSet = new AnimationSet(true);<font></font>AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);<font></font>RotateAnimation rotateAnimation = new RotateAnimation(0, 360,<font></font>Animation.RELATIVE_TO_SELF,0.5f,<font></font>Animation.RELATIVE_TO_SELF,0.5f);<font></font>rotateAnimation.setDuration(1000);<font></font>animationSet.addAnimation(rotateAnimation);<font></font>animationSet.addAnimation(alphaAnimation);<font></font>image.startAnimation(animationSet);<font></font>
<font></font>}<font></font>}</code>

Interpolator的具体使用方法

<code><code>   Interpolator定义了动画变化的速率,在Animations框架当中定义了一下几种Interpolator
</code></code>

? AccelerateDecelerateInterpolator:在动画开始与结束的地方速率改变比较慢,在中间的时候速率快。
? AccelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始加速
? CycleInterpolator:动画循环播放特定的次数,速率改变沿着正弦曲线
? DecelerateInterpolator:在动画开始的地方速率改变比较慢,然后开始减速
? LinearInterpolator:动画以均匀的速率改变
分为以下几种情况:
1、在set标签中

1

<code><code><set android:interpolator="@android:anim/accelerate_interpolator" xmlns:android=""></set></code></code>

2、如果在一个set标签中包含多个动画效果,如果想让这些动画效果共享一个Interpolator。
android:shareInterpolator=”true”
3、如果不想共享一个interpolator,则设置android:shareInterpolator=”true”,并且需要在每一个动画效果处添加interpolator。

1

<code><code></alpha></code></code>

4、如果是在代码上设置共享一个interpolator,则可以在AnimationSet设置interpolator。

1

2

<code><code>AnimationSet animationSet = newAnimationSet(true);

animationSet.setInterpolator(new AccelerateInterpolator());</code></code>

5、如果不设置共享一个interpolator则可以在每一个Animation对象上面设置interpolator。

1

2

3

<code><code>AnimationSet animationSet = newAnimationSet(false);

alphaAnimation.setInterpolator(new AccelerateInterpolator());

rotateAnimation.setInterpolator(new DecelerateInterpolator());</code></code>

Frame-By-Frame Animations的使用方法

1

2

<code><code><code>   Frame-By-Frame Animations是一帧一帧的格式显示动画效果。类似于电影胶片拍摄的手法。

</code></code></code>

main.xml

1

2

3

4

6

7

<code><code><code><!--?xml version="1.0" encoding="utf-8"?-->

<linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="">

    <linearlayout android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal"><button android:id="@+id/button" android:layout_height="wrap_content" android:layout_width="wrap_content" android:text="运动">

     

    <linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical">

       <imageview android:id="@+id/image" android:layout_centerinparent="true" android:layout_height="wrap_content" android:layout_width="wrap_content">

    </imageview></linearlayout></button></linearlayout></linearlayout></code></code></code>

3、anim.xml

1

2

3

4

6

7

8

9

<code><code><code><!--?xml version="1.0" encoding="utf-8"?-->

 

    <item android:drawable="@drawable/a_01" android:duration="50">

    <item android:drawable="@drawable/a_02" android:duration="50">

    <item android:drawable="@drawable/a_03" android:duration="50">

    <item android:drawable="@drawable/a_04" android:duration="50">

    <item android:drawable="@drawable/a_05" android:duration="50">

    <item android:drawable="@drawable/a_06" android:duration="50">

</item></item></item></item></item></item></animation-list></code></code></code>

4、.java文件

1

2

3

4

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

<code><code><code>importandroid.app.Activity;

importandroid.graphics.drawable.AnimationDrawable;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.ImageView;

public class AnimationsActivity extends Activity {

    private Button button = null;

    private ImageView imageView = null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        button = (Button)findViewById(R.id.button);

        imageView = (ImageView)findViewById(R.id.image);

        button.setOnClickListener(newButtonListener());

    }

    class ButtonListener implementsOnClickListener{

       public void onClick(View v) {

           imageView.setBackgroundResource(R.anim.anim);

           AnimationDrawable animationDrawable = (AnimationDrawable)

              imageView.getBackground();

           animationDrawable.start();

       }

    }

}</code></code></code>

LayoutAnimationsController
1、什么是LayoutAnimationsController
LayoutAnimationsController可以用于实现使多个控件按顺序一个一个的显示。
1)LayoutAnimationsController用于为一个layout里面的控件,或者是一个ViewGroup里面的控件设置统一的动画效果。
2)每一个控件都有相同的动画效果。
3)控件的动画效果可以在不同的时间显示出来。
4)LayoutAnimationsController可以在xml文件当中设置,以可以在代码当中进行设置。
2、在xml当中使用LayoutAnimationController
1)在res/anim文件夹下创建一个名为list_anim_layout.xml文件:
android:delay - 动画间隔时间;子类动画时间间隔 (延迟) 70% 也可以是一个浮点数 如“1.2”等
android:animationOrder - 动画执行的循序(normal:顺序,random:随机,reverse:反向显示)
android:animation – 引用动画效果文件

1

<code><code><code><layoutanimation android:animation="@anim/list_anim" android:animationorder="normal" android:delay="0.5" xmlns:android=""></layoutanimation></code></code></code>

2)创建list_anim.xml文件,设置动画效果

1

2

3

4

<code><code><code><!--?xml version="1.0" encoding="utf-8"?-->

<set android:interpolator="@android:anim/accelerate_interpolator" android:shareinterpolator="true" xmlns:android="">

     

</alpha></set></code></code></code>

3)在布局文件main.xml当中为ListVIew添加如下配置

1

<code><code><code><listview android:id="@id/android:list" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layoutanimation="@anim/list_anim_layout" android:scrollbars="vertical"></listview></code></code></code>

4)程序结构:

5)list_anim_layout.xml

1

<code><code><code><layoutanimation android:animation="@anim/list_anim" android:animationorder="normal" android:delay="0.5" xmlns:android=""></layoutanimation></code></code></code>

6)list_anim.xml

1

2

3

4

<code><code><code><!--?xml version="1.0" encoding="utf-8"?-->

<set android:interpolator="@android:anim/accelerate_interpolator" android:shareinterpolator="true" xmlns:android="">

     

</alpha></set></code></code></code>

7)main.xml

1

2

3

<code><code><code><!--?xml version="1.0" encoding="utf-8"?-->

<linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="">

    <listview android:id="@id/android:list" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layoutanimation="@anim/list_anim_layout" android:scrollbars="vertical"><button android:id="@+id/button" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="测试"></button></listview></linearlayout></code></code></code>

8)item.xml

1

2

3

4

<code><code><code><!--?xml version="1.0" encoding="utf-8"?-->

<linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="horizontal" android:paddingbottom="1dip" android:paddingleft="10dip" android:paddingright="10dip" android:paddingtop="1dip" xmlns:android="">

    <textview android:id="@+id/name" android:layout_height="30dip" android:layout_width="180dip" android:singleline="true" android:textsize="5pt">

    <textview android:id="@+id/sex" android:layout_height="fill_parent" android:layout_width="fill_parent" android:singleline="true" android:textsize="5pt">

</textview></textview></linearlayout></code></code></code>

9)java文件

1

2

3

4

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

三十

31

32

33

34

35

36

37

38

39

40

41

<code><code><code>public class Animation2Activity extendsListActivity {

    private Button button = null;

    private ListView listView = null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        listView = getListView();

        button = (Button)findViewById(R.id.button);

        button.setOnClickListener(newButtonListener());

    }

    private ListAdapter createListAdapter() {

       List<hashmap<string,string>> list =

           new ArrayList<hashmap<string,string>>();

       HashMap<string,string> m1 = new HashMap<string,string>();

       m1.put("name", "bauble");

       m1.put("sex", "male");

       HashMap<string,string> m2 = new HashMap<string,string>();

       m2.put("name", "Allorry");

       m2.put("sex", "male");

       HashMap<string,string> m3 = new HashMap<string,string>();

       m3.put("name", "Allotory");

       m3.put("sex", "male");

       HashMap<string,string> m4 = new HashMap<string,string>();

       m4.put("name", "boolbe");

       m4.put("sex", "male");

       list.add(m1);

       list.add(m2);

       list.add(m3);

       list.add(m4);

       SimpleAdapter simpleAdapter = new SimpleAdapter(

              this,list,R.layout.item,new String[]{"name","sex"},

              new int[]{R.id.name,R.id.sex});

       return simpleAdapter;

    }

    private class ButtonListener implementsOnClickListener{

       public void onClick(View v) {

           listView.setAdapter(createListAdapter());

       }

    }

}</string,string></string,string></string,string></string,string></string,string></string,string></string,string></string,string></hashmap<string,string></hashmap<string,string></code></code></code>

备注:要将整个动画效果设置到LinerLayout中,可以这样设置:

1

 

3、在代码当中使用LayoutAnimationController
1)去掉main.xml中的

1

<code><code><code>android:layoutAnimation="@anim/list_anim_layout"/></code></code></code>

2)创建一个Animation对象:可以通过装载xml文件,或者是直接使用Animation的构造方法创建Animation对象;

1

2

<code><code><code>Animation animation = (Animation) AnimationUtils.loadAnimation(

                  Animation2Activity.this, R.anim.list_anim);</code></code></code>

3)创建LayoutAnimationController对象:

1

<code><code><code>LayoutAnimationController controller = new LayoutAnimationController(animation); </code></code></code>

4)设置控件的显示顺序以及延迟时间

1

2

<code><code><code>controller.setOrder(LayoutAnimationController.ORDER_NORMAL);

controller.setDelay(0.5f);   </code></code></code>

5)为ListView设置LayoutAnimationController属性:

1

<code><code><code>listView.setLayoutAnimation(controller);</code></code></code>

完整代码:

1

2

3

4

6

7

8

9

10

11

12

<code><code><code>private class ButtonListener implementsOnClickListener {

       public void onClick(View v) {

           listView.setAdapter(createListAdapter());

           Animation animation = (Animation) AnimationUtils.loadAnimation(

                  Animation2Activity.this, R.anim.list_anim);

 

           LayoutAnimationController controller = new LayoutAnimationController(animation);

           controller.setOrder(LayoutAnimationController.ORDER_NORMAL);

           controller.setDelay(0.5f);

           listView.setLayoutAnimation(controller);

       }

    }</code></code></code>

AnimationListener
1、什么是AnimationListener
1).AnimationListener是一个监听器,该监听器在动画执行的各个阶段会得到通知,从而调用相应的方法;
2).AnimationListener主要包括如下三个方法:
n ·onAnimationEnd(Animation animation) - 当动画结束时调用
n ·onAnimationRepeat(Animation animation) - 当动画重复时调用
n ·onAniamtionStart(Animation animation) - 当动画启动时调用
2、具体实现
1)main.xml

1

2

3

4

<code><code><code><!--?xml version="1.0" encoding="utf-8"?-->

<relativelayout android:id="@+id/layout" android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android=""><button android:id="@+id/addButton" android:layout_alignparentbottom="true" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="添加图片"></button><button android:id="@+id/deleteButton" android:layout_above="@id/addButton" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="删除图片">

    <imageview android:id="@+id/image" android:layout_centerinparent="true" android:layout_height="wrap_content" android:layout_margintop="100dip" android:layout_width="wrap_content" android:src="@drawable/an">

</imageview></button></relativelayout></code></code></code>

2).java文件

1

2

3

4

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

三十

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

<code><code><code>public class Animation2Activity extends Activity {

    private Button addButton = null;

    private Button deleteButton = null;

    private ImageView imageView = null;

    private ViewGroup viewGroup = null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        addButton = (Button)findViewById(R.id.addButton);

        deleteButton = (Button)findViewById(R.id.deleteButton);

        imageView = (ImageView)findViewById(R.id.image);

        //LinearLayout下的一组控件

        viewGroup = (ViewGroup)findViewById(R.id.layout);

        addButton.setOnClickListener(newAddButtonListener());

        deleteButton.setOnClickListener(newDeleteButtonListener());

    }

    private class AddButtonListener implements OnClickListener{

       public void onClick(View v) {

           //淡入

           AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);

           animation.setDuration(1000);

           animation.setStartOffset(500);

           //创建一个新的ImageView

           ImageView newImageView = new ImageView(

              Animation2Activity.this);

           newImageView.setImageResource(R.drawable.an);

           viewGroup.addView(newImageView,

              new LayoutParams(

                  LayoutParams.FILL_PARENT,

                  LayoutParams.WRAP_CONTENT));

           newImageView.startAnimation(animation);

       }

    }

    private class DeleteButtonListener implements OnClickListener{

       public void onClick(View v) {

           //淡出

           AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);

           animation.setDuration(1000);

           animation.setStartOffset(500);

           //为Aniamtion对象设置监听器

           animation.setAnimationListener(

              new RemoveAnimationListener());

           imageView.startAnimation(animation);

       }

    }

    private class RemoveAnimationListener implements AnimationListener{

       //动画效果执行完时remove

       public void onAnimationEnd(Animation animation) {

           System.out.println("onAnimationEnd");

           viewGroup.removeView(imageView);

       }

       public void onAnimationRepeat(Animation animation) {

           System.out.println("onAnimationRepeat");

       }

       public void onAnimationStart(Animation animation) {

           System.out.println("onAnimationStart");

       }

    }

}</code></code></code>

3、总结一下
可以在Activity中动态添加和删除控件,方法是:
1)取到那个Layout

1

<code><code><code>viewGroup = (ViewGroup)findViewById(R.id.layout);</code></code></code>

2)添加时,先创建对象,然后添加

1

2

3

4

6

7

<code><code><code>ImageView newImageView = new ImageView(

              Animation2Activity.this);

newImageView.setImageResource(R.drawable.an);

viewGroup.addView(newImageView,

              new LayoutParams(

                  LayoutParams.FILL_PARENT,

                  LayoutParams.WRAP_CONTENT));</code></code></code>

3)删除时,直接删除。

更多推荐

Android动画 很详细

本文发布于:2024-02-13 01:00:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1690083.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:动画   详细   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!