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

余生颓废 提交于 2020-01-15 04:18:51


在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();
		}

	}
}


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

 

源码下载:

点击打开链接

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!