直接上代码。
activity_ad.xml
adActivity.java
public class adActivity extends AppCompatActivity implements Animation.AnimationListener,Runnable {
Handler handler=new Handler(){//处理者
@Override
public void handleMessage(Message msg) {
TextView textView= (TextView) findViewById(R.id.adtextview);//初始化控件
switch (msg.what){//按时间自动逐秒递减
case 1:
textView.setText("广告倒计时:4秒");
break;
case 2:
textView.setText("广告倒计时:3秒");
break;
case 3:
textView.setText("广告倒计时:2秒");
break;
case 4:
textView.setText("广告倒计时:1秒");
break;
case 5:
textView.setText("广告倒计时:0秒");
break;
}
super.handleMessage(msg);//发送消息指令
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ad);
setAnimation();
Thread thread=new Thread(this);
thread.start();
}
private void setAnimation() {//渐变动画
ImageView imageView= (ImageView) findViewById(R.id.adimageview);
AlphaAnimation a=new AlphaAnimation(0.8f,1);
a.setDuration(5000);//时间
a.setAnimationListener(this);//配置监听器
imageView.startAnimation(a);//启动动画
}
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
//当动画结束的时候跳转
startActivity(new Intent(adActivity.this,loginActivity.class));
finish();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
@Override
public void run() {//线程处理倒计时问题
for (int i=1;i<=5;i++){
Message message=new Message();
try {
Thread.sleep(950);//线程休眠时间
} catch (InterruptedException e) {
e.printStackTrace();
}
message.what=i;
handler.sendMessage(message);//发送消息给处理者
}
}
}
这个可以实现首页广告倒计时的功能,有点不完善,但是努力做出来还是很不错的。