Android --- flash pages and countdown, android --- countdown
The implementation of android flash frequency is very simple. It can be implemented using the postDelayed () method of the Handler object. In this method, a Runnable object and a delay time are passed. This method achieves a delayed execution effect. The delay time is specified by 2nd parameters, in milliseconds. The first parameter is the Runnable object, which contains the operations to be performed after the delay. I added a Simple Amplification effect to him here. Paste the code below:
Package com. msn. zn. splashdemo;
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. view. Window;
Import android. view. WindowManager;
Import android. view. animation. Animation;
Import android. view. animation. AnimationUtils;
Import android. widget. ImageView;
Public class SplashActivity extends Activity {
Private ImageView iv_logo;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
// Cancel the title
This. requestWindowFeature (Window. FEATURE_NO_TITLE );
// Cancel the status bar
This. getWindow (). setFlags (WindowManager. LayoutParams. FLAG_FULLSCREEN,
WindowManager. LayoutParams. FLAG_FULLSCREEN );
SetContentView (R. layout. activity_splash );
Iv_logo = (ImageView) findViewById (R. id. logo );
// Load the animation in the xml file
Animation anim = AnimationUtils. loadAnimation (this, R. anim. splash );
Iv_logo.startAnimation (anim );
Handler handler = new Handler ();
Handler. postDelayed (new Runnable (){
@ Override
Public void run (){
Intent intent = new Intent (SplashActivity. this, MainActivity. class );
StartActivity (intent );
Finish ();
}
},3000 );
}
}
Layout xml:
<? Xml version = "1.0" encoding = "UTF-8"?>
<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"
Tools: context = "com. msn. zn. splashdemo. SplashActivity">
<ImageView
Android: id = "@ + id/logo"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: scaleType = "fitXY"
Android: src = "@ mipmap/splasht2"/>
</RelativeLayout>
Animation xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="1500"
android:fillAfter="true">
<scale
android:fromXScale="1.0"
android:fromYScale="1.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.1"
android:toYScale="1.1"/>
<alpha
android:fromAlpha="1.0"
android:toAlpha="0.8"/>
<! --
Duration = "1500" sets the animation duration to 1.5 s.
FillAfter = "true" sets the current position after the animation ends (that is, the position before the animation starts is not returned)
These can also be implemented in the code
Animation. setDuration (1500 );
Animation. setFillAfter (true );
-->
</set>
Countdown:
I used a countdown class CountDownTimer encapsulated by android. In fact, it encapsulates the creation of background threads and the Handler queue into a convenient class call. The following code is attached:
Package com. msn. zn. splashdemo;
Import android. app. Activity;
Import android. content. Intent;
Import android. OS. Bundle;
Import android. OS. CountDownTimer;
Import android. OS. Handler;
Import android. widget. TextView;
Public class SplashTimeActivity extends Activity {
Private TextView TV _time;
Private MyCountDownTimer mc;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_splash_time );
TV _time = (TextView) findViewById (R. id. TV _time );
Mc = new MyCountDownTimer (5000,1000 );
Mc. start (); // countdown to startup
Handler. postDelayed (new Runnable (){
@ Override
Public void run (){
Intent intent = new Intent (SplashTimeActivity. this, MainActivity. class );
StartActivity (intent );
}
},5000 );
}
Private Handler handler = new Handler ();
/**
* Defines an internal countdown class.
*/
Class MyCountDownTimer extends CountDownTimer {
/**
*
* @ Param millisInFuture the number of milliseconds from the start () call to the countdown completion and the onfinish () method being called
* @ Param countDownInterval interval for receiving onTick (long) callbacks
*/
Public MyCountDownTimer (long millisInFuture, long countDownInterval ){
Super (millisInFuture, countDownInterval );
}
@ Override
Public void onTick (long l ){
TV _time.setText ("Countdown" + l/1000 );
}
@ Override
Public void onFinish (){
TV _time.setText ("Jump in progress ");
}
}
}
Xml:
<? Xml version = "1.0" encoding = "UTF-8"?>
<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"
Android: background = "@ mipmap/splasht2"
Tools: context = "com. msn. zn. splashdemo. SplashTimeActivity">
<TextView
Android: id = "@ + id/TV _time"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "15dp"
Android: textSize = "20sp"
Android: text = "transient"
Android: layout_alignParentRight = "true"
Android: layout_alignParentEnd = "true"/>
</RelativeLayout>