Java乔晓松-android中的帧动画FrameByFrame

时间:2022-06-01 05:25:38
先看效果后上代码:

动画开始----

Java乔晓松-android中的帧动画FrameByFrame

动画切换的界面----

Java乔晓松-android中的帧动画FrameByFrame

动画播放完毕后的跳转界面-----

Java乔晓松-android中的帧动画FrameByFrame

重要的方法:

                  imageView.setBackgroundResource(R.anim.framebyframe);

		animationDrawable = (AnimationDrawable) imageView.getBackground();
// 设置是否循环播放,false是循环播放,true只播放一遍
// animationDrawable.setOneShot(false);
animationDrawable.start();
//animationDrawable.setOneShot(false);是否循环播放
  //animationDrawable.stop();停止播放
  //animationDrawable.isRunning();//是否播放
  //animationDrawable.getNumberOfFrames();//播放帧
  //animationDrawable.getFrame(index); 返回制定索引的 Drawable对象
  //animationDrawable.getDuration(i);停留的时间

项目的开发结构:

Java乔晓松-android中的帧动画FrameByFrame

完整代码:

SplashActivity.java源码:

package com.example.lesson18_framebyframe;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.widget.ImageView; /**
* 2013-6-27 下午3:41:20
*
* @author 乔晓松
*/
public class SplashActivity extends Activity { private ImageView imageView;
private AnimationDrawable animationDrawable; @SuppressLint("HandlerLeak")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setBackgroundResource(R.anim.framebyframe); animationDrawable = (AnimationDrawable) imageView.getBackground();
// 设置是否循环播放,false是循环播放,true只播放一遍
// animationDrawable.setOneShot(false);
animationDrawable.start();
new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
if (msg.what == 1) {
Intent intent = new Intent(SplashActivity.this,
MainActivity.class);
startActivity(intent); SplashActivity.this.finish();
}
}
}.sendEmptyMessageDelayed(1, 3000);
// this.finish();
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

framebyframe.xml源码

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true" > <item
android:drawable="@drawable/span"
android:duration="1000"/>
<item
android:drawable="@drawable/span1"
android:duration="1000"/>
<item
android:drawable="@drawable/psbpan"
android:duration="1000"/>
<item
android:drawable="@drawable/ic_launcher"
android:duration="1000"/> </animation-list>

MainActivity.java源码:

package com.example.lesson18_framebyframe;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView; /**
* 2013-6-27 下午3:46:39
*
* @author 乔晓松
*/
public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TextView tv = new TextView(this);
tv.setText("主页面");
this.setContentView(tv);
}
}

layout-main.xml布局文件:

<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=".MainActivity" > <ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
tools:ignore="ContentDescription" /> </RelativeLayout>

清单文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lesson18_framebyframe"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.lesson18_framebyframe.SplashActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.example.lesson18_framebyframe.MainActivity" >
</activity>
</application> </manifest>