加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

在XML中定义逐帧动画

发布时间:2020-12-15 23:38:58 所属栏目:百科 来源:网络整理
导读:逐帧(Frame)是最容易理解的动画,它要求开发者把动画过程的每张静态图片都收集起来,然后由Android来控制依次显示这些静态图片,然后利用人眼“视觉暂留”原理,给用户造成“动画”的错觉。逐帧动画的动画原理与放电影的原理一样。 下面使用在XML中定义逐

逐帧(Frame)是最容易理解的动画,它要求开发者把动画过程的每张静态图片都收集起来,然后由Android来控制依次显示这些静态图片,然后利用人眼“视觉暂留”原理,给用户造成“动画”的错觉。逐帧动画的动画原理与放电影的原理一样。

下面使用在XML中定义逐帧动画的每一帧,代码如下:

Activity:

package com.lovo.frameanim;

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;

public class MainActivity extends Activity implements OnClickListener {
	private Button startBtn;
	private Button stopBtn;
	private ImageView image;
	// 声明AnimationDrawable对象
	private AnimationDrawable animationDrawable;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		startBtn = (Button) findViewById(R.id.main_btn_start);
		stopBtn = (Button) findViewById(R.id.main_btn_stop);
		startBtn.setOnClickListener(this);
		stopBtn.setOnClickListener(this);
		image = (ImageView) findViewById(R.id.main_image);
		// 从image中获取AnimationDrawable对象
		animationDrawable = (AnimationDrawable) image.getBackground();
	}

	@Override
	public void onClick(View v) {
		if (v == startBtn) {
			// 启动动画
			animationDrawable.start();
		}
		if (v == stopBtn) {
			// 停止动画
			animationDrawable.stop();
		}
	}

}

动画XML(freedom_frame_anim.xml):

<?xml version="1.0" encoding="utf-8"?>
<!-- android:oneshot:是否循环;true-不循环,false-循环 -->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/freedom1" android:duration="50"></item>
    <item android:drawable="@drawable/freedom2" android:duration="50"></item>
    <item android:drawable="@drawable/freedom3" android:duration="50"></item>
    <item android:drawable="@drawable/freedom4" android:duration="50"></item>
    <item android:drawable="@drawable/freedom5" android:duration="50"></item>
    <item android:drawable="@drawable/freedom6" android:duration="50"></item>
    <item android:drawable="@drawable/freedom7" android:duration="50"></item>
    <item android:drawable="@drawable/freedom8" android:duration="50"></item>
</animation-list>

Activity布局XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/main_btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始动画" />

    <Button
        android:id="@+id/main_btn_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="停止动画" />

    <ImageView
        android:id="@+id/main_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@anim/freedom_frame_anim" />

</LinearLayout>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读