Android开发(25)--framebyframe帧动画并实现启动界面到主界面的跳转

来源:互联网 发布:我的淘宝没有数据分析 编辑:程序博客网 时间:2024/05/16 19:52

      Drawable animation可以加载Drawable资源实现帧动画。AnimationDrawable是实现Drawable animations的基本类。推荐用XML文件的方法实现Drawable动画,不推荐在代码中实现。这种XML文件存放在工程中res/drawable/目录下。XML文件的指令(即属性)为动画播放的顺序和时间间隔。

在XML文件中<animation-list>元素为根节点,<item>节点定义了每一帧,表示一个drawable资源的帧和帧间隔。此XML文件必须写在res资源文件目录下的anim文件夹下,

下面是一个XML文件的实例:

framebyframe.xml

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"    android:oneshot="false">    <item android:drawable="@drawable/m1" android:duration="1000" />    <item android:drawable="@drawable/m2" android:duration="1000" />    <item android:drawable="@drawable/m3" android:duration="1000" />    <item android:drawable="@drawable/m4" android:duration="1000" />    <item android:drawable="@drawable/m5" android:duration="1000" />    <item android:drawable="@drawable/m6" android:duration="1000" />    <item android:drawable="@drawable/m7" android:duration="1000" /></animation-list><!-- oneshot :是否只播放一遍动画 true 播放一遍 false 循环播放默认就是false -->


下面是StartActivity.java

package com.example.lesson18_framebyframe;import android.app.Activity;import android.content.Intent;import android.graphics.drawable.AnimationDrawable;import android.os.Bundle;import android.os.Handler;import android.view.Menu;import android.widget.ImageView;public class StartActivity extends Activity {private ImageView imageView;private AnimationDrawable animationDrawable;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_start);//帧动画  imageView = (ImageView) findViewById(R.id.imageView1);//第一种方式实现的动画/*animationDrawable = (AnimationDrawable) getResources().getDrawable(R.anim.framebyframe);imageView.setBackgroundDrawable(animationDrawable);*///第二种方式实现的动画imageView.setBackgroundResource(R.anim.framebyframe);animationDrawable = (AnimationDrawable) imageView.getBackground();animationDrawable.start();new Handler(){public void handleMessage(android.os.Message msg) {  if(msg.what==1){  Intent intent = new Intent(StartActivity.this,NextActivity.class);  startActivity(intent);  }};}.sendEmptyMessageDelayed(1, 7000);//animationDrawable.setOneShot(false);是否循环播放//animationDrawable.stop();停止播放//animationDrawable.isRunning();//是否播放//animationDrawable.getNumberOfFrames();//播放帧//animationDrawable.getFrame(index); 返回制定索引的 Drawable对象//animationDrawable.getDuration(i);停留的时间}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.start, menu);return true;}}


布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".StartActivity" > <ImageView     android:id="@+id/imageView1"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:layout_alignParentLeft="true" /></RelativeLayout>


NextActivity.java

package com.example.lesson18_framebyframe;import android.app.Activity;import android.os.Bundle;import android.widget.TextView;public class NextActivity extends Activity {protected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);TextView tv = new TextView(this);tv.setText("启动界面到主界面完成");setContentView(tv);}}

效果如下:

               

原创粉丝点击