import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private TextView times;
private Button buttons;
private MyHandler handler = new MyHandler();
int time = 3;
private AnimationSet animationSet;
private RelativeLayout image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找控件
initView();
// 倒计时
startTime();
// 动画
startAnimation();
}
private void startAnimation() {
animationSet = new AnimationSet(false);
AlphaAnimation aa = new AlphaAnimation(0, 100);
TranslateAnimation ta = new TranslateAnimation(0, 360, 0, 0);
animationSet.addAnimation(aa);
animationSet.setDuration(3000);
animationSet.setFillAfter(true);
image.startAnimation(animationSet);
}
private void startTime() {
// TODO Auto-generated method stub
// 继续发送消息
handler.sendEmptyMessageDelayed(0, 1000);
}
private void initView() {
image = (RelativeLayout) findViewById(R.id.image);
times = (TextView) findViewById(R.id.times);
buttons = (Button) findViewById(R.id.buttons);
// 点击跳转
buttons.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
handler.removeCallbacksAndMessages(null);
startActivity(new Intent(MainActivity.this, TwoActivity.class));
finish();
}
class MyHandler extends Handler {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
time--;
times.setText(time + "S");
if (time == 0) {
// 停止发送消息
handler.removeCallbacksAndMessages(null);
// 跳转页面
startActivity(new Intent(MainActivity.this, TwoActivity.class));
finish();
} else {
// 继续发送消息
handler.sendEmptyMessageDelayed(0, 1000);
}
}
}
}
该博客介绍了如何在Android中实现倒计时以及动画效果。通过创建一个自定义Handler,实现了从3秒开始的倒计时,并在倒计时结束后跳转到新的Activity。同时,使用AlphaAnimation和TranslateAnimation组合成的AnimationSet为ImageView添加了动画效果。
770

被折叠的 条评论
为什么被折叠?



