实现逐帧动画
实现补间动画
一实现逐帧动画
新建android项目
新建layout布局
<LinearLayout 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:background="@animator/fox"
android:id="@+id/main_rl"
tools:context=".MainActivity" >
<ImageView
android:layout_width="match_parent"
android:layout_height="308dp"
android:src="@drawable/ic_launcher" />
</LinearLayout>
在资源res文件下新建动画文件animator代码如下同时在res文件下drawable-hdpi放入照片
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/a1" android:duration="100" />
<item android:drawable="@drawable/a2" android:duration="100" />
<item android:drawable="@drawable/a3" android:duration="100" />
<item android:drawable="@drawable/a4" android:duration="100" />
<item android:drawable="@drawable/a5" android:duration="100" />
<item android:drawable="@drawable/a6" android:duration="100" />
<item android:drawable="@drawable/a7" android:duration="100" />
</animation-list>
新建activity活动java类
package com.example.android_lession9_1;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class MainActivity extends Activity {
private LinearLayout rl;
boolean flag=false;
private MyHandler my;
AnimationDrawable ani;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rl=(LinearLayout) findViewById(R.id.main_rl);
my=new MyHandler();
ani=(AnimationDrawable) rl.getBackground();
// rl.setOnClickListener(new OnClickListener() {
//
// @Override
// public void onClick(View v) {
// //启动动画
// //获取动画文件
// AnimationDrawable ani=(AnimationDrawable) rl.getBackground();
// flag=!flag;
// if(flag){
// ani.start();
// }else{
// ani.stop();
// }
//
//
// }
// });
// ani.start();
new Thread(new Runnable() {
@Override
public void run() {
my.sendEmptyMessage(0);
}
}).start();
}
@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;
}
public class MyHandler extends Handler{
@Override
public void handleMessage(Message msg) {
ani.start();
super.handleMessage(msg);
}
}
}