A countdown effect similar to a movie in Android
1. Activity
Import java. util. Timer;
Import java. util. TimerTask;
Import android. annotation. SuppressLint;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. Message;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. view. animation. Animation;
Import android. view. animation. RotateAnimation;
Import android. widget. Button;
Import android. widget. ImageView;
Import android. widget. TextView;
Public class Test extends Activity {
Private long mlCount = 50;
Private long mCount = 0;
TextView tvTime;
Private Button startbuttondaoji;
Private Timer timer = null;
Private TimerTask task = null;
Private Handler handler = null;
Private Message msg = null;
Private ImageView min_progress, min_progress_hand;
Animation rotateAnimation;
Float predegree = 0;
Boolean okclear = false;
@ SuppressLint ("HandlerLeak ")
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
Init ();
}
Private void init (){
SetContentView (R. layout. test );
TvTime = (TextView) findViewById (R. id. duocitvTime );
Startbuttondaoji = (Button) findViewById (R. id. startbuttonduoci );
Min_progress = (ImageView) this. findViewById (R. id. duocimin_progress );
Min_progress_hand = (ImageView) this
. FindViewById (R. id. duocimin_progress_hand );
TvTime. setText ("4 ");
SaveRun. setisjishi (false );
Handler = new Handler (){
@ Override
Public void handleMessage (Message msg ){
Switch (msg. what ){
Case 1:
If (mlCount> 1 ){
MlCount --;
MCount ++;
} Else {
Break;
}
Int totalSec = 0;
TotalSec = (int) (mlCount/10 );
Int sec = (totalSec % 60 );
Try {
RotateAnimation = new RotateAnimation (predegree,
(Float) (36 * mCount ),
Animation. RELATIVE_TO_SELF, 0.5f,
Animation. RELATIVE_TO_SELF, 0.5f );
RotateAnimation. setDuration (100 );
RotateAnimation. setFillAfter (false );
Min_progress_hand.startAnimation (rotateAnimation );
Min_progress.startAnimation (rotateAnimation );
TvTime. setText (String. format ("% 1 $ 2d ",
Sec ));
Predegree = (float) (36 * mCount );
} Catch (Exception e ){
TvTime. setText (sec + "");
E. printStackTrace ();
}
Break;
Default:
Break;
}
Super. handleMessage (msg );
}
};
}
@ Override
Protected void onStart (){
Star ();
Super. onStart ();
}
Private void star (){
Startbuttondaoji. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View arg0 ){
Startbuttondaoji. setVisibility (View. GONE );
If (null = timer ){
If (null = task ){
SaveRun. setisjishi (true );
Okclear = false;
Min_progress.setVisibility (View. VISIBLE );
Task = new TimerTask (){
@ Override
Public void run (){
If (null = msg ){
Msg = new Message ();
} Else {
Msg = Message. obtain ();
}
Msg. what = 1;
Handler. sendMessage (msg );
}
};
}
Timer = new Timer (true );
Timer. schedule (tasks, 100,100 );
}
}
});
}
View. OnClickListener startPauseListener = new View. OnClickListener (){
@ Override
Public void onClick (View v ){
If (null = timer ){
If (null = task ){
SaveRun. setisjishi (true );
Okclear = false;
Min_progress.setVisibility (View. VISIBLE );
Task = new TimerTask (){
@ Override
Public void run (){
If (null = msg ){
Msg = new Message ();
} Else {
Msg = Message. obtain ();
}
Msg. what = 1;
Handler. sendMessage (msg );
}
};
}
Timer = new Timer (true );
Timer. schedule (tasks, 100,100 );
} Else {
Try {
SaveRun. setisjishi (false );
Okclear = true;
Task. cancel ();
Task = null;
Timer. cancel ();
Timer. purge ();
Timer = null;
Handler. removeMessages (msg. what );
} Catch (Exception e ){
E. printStackTrace ();
}
}
}
};
}
2. layout files
<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 = "@ android: color/background_dark "> <RelativeLayout android: layout_width =" match_parent "android: layout_height =" match_parent "android: layout_above =" @ + id/buttonlinear "> <ImageView android: id = "@ + id/duocimin_dial" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerInParent = "true" android: src = "@ drawable/iv_bg"/> <ImageView android: id = "@ + id/duocimin_progress" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerInParent = "true" android: src = "@ drawable/min_progress" android: visibility = "invisible"/> <ImageView android: id = "@ + id/duocimin_progress_hand" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerInParent = "true" android: src = "@ drawable/min_progress_hand"/> <TextView android: id = "@ + id/duocitvTime" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_centerInParent = "true" android: text = "00:00. 0 "android: textSize =" 35sp "android: textStyle =" bold "/> <LinearLayout android: id =" @ + id/duocihoursoflinear "android: layout_width =" wrap_content "android: layout_height = "wrap_content" android: layout_above = "@ + id/duocitvTime" android: layout_centerHorizontal = "true" android: gravity = "center" android: orientation = "horizontal" android: visibility = "invisible"> </LinearLayout> <LinearLayout android: layout_width = "match_parent" android: layout_height = "wrap_content" android: Layout = "@ + id/duocimin_dial" android: gravity = "center_horizontal" android: orientation = "horizontal"> <RelativeLayout android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_marginRight = "100sp"> </RelativeLayout> <RelativeLayout android: layout_width = "wrap_content" android: layout_height = "wrap_content"> </RelativeLayout> </LinearLayout> </RelativeLayout> <RelativeLayout android: id = "@ + id/buttonlinear" android: layout_width = "external" android: layout_height = "74sp" android: layout_alignParentBottom = "true" android: background = "@ drawable/v5_bottom_bar_bg_light"> <Button android: id = "@ + id/startbuttonduoci" android: layout_width = "match_parent" android: layout_height = "wrap_content" android: layout_centerInParent = "true" android: layout_marginLeft = "10sp" android: layout_marginRight = "10sp" android: background = "@ drawable/startback" android: text = "" "/> </RelativeLayout>