Frame动画完整代码示例:" />
当前位置: 代码迷 >> Android >> Frame动画完整代码示例:
  详细解决方案

Frame动画完整代码示例:

热度:55   发布时间:2016-04-28 06:13:22.0
Android下的动画处理

动画(Animation)

Android提供了2种动画:

1> Tween动画

通过对 View 的内容进行一系列的图形变换 (包括平移、缩放、旋转、改变透明度)来实现动画效果。动画效果的定义可以采用XML来做也可以采用编码来做。Tween动画有4种类型:


本例要实现对ImageView对象进行渐变尺寸缩放动画效果
1> 在项目的res目录下创建文件夹anim,然后在anim文件夹下面定义动画XML文件,文件名称可以自定义,如:scale.xml,内容如下:
<?xml version="1.0" encoding="utf-8"?>
 <set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
        android:fromXScale="0.0" 
        android:fromYScale="0.0"  
        android:toXScale="5" 
        android:toYScale="5" 
        android:pivotX="50%" 
        android:pivotY="50%" 
        android:fillAfter="false" 
        android:duration="5000"
        /> 
</set>
动画的进度使用interpolator控制,android提供了几个Interpolator 子类,实现了不同的速度曲线,如LinearInterpolator实现了匀速效果、Accelerateinterpolator实现了加速效果、DecelerateInterpolator实现了减速效果等。还可以定义自己的Interpolator子类,实现抛物线、自由落体等物理效果。

fromXScale(浮点型) 属性为动画起始时X坐标上的缩放尺寸 
fromYScale(浮点型) 属性为动画起始时Y坐标上的缩放尺寸
toXScale(浮点型)   属性为动画结束时X坐标上的缩放尺寸
toYScale(浮点型)   属性为动画结束时Y坐标上的缩放尺寸
说明: 以上四种属性值 
0.0表示收缩到没有 
1.0表示正常无缩放
值小于1.0表示收缩 
值大于1.0表示放大
pivotX(浮点型)     属性为动画相对于物件的X坐标的开始位置 
pivotY(浮点型)     属性为动画相对于物件的Y坐标的开始位置 
说明: 
以上两个属性值 从0%-100%中取值
50%为物件的X或Y方向坐标上的中点位置
duration(长整型)属性为动画持续时间 。说明:   时间以毫秒为单位
fillAfter(布尔型)属性当设置为true,该动画转化在动画结束后被应用


2> 在layout文件添加<ImageView>节点:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<ImageView
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@drawable/icon"
   android:id="@+id/imageView"
   />
</LinearLayout>
说明:除了可以对<ImageView>实现动画效果,其实也可以对其他View实现动画效果,如:<TextView>


3>在Activity里对ImageView使用前面定义好的动画效果:
public class AnimationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imageView = (ImageView)this.findViewById(R.id.imageView);
//加载动画XML文件,生成动画指令
Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale);
//开始执行动画
imageView.startAnimation(animation);
}
}


备注:上面采用的是xml文件定义动画效果,作为代替,也可以采用编码方式实现。下面采用编码方式实现上述例子同样的效果:
public class AnimationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imageView = (ImageView)this.findViewById(R.id.imageView);
ScaleAnimation animation = new ScaleAnimation(0.0f, 5f, 0.0f, 5f, 
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(5000); //设置持续时间5秒
imageView.startAnimation(animation);
}
}




其他动画效果定义例子:
=================渐变透明度动画效果======================
<?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="3000"
/>
</set>
编码实现透明度动画效果:
public class AnimationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imageView = (ImageView)this.findViewById(R.id.imageView);
AlphaAnimation animation = new AlphaAnimation(0.1, 1.0); 
animation.setDuration(5000); //设置持续时间5秒
imageView.startAnimation(animation);
}
}


=================画面位置移动动画效果======================
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:repeatCount="2"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="120"
android:toYDelta="120"
android:duration="3000"
/>
<!-- fromXDelta fromYDelta 为动画起始时 X和Y坐标上的位置
toXDelta toYDelta为动画结束起始时  X和Y坐标上的位置
 -->
</set>


编码实现位置移动动画效果:
public class AnimationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imageView = (ImageView)this.findViewById(R.id.imageView);
TranslateAnimation animation = new TranslateAnimation(0, 120, 0, 120); 
animation.setDuration(5000); //设置持续时间5秒
imageView.startAnimation(animation);
}
}


=================画面旋转动画效果======================
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:interpolator="@android:anim/accelerate_interpolator"
android:repeatCount="2"
android:fromDegrees="0"
android:toDegrees="+360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="3000"
/>
<!-- 
repeatCount 重复次数
fromDegrees为动画起始时物件的角度:
当角度为负数——表示逆时针旋转
当角度为正数——表示顺时针旋转
(负数fromDegrees——toDegrees正数:顺时针旋转)
(负数fromDegrees——toDegrees负数:逆时针旋转)
(正数fromDegrees——toDegrees正数:顺时针旋转)
(正数fromDegrees——toDegrees负数:逆时针旋转)
toDegrees属性为动画结束时物件旋转的角度 可以大于360度
pivotX,pivotY  为动画相对于物件的X、Y坐标的开始位.说明:以上两个属性值 从0%-100%中取值,50%为物件的X或Y方向坐标上的中点位置
 -->
</set>


编码实现:
RotateAnimation animation = new RotateAnimation(0, -90, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
animation.setDuration(500);
imageView.startAnimation(animation);

 Tween动画代码示例:

alpha.xml:透明度变化
<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="2000"    android:fromXDelta="0"    android:fromYDelta="0"    android:startOffset="1000"    android:toXDelta="100"    android:toYDelta="100" ></translate>
rotate.xml:旋转动画
<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="2000"    android:fromDegrees="0"    android:pivotX="50%p"    android:pivotY="50%"    android:toDegrees="1220" ></rotate>
scale.xml:缩放动画
<?xml version="1.0" encoding="utf-8"?><scale xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="2000"    android:fromXScale="0.0"    android:fromYScale="0.0"    android:repeatCount="1"    android:repeatMode="reverse"    android:toXScale="3.0"    android:toYScale="2.0" ></scale>
translate.xml:平移动画
<?xml version="1.0" encoding="utf-8"?><translate xmlns:android="http://schemas.android.com/apk/res/android"    android:duration="2000"    android:fromXDelta="0"    android:fromYDelta="0"    android:startOffset="1000"    android:toXDelta="100"    android:toYDelta="100" ></translate>
set.xml:动画集合
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <alpha        android:duration="2000"        android:fromAlpha="0.0"        android:repeatCount="0"        android:repeatMode="restart"        android:toAlpha="1.0" />    <rotate        android:duration="2000"        android:fromDegrees="0"        android:pivotX="50%"        android:pivotY="50%"        android:toDegrees="90" />    <scale        android:duration="2000"        android:fromXScale="0.0"        android:fromYScale="0.0"        android:repeatCount="4"        android:repeatMode="reverse"        android:toXScale="2.0"        android:toYScale="2.0" />    <translate        android:duration="2000"        android:fromXDelta="0"        android:fromYDelta="0"        android:startOffset="1000"        android:toXDelta="100"        android:toYDelta="100" >    </translate></set>
Demoactivity.java:
package cn.itcast.anim;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.animation.AlphaAnimation;import android.view.animation.Animation;import android.view.animation.AnimationSet;import android.view.animation.AnimationUtils;import android.view.animation.RotateAnimation;import android.view.animation.ScaleAnimation;import android.view.animation.TranslateAnimation;import android.widget.ImageView;public class DemoActivity extends Activity {	private ImageView iv;	@Override	public void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.main);		iv = (ImageView) this.findViewById(R.id.iv);	}	/**	 * 播放透明度变化的动画	 * 	 */	public void alpha(View view) {		// AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);		// aa.setDuration(2000);		Animation aa = AnimationUtils.loadAnimation(this, R.anim.alpha);		iv.startAnimation(aa);	}	/**	 * 播放选择变化的动画	 * 	 */	public void rotate(View view) {		// RotateAnimation ra = new RotateAnimation(0, 90);		// RotateAnimation ra = new RotateAnimation(0, 90,		// (iv.getRight()+iv.getLeft())/2, (iv.getTop()+iv.getBottom())/2);		// 怎么去定义旋转的中间位置?		// RotateAnimation ra = new RotateAnimation(0, 90, 0.5f, 0.5f);		// ra.setDuration(2000);		Animation ra = AnimationUtils.loadAnimation(this, R.anim.rotate);		iv.startAnimation(ra);	}	public void scale(View view) {		// ScaleAnimation sa = new ScaleAnimation(0.0f, 2.0f, 0.0f, 2.0f);		// sa.setDuration(2000);		Animation sa = AnimationUtils.loadAnimation(this, R.anim.scale);		iv.startAnimation(sa);	}	public void translate(View view) {		// TranslateAnimation ta = new TranslateAnimation(100, -200, 300, 100);		// ta.setDuration(2000);		Animation ta = AnimationUtils.loadAnimation(this, R.anim.translate);		iv.startAnimation(ta);	}	/**	 * 动画的组合	 */	public void set(View view) {		// AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);		// aa.setDuration(2000);		// RotateAnimation ra = new RotateAnimation(0, 90);		// ra.setDuration(2000);		// TranslateAnimation ta = new TranslateAnimation(0, 200, 0, 200);		// ta.setDuration(2000);		// AnimationSet set = new AnimationSet(false);		// set.addAnimation(ta);		// set.addAnimation(ra);		// set.addAnimation(aa);		// iv.startAnimation(set);		Animation aa = AnimationUtils.loadAnimation(this, R.anim.set);		iv.startAnimation(aa);	}}

main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:gravity="center"        android:orientation="horizontal" >        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="alpha"            android:text="透明度" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="rotate"            android:text="旋转" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="scale"            android:text="缩放" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="translate"            android:text="平移" />        <Button            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:onClick="set"            android:text="组合动画" />    </LinearLayout>    <LinearLayout        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:gravity="center_vertical|center_horizontal"        android:orientation="vertical" >        <ImageView            android:id="@+id/iv"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:scaleType="fitXY"            android:src="@drawable/ic_launcher" />    </LinearLayout></LinearLayout>

2> Frame动画,即顺序播放事先做好的图像,跟放胶片电影类似。开发步骤:

(1)把准备好的图片放进项目res/ drawable下。
(2)在项目的res目录下创建文件夹anim,然后在anim文件夹下面定义动画XML文件,文件名称可以自定义。当然也可以采用编码方式定义动画效果(使用AnimationDrawable类)。
(3)为View控件绑定动画效果。调用代表动画的AnimationDrawable的start()方法开始动画。



=====================  Frame动画例子  ===============================
(1)把准备好的图片放进项目res/ drawable下。
   图片有:girl_1.gif, girl_2.gif, girl_3.gif
(2)在项目的res目录下创建文件夹anim,然后在anim文件夹下面定义动画XML文件,文件名称可以自定义,如:frame.xml。
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/girl_1" android:duration="200" />
    <item android:drawable="@drawable/girl_2" android:duration="200" />
    <item android:drawable="@drawable/girl_3" android:duration="200" />
</animation-list>
上面的XML就定义了一个Frame动画,其包含3帧动画,3帧动画中分别应用了drawable中的3张图片:girl_1.gif, girl_2.gif, girl_3.gif,每帧动画持续200毫秒。android:oneshot属性如果为true,表示动画只播放一次停止在最后一帧上,如果设置为false表示动画循环播放。
(3)为View控件绑定动画效果,调用代表动画的AnimationDrawable的start()方法开始动画。
public class FrameActivity extends Activity {
private AnimationDrawable animationDrawable;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView imageView = (ImageView)this.findViewById(R.id.imageView);
imageView.setBackgroundResource(R.anim.frame);
animationDrawable = (AnimationDrawable) imageView.getBackground();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
 if (event.getAction() == MotionEvent.ACTION_DOWN) {//按下
 animationDrawable.start();
 return true;
 }
 return super.onTouchEvent(event);
}
}
有一点需要强调的是:启动Frame动画的代码animationDrawable.start();不能应用在OnCreate()方法中,因为在OnCreate()中 AnimationDrawable还没有完全的与ImageView绑定。在OnCreate()中启动动画,只能看到第一张图片。这里在触摸事件中实现的。

Frame动画完整代码示例:

list.xml:

<?xml version="1.0" encoding="utf-8"?><animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="false" >    <item        android:drawable="@drawable/girl_1"        android:duration="100"/>    <item        android:drawable="@drawable/girl_2"        android:duration="100"/>    <item        android:drawable="@drawable/girl_3"        android:duration="100"/>    <item        android:drawable="@drawable/girl_4"        android:duration="100"/>    <item        android:drawable="@drawable/girl_5"        android:duration="100"/>    <item        android:drawable="@drawable/girl_6"        android:duration="100"/>    <item        android:drawable="@drawable/girl_7"        android:duration="150"/>    <item        android:drawable="@drawable/girl_8"        android:duration="100"/>    <item        android:drawable="@drawable/girl_9"        android:duration="200"/>    <item        android:drawable="@drawable/girl_10"        android:duration="200"/>    <item        android:drawable="@drawable/girl_11"        android:duration="200"/></animation-list>
FrameActivity.java:

package cn.itcast.frame;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.os.Bundle;import android.view.MotionEvent;import android.widget.ImageView;public class FrameActivity extends Activity {	private ImageView iv;	private AnimationDrawable drawable;	@Override	public void onCreate(Bundle savedInstanceState) {		super.onCreate(savedInstanceState);		setContentView(R.layout.main);		iv = (ImageView) this.findViewById(R.id.iv);		iv.setBackgroundResource(R.drawable.list);		drawable = (AnimationDrawable) iv.getBackground();	}	@Override	public boolean onTouchEvent(MotionEvent event) {		if (event.getAction() == MotionEvent.ACTION_DOWN) {			// 开始播放帧动画			drawable.start();		}		return super.onTouchEvent(event);	}}
main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <ImageView        android:id="@+id/iv"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout>

  相关解决方案