package com.example.activity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
private Button startBtn;
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
this.init();
}
private void init() {
this.startBtn = (Button) this.findViewById(R.id.startBtn);
this.view = this.findViewById(R.id.view);
this.startBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//创建补间动画
Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.t2);
animation1.setFillAfter(true);//停留在结束状态,不回来了, false会回到开始状态
//让view开始动画
view.startAnimation(animation1);
animation1.setAnimationListener(new AnimationListener() {
//动画开始事件
@Override
public void onAnimationStart(Animation animation) {
Toast.makeText(MainActivity.this, "开始动了", 1).show();
}
//重得执行动画事件
@Override
public void onAnimationRepeat(Animation animation) {
}
//动画结束事件
@Override
public void onAnimationEnd(Animation animation) {
Toast.makeText(MainActivity.this, "结束动了", 1).show();
}
});
}
}
anim>t.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<scale
android:duration="4000"
android:fromXScale="0.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="0"
android:toXScale="1.0"
android:toYScale="1.0" />
<rotate
android:duration="2000"
android:fromDegrees="0"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="4000"
android:toDegrees="720" >
</rotate>
<rotate
android:duration="2000"
android:fromDegrees="360"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="6000"
android:toDegrees="0" >
</rotate>
<alpha
android:duration="8000"
android:fromAlpha="1"
android:repeatCount="1"
android:repeatMode="reverse"
android:startOffset="0"
android:toAlpha="0" />
<translate
android:startOffset="16000"
android:duration="2000"
android:fillAfter="false"
android:fromXDelta="0"
android:fromYDelta="0"
android:repeatCount="1"
android:repeatMode="reverse"
android:toXDelta="50%p"
android:toYDelta="0" >
</translate>
</set>
anim>t2.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >
<translate
android:startOffset="0"
android:duration="2000"
android:fillAfter="true"
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="50%p"
android:toYDelta="0" >
</translate>
</set>
<!--
android:repeatCount="1"
android:repeatMode="reverse"
-->
layout>activity_main.xml
<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"
tools:context=".MainActivity"
android:orientation="vertical"
>
<View
android:id="@+id/view"
android:layout_width="200dp"
android:layout_height="200dp"
android:background="#f0f0"
/>
<Button
android:id="@+id/startBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="开始" />
</LinearLayout>
炸月球小项目
com.train.rock>StartActivity.jab
package com.train.rock;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
public class StartActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
final Button button1 = (Button) findViewById(R.id.button1);
final Button button2 = (Button) findViewById(R.id.button2);
Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.translate_left);
Animation animation2 = AnimationUtils.loadAnimation(this, R.anim.translate_right);
button1.startAnimation(animation1);
button2.startAnimation(animation2);
animation1.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation arg0) {//开始
}
@Override
public void onAnimationRepeat(Animation arg0) {//重新
}
@Override
public void onAnimationEnd(Animation arg0) {//结束
//内部类方法只能外部方法的常量, 不能访问外部方法的变量
// 隐藏两个Button的展示
//View.GONE:隐藏,并且不占空间; View.VISIBLE:显示; View.INVISIBLE:隐藏,但是占空间
button1.setVisibility(View.GONE);
button2.setVisibility(View.GONE);
// 打开PK主界面
Intent intent = new Intent(StartActivity.this, MainActivity.class);
startActivity(intent);
// // 关闭当前Activity
StartActivity.this.finish();
}
});
}
}
com.train.rock>MainActivity.java
package com.train.rock;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ImageView rocket=null;
private ImageView bomb=null;
private Button button_fire=null;
private Button button_send=null;
private AnimationDrawable drawable=null;//火箭帧动画
private AnimationDrawable drawable1=null;//帧动画
private Animation animation=null;//补间动画
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rocket = (ImageView) findViewById(R.id.rocket);//火箭
bomb=(ImageView) findViewById(R.id.bomb);//炸弹
button_fire = (Button) findViewById(R.id.button_fire);//点火
button_send = (Button) findViewById(R.id.button_send);//发送
drawable = (AnimationDrawable) rocket.getBackground();//取出帧动画
drawable1=(AnimationDrawable) bomb.getBackground();//取出帧动画
//创建火箭使用的补间动画
animation = AnimationUtils.loadAnimation(this, R.anim.translate);
animation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
//动画结束后
rocket.setVisibility(View.GONE);//
drawable.setOneShot(false);
drawable1.start();//bomb 开始月球上帧动画
//回收火箭对象
drawable.stop();
drawable=null;
//控件不可点击
// button_fire.setEnabled(false);
// button_send.setEnabled(false);
//隐藏控件
button_fire.setVisibility(View.GONE);
button_send.setVisibility(View.GONE);
}
});
button_fire.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//启动
if(drawable.isRunning()){//判断火箭是否正在执行帧动画
Toast.makeText(getApplicationContext(),"已经点火,无需在点",Toast.LENGTH_SHORT).show();
}else{
drawable.setOneShot(false);//设置循环播放
drawable.start();//开始火箭帧动画
}
}
});
button_send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//发送
if(drawable.isRunning()){
rocket.startAnimation(animation);//开始火箭的补间动画
}else{
Toast.makeText(getApplicationContext(),"先点火,在发射",Toast.LENGTH_SHORT).show();
}
}
});
}
}
anim>translate_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="-50%p"
android:toYDelta="0"
android:duration="3000"/>
</set>
anim>translate_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="50%p"
android:toYDelta="0"
android:duration="3000"/>
</set>
anim>translate.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<translate
android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="0"
android:interpolator="@android:anim/accelerate_interpolator"
android:toYDelta="-100%p"
android:duration="3000"/>
</set>
炸月球图片
drawable>bomb_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/bomb1"
android:duration="300"/>
<item
android:drawable="@drawable/bomb2"
android:duration="300"/>
<item
android:drawable="@drawable/bomb3"
android:duration="300"/>
<item
android:drawable="@drawable/bomb4"
android:duration="300"/>
<item
android:drawable="@drawable/bomb5"
android:duration="300"/>
<item
android:drawable="@drawable/bomb6"
android:duration="300"/>
<item
android:drawable="@drawable/bomb7"
android:duration="300"/>
<item
android:drawable="@drawable/bomb8"
android:duration="300"/>
<item
android:drawable="@drawable/bomb9"
android:duration="300"/>
<item
android:drawable="@drawable/bomb10"
android:duration="300"/>
<item
android:drawable="@drawable/chenge1"
android:duration="300"/>
<item
android:drawable="@drawable/chenge2"
android:duration="300"/>
<item
android:drawable="@drawable/chenge3"
android:duration="300"/>
<item
android:drawable="@drawable/chenge4"
android:duration="300"/>
<item
android:drawable="@drawable/chenge5"
android:duration="300"/>
<item
android:drawable="@drawable/chenge6"
android:duration="300"/>
<item
android:drawable="@drawable/chenge7"
android:duration="300"/>
<item
android:drawable="@drawable/chenge8"
android:duration="300"/>
<item
android:drawable="@drawable/chenge9"
android:duration="300"/>
<item
android:drawable="@drawable/chenge10"
android:duration="300"/>
<item
android:drawable="@drawable/chenge11"
android:duration="300"/>
</animation-list>
drawable>rocket_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/rocket_launch_1"
android:duration="200"/>
<item
android:drawable="@drawable/rocket_launch_2"
android:duration="200"/>
</animation-list>
layout>activity_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:background="@drawable/bg"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/rocket"
android:layout_width="@dimen/activity_horizontal_margin"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="@drawable/rocket_bg" />
<ImageView
android:id="@+id/bomb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
android:background="@drawable/bomb_bg"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
<Button
android:id="@+id/button_fire"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点火" />
<Button
android:id="@+id/button_send"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="发射"
/>
</LinearLayout>
</RelativeLayout>
layout>start.xml
<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="@drawable/bg"
tools:context=".MainActivity"
>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/left"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@drawable/right"/>
</LinearLayout>