倒计时与动画

布局文件

<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”
>

<ImageView
    android:id="@+id/img"
    android:src="@drawable/ic_launcher"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_world" />

<TextView
    android:id="@+id/tv_time"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:text="5s"
    android:textSize="25sp" />

<Button
    android:id="@+id/jump"
    android:layout_width="200dp"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="22dp"
    android:text="直接进入" />

</RelativeLayout…>

主页面

/*

  • 1.开机广播
  • 2.动画+倒计时
  •  旋转+透明度
    

*/
public class MainActivity extends Activity {
private MyHandler myHandler = new MyHandler();
private int time = 5;
private TextView tv;
private Button jump;
private AnimationSet set;

@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_main);
	// 动画
	ImageView img = (ImageView) findViewById(R.id.img);
	tv = (TextView) findViewById(R.id.tv_time);
	jump = (Button) findViewById(R.id.jump);
	// 动画
	set = new AnimationSet(false);
	RotateAnimation ra = new RotateAnimation(0, 360,
			Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
			0.5f);
	AlphaAnimation aa = new AlphaAnimation(0, 1);
	set.addAnimation(aa);
	set.addAnimation(ra);
	//
	set.setDuration(5000);
	img.startAnimation(set);
	// 倒计时
	myHandler.sendEmptyMessageDelayed(0, 1000);
	// 直接进入的点击事件
	jump.setOnClickListener(new OnClickListener() {

		@Override
		public void onClick(View v) {
			// handler 停止
			myHandler.removeCallbacksAndMessages(null);
			// 动画也要停止
			set.cancel();
			// 跳转
			Intent intent = new Intent(MainActivity.this,
					DetailActivity.class);

			startActivity(intent);
		}
	});
}

class MyHandler extends Handler {
	@Override
	public void handleMessage(Message msg) {
		time--;
		tv.setText(time + "S");
		if (time == 0) {
			// 跳转
			Intent intent = new Intent(MainActivity.this,
					DetailActivity.class);
			startActivity(intent);
			myHandler.removeCallbacksAndMessages(null);
		} else {
			myHandler.sendEmptyMessageDelayed(0, 1000);
		}
	}
}

}

猜你喜欢

转载自blog.csdn.net/weixin_44424692/article/details/86559126
今日推荐