Android Media and Animation 5

กดปุ่ม Alpha ยิงจรวดขึ้นข้างบน

MainAcitivity3.java

package com.example.myapp3;
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.ImageView;

public class MainActivity extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btAlpha=(Button)findViewById(R.id.button1);
btAlpha.setOnClickListener(this);
Button btFire=(Button)findViewById(R.id.button2);
btFire.setOnClickListener(this);
}
@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;
}

@Override
public void onClick(View v) {
final ImageView iv=(ImageView)findViewById(R.id.imageView1);
switch(v.getId())
{
case R.id.button1:
Animation a=AnimationUtils.loadAnimation(this,R.anim.rocket_alpha);
a.setAnimationListener(new AnimationListener() {

@Override
public void onAnimationStart(Animation arg0) {
iv.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
}
});
iv.startAnimation(a);
break;
case R.id.button2:
Animation a2 = AnimationUtils.loadAnimation(this,R.anim.rocket_move);
iv.startAnimation(a2);
break;
}
}
}
-----------------------
Rocket_move.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="0%p" android:toYDelta="-100%p"
android:duration="2000" />
</set>

 

Picture 2014-03-08 14_47_05 Picture 2014-03-08 14_47_33 Picture 2014-03-08 14_47_51