1.布局文件
<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:orientation="vertical"
android:background="@drawable/rocket_bg"
>
<ImageView
android:id="@+id/iv_num"
android:visibility="gone"
android:layout_height="50dp"
android:layout_width="80dp"
android:layout_gravity="center"
android:background="@drawable/num_anim"
/>
<Button
android:onClick="load"
android:layout_width="50dp"
android:layout_height="30dp"
android:layout_gravity="right"
android:text="装载"
android:layout_marginTop="150px"
android:background="@drawable/btn_normal"
></Button>
<Button
android:onClick="fire"
android:layout_width="50dp"
android:layout_height="30dp"
android:layout_gravity="right"
android:text="点火"
android:layout_marginTop="30px"
android:background="@drawable/btn_normal"
></Button>
<Button
android:onClick="send"
android:layout_width="50dp"
android:layout_height="30dp"
android:layout_gravity="right"
android:text="发射"
android:layout_marginTop="30px"
android:background="@drawable/btn_normal"
></Button>
<ImageView
android:visibility="gone"
android:id="@+id/iv_01"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="-30px"
android:class="lazyload" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB/AAffA0nNPuCLAAAAAElFTkSuQmCC" data-original="@drawable/rocket2"
android:background="@drawable/fire_anim"
/>
</LinearLayout>
3.帧动画xml文件
3.1 num_main.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true"
>
<item
android:drawable="@drawable/number5" android:duration="1000"
></item>
<item
android:drawable="@drawable/number4" android:duration="1000"
></item>
<item
android:drawable="@drawable/number3" android:duration="1000"
></item>
<item
android:drawable="@drawable/number2" android:duration="1000"
></item>
<item
android:drawable="@drawable/number1" android:duration="1000"
></item>
</animation-list>
3.2 fire_main.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false"
>
<item
android:drawable="@drawable/rocket1" android:duration="100"
></item>
<item
android:drawable="@drawable/rocket2" android:duration="100"
></item>
<item
android:drawable="@drawable/rocket3" android:duration="100"
></item>
<item
android:drawable="@drawable/rocket4" android:duration="100"
></item>
<item
android:drawable="@drawable/rocket5" android:duration="100"
></item>
<item
android:drawable="@drawable/rocket6" android:duration="100"
></item>
</animation-list>
2.MainActivity.java
package com.example.day016_ex01;
import android.os.AsyncTask;
import android.os.Bundle;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.AsyncTaskLoader;
import android.graphics.drawable.AnimationDrawable;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
static int load=0,fire=1,send=2;
//记录倒计时的时间
int time=0;
ImageView iv_fire,iv_num;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_fire=(ImageView) findViewById(R.id.iv_01);
iv_num=(ImageView) findViewById(R.id.iv_num);
}
public void load(View v){
//装载
iv_fire.setVisibility(View.VISIBLE);
load++;
}
public void fire(View v){
AnimationDrawable num=(AnimationDrawable) iv_num.getBackground();
if(load==fire){
iv_num.setVisibility(View.VISIBLE);
//点火倒计时
num.start();
int count=num.getNumberOfFrames();
for(int i=0;i<count;i++){
time+=num.getDuration(i);
}
new MyTask().execute();
}else{
Toast.makeText(this,"您还没有加载火箭呢!",0).show();
}
}
@SuppressLint("NewApi")
public void send(View v){
if(load==1&&fire==send){
ObjectAnimator tranX=ObjectAnimator.ofFloat
(iv_fire,"translationY",0,-400);
tranX.setDuration(2000);
ObjectAnimator scaleX=ObjectAnimator.ofFloat
(iv_fire,"scaleX",1.0f,0.5f);
ObjectAnimator scaleY=ObjectAnimator.ofFloat
(iv_fire,"scaleY",1.0f,0.5f);
scaleX.setDuration(2000);
scaleY.setDuration(2000);
AnimatorSet set=new AnimatorSet();
set.playTogether(tranX,scaleX,scaleY);
set.start();
}else{
Toast.makeText(this,"您加载火箭了吗?点火了吗?", 0).show();
}
}
private class MyTask extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
iv_num.setVisibility(View.GONE);
fire++;
AnimationDrawable fire=(AnimationDrawable)
iv_fire.getBackground();
//点火启动
fire.start();
}
}
}
共同学习,写下你的评论
评论加载中...
作者其他优质文章