set animation in android?
In my application i use the single Activity.
Here is my code
import android开发者_如何学C.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class Animation2 extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Show1();
}
private void Show1() {
LinearLayout linLay = new LinearLayout(this);
linLay.setBackgroundColor(Color.RED);
TextView tv = new TextView(this);
tv.setText("This is form 1");
Button btn = new Button(this);
btn.setText("Go to Form 2");
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Show2();
}
});
linLay.addView(tv);
linLay.addView(btn);
Animation animation=new TranslateAnimation(0,0,Animation.ZORDER_NORMAL, 300);
animation.setDuration(1500);
linLay.setAnimation(animation);
setContentView(linLay);
}
protected void Show2() {
LinearLayout linLay = new LinearLayout(this);
linLay.setBackgroundColor(Color.GRAY);
TextView tv = new TextView(this);
tv.setText("This is form 2");
Button btn = new Button(this);
btn.setText("Back to form 1");
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Show1();
}
});
linLay.addView(tv);
linLay.addView(btn);
Animation animation=new TranslateAnimation(0,0,Animation.ZORDER_NORMAL, 300);
animation.setDuration(1500);
linLay.setAnimation(animation);
setContentView(linLay);
}
}
when i call the function Show1() and show2() i wish to show the animation like a intent action.How to do this?
Try linLay.startAnimation(animation);
instead of linLay.setAnimation(animation);
.
精彩评论