Android AnimationDrawable动画与APP启动、加载引导页面(画面)

AnimationDrawable是Android的Frame动画,可以简单的认为此AnimationDrawable能够将一系列资源图片加载成“电影”一样播放。当下,在一些APP中,往往需要在APP切入主题之前加载一些引导页面(宣传海报、装饰画面、赞助商广告等等),这些内容要两个共同特点:

(1)不是APP的重点内容,而仅仅只是像展台一样展览一些画面。

(2)前、当前、后页面基本上无特别需要处理的交互设计(因为只是播放几张或几帧画面而已),然后就跳转进入APP实质内容。

现在就给出一个示例小demo,实现一个简单的样例:一个APP的MainActivity启动后,循环播放3张图片。

(第1步)先在res/drawable目录下事先放置好需要播放的图片资源。如图:

142d9e27de2c2c7342a86c4ed008c7fa.png

(第2步)在res/anim目录下建立animation-list的xml代码文件,假设这个代码文件就叫做my_animation.xml,如图,

2b686918d962cd51cac52f03e9cc5b46.png

该文件的具体代码内容:

android:drawable="@drawable/p1"

android:duration="2000"/>

android:drawable="@drawable/p2"

android:duration="2000"/>

android:drawable="@drawable/p3"

android:duration="2000"/>

Item的资源图片p1,p2,p3将顺序播放。android:duration的值(毫秒)是该item播放持续的时间,在这个例子中,就是将图片显示一段时间。

(第3步)在Activity加载的布局文件activity_main.xml里面写一个ImageView,设置该ImageView的android:src= 第2步创建好的my_animation.xml。

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/play"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/play" />

android:id="@+id/stop"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/stop" />

android:id="@+id/imegaView"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:src="@anim/my_animation" />

到这里,就完成了设计目的,这个Activity启动后,就会在ImageView里面自带播放动画,当然,可以做的更多,为这个动画添加一些控制按钮(play,stop),控制播放(play)或者停止(stop)。

测试的activity:MainActivity.java:

package zhangphil.animation;

import android.support.v7.app.ActionBarActivity;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

import android.graphics.drawable.AnimationDrawable;

import android.os.Bundle;

public class MainActivity extends ActionBarActivity {

privateAnimationDrawable mAnimationDrawable=null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

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

mAnimationDrawable=(AnimationDrawable) imageView.getDrawable();

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

play.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

mAnimationDrawable.start();

}

});

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

stop.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

mAnimationDrawable.stop();

}

});

}

}

版权声明:本文为博主原创文章,未经博主允许不得转载。转载请注明出处:http://blog.csdn.net/zhangphil

原文:http://blog.csdn.net/zhangphil/article/details/47416915

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐