Android常用动画Frame-By-Frame Animations的使用


在Android的动画中有一种叫做Frame by Frame 的动画效果,就是跟Flash播放一样,是一帧一帧地显示,如果动画是连续并且有规律的话,就跟播放视频一样。

首先在drawable目录下添加anim_nv.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/psb1"
        android:duration="500"/>
    <item android:drawable="@drawable/psb2"
        android:duration="500"/>
    <item android:drawable="@drawable/psb3"
        android:duration="500"/>
    <item android:drawable="@drawable/psb4"
        android:duration="500"/>
	
</animation-list>


布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button 
        android:id="@+id/alpha"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="动画效果演示"/>
    
    <ImageView 
        android:id="@+id/image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dip"/>

</LinearLayout>


在Activity中

package com.example.animation;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

import com.example.widgetdemo.R;

public class AnimationXmlDemo3 extends Activity {
	private Button alpha = null;
	private ImageView image = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.animation_xml3);
		alpha = (Button) findViewById(R.id.alpha);
		image = (ImageView) findViewById(R.id.image);

		alpha.setOnClickListener(new alphaListener());
	}

	class alphaListener implements OnClickListener {

		@Override
		public void onClick(View arg0) {
			// TODO Auto-generated method stub
			//为图片添加图片背景
			image.setBackgroundResource(R.drawable.anim_nv);  
			AnimationDrawable animationDrawable = (AnimationDrawable) image
					.getBackground();
			animationDrawable.start();
		}

	}
}


这样一个连续显示图片的效果就完成了。

源码下载:

点击打开链接

原文地址:https://www.cnblogs.com/james1207/p/3297121.html