After two days of hard work, the screen saver test program was finally completed. Because it was necessary to be used in the project, we did a test first and hoped to be useful to everyone, I have not found any complete information on the Internet at the beginning, and I have no source code. So I have attached the source code today. I hope you can share it with me. The source code is http://download.csdn.net/detail/weijing331/4518413.
This test procedure has two key points:
1. Control and identify the time and events when the user is still not touching the keyboard or screen.
2. design dynamic full-screen fade-in (Fade-in) and fade-out (fade-out) images.
The preceding two points need to be implemented through the thread. The time interval between the last keyboard or screen is determined and measured in a time-captured and remembered manner, when the specified number of seconds is exceeded (10 seconds in the Code), the screen saver mode is enabled.
It is a main activity, a Java file, and manifest does not need to be changed.
Package weijing. zhcw;
Import java. util. date;
Import Android. R. bool;
Import Android. App. activity;
Import Android. App. alertdialog;
Import Android. App. Dialog;
Import Android. content. dialoginterface;
Import Android. Graphics. Bitmap;
Import Android. Graphics. bitmapfactory;
Import Android. Graphics. matrix;
Import Android. Graphics. drawable. bitmapdrawable;
Import Android. OS. Bundle;
Import Android. OS. Handler;
Import Android. util. displaymetrics;
Import Android. util. log;
Import Android. View. keyevent;
Import Android. View. layoutinflater;
Import Android. View. Menu;
Import Android. View. menuitem;
Import Android. View. motionevent;
Import Android. View. view;
Import Android. View. window;
Import Android. View. windowmanager;
Import Android. widget. edittext;
Import Android. widget. imageview;
Import Android. widget. textview;
Public class pingbao_testactivity extends activity {
Private textview mtextview01 = NULL;
Private imageview mimageviewo1 = NULL;
/* Use the object as the new alertdialog */
Private layoutinflater minflater01 = NULL;
/* Enter the unlocked view */
Private view mview01 = NULL;
Private edittext medittext01, medittext02;
/* Menu option identifier to identify events */
Static final private int menu_about = menu. first;
Static final private int menu_exit = menu. First + 1;
Private handler mhandler01 = new handler ();
Private handler mhandler02 = new handler ();
Private handler mhandler03 = new handler ();
Private handler mhandler04 = new handler ();
/* Enter the password's dialog and determine whether the dialog is displayed */
Dialog alertdialog = NULL;
Private int alertdialogflag =-1;
/* Counter that controls whether a user is static */
Private int intcounter1, intcounter2;
/* Counter of fade in and fade out */
Private int intcounter3, intcounter4;
/* Replace counter of the background image ID in the control sequence */
Private int intdrable = 0;
/** Time stamp of the last user action */
Private date lastupdatetime;
/* Calculate the number of seconds when the user has no action */
Private long timeperiod;
/* The screen saver automatically enters the static state after n seconds */
Private float fholdstillsecond = 10;
Private Boolean bifrunscreensaver;
Private Boolean bfadeflagout, bfadefalgin = false;
Private long intervalscreensaver= 1000;
Private long maid = 1000;
Private long intervalfade = 100;
Private int screenwidth, screenheight;
/* Change the image every n seconds */
Private int intsecondstochange = 5;
/* Set the background image required by Screen Saver */
Private Static int [] screendrable = new int [] {R. drawable. bg_800x600,
R. drawable. bg_kuang2, R. drawable. bg, R. drawable. login_bg };
/** Called when the activity is first created .*/
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
/* Full screen display must be called before setcontentview */
Requestwindowfeature (window. feature_no_title );
Getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen,
Windowmanager. layoutparams. flag_fullscreen );
Setcontentview (R. layout. Main );
Mtextview01 = (textview) findviewbyid (R. Id. mytextview1 );
Mimageviewo1 = (imageview) findviewbyid (R. Id. myimageview1 );
Medittext01 = (edittext) findviewbyid (R. Id. myedittext1 );
/* Initial time for obtaining the user to touch the screen */
Lastupdatetime = new date (system. currenttimemillis ());
/* Initialize the widget visibility on Layout */
Recoveroriginallayout ();
}
/* Monitor threads with no user actions **/
Private runnable mtask01 = new runnable (){
@ Override
Public void run (){
// Todo auto-generated method stub
Intcounter1 ++;
Date timenow = new date (system. currenttimemillis ());
/* Calculate the time interval between user static and non-action */
Timeperiod = (long) timenow. gettime ()
-(Long) lastupdatetime. gettime ();
// System. Out. println ("timeperiod->" + timeperiod );
// System. Out. println ("timeperiod->" + timeperiod );
Float timeperiodsecond = (float) timeperiod/1000 );
/* If the object remains unchanged after the time limit */
System. Out. println ("timeperiodsecond->" + timeperiodsecond
+ "Fholdstillsecond->" + fholdstillsecond );
If (timeperiodsecond> fholdstillsecond ){
If (bifrunscreensaver = false ){
/* Start thread */
Mhandler02.postattime (mtask02, intervalscreensaver );
/* Fade out */
If (intcounter1% intsecondstochange) = 0 ){
Bfadeflagout = true;
Mhandler03.postdelayed (mtask03, intervalfade );
} Else {
/* Immediately after fadeout */
If (bfadeflagout = true ){
Bfadefalgin = true;
Mhandler04.postdelayed (mtask04, intervalfade );
} Else {
Bfadefalgin = false;
Intcounter4 = 0;
Mhandler04.removecallbacks (mtask04 );
}
Intcounter3 = 0;
Bfadeflagout = false;
}
Bifrunscreensaver = true;
} Else {
/* Screen Saver is running */
/* Fade out */
If (intcounter1% intsecondstochange) = 0 ){
Bfadeflagout = true;
Mhandler03.postdelayed (mtask03, intervalfade );
} Else {
/* Fade in immediately after fade out */
If (bfadeflagout = true ){
Bfadefalgin = true;
Mhandler04.postdelayed (mtask04, intervalfade );
} Else {
Bfadefalgin = false;
Intcounter4 = 0;
Mhandler04.removecallbacks (mtask04 );
}
Intcounter3 = 0;
Bfadeflagout = false;
}
}
}
Else {
/* When the distance between the user and the user with no actions does not exceed the time */
Bifrunscreensaver = false;
Recoveroriginallayout ();
}
Log. I ("Hippo", "counter1:" + integer. tostring (intcounter1) + "/"
+ Float. tostring (timeperiodsecond ));
/* Run thread 1 repeatedly */
Mhandler01.postdelayed (mtask01, intervalkeypadesaver );
}
};
Private runnable mtask02 = new runnable (){
@ Override
Public void run (){
// Todo auto-generated method stub
If (bifrunscreensaver = true ){
Intcounter2 ++;
Hideoriginallayout ();
Showscreensaver ();
Mhandler02.postdelayed (mtask02, intervalscreensaver );
} Else {
Mhandler02.removecallbacks (mtask02 );
}
}
};
/* Fade out special effect runnable */
Private runnable mtask03 = new runnable (){
@ Override
Public void run (){
// Todo auto-generated method stub
If (bifrunscreensaver = true & bfadeflagout = true ){
Intcounter3 ++;
/* Set the transparency of the imageview to fade down */
// Mimageviewo1.setalpha (255-intcounter3*28 );
System. Out. println ("mtask03->"
+ Integer. tobinarystring (intcounter3 ));
Mhandler03.postdelayed (mtask03, intervalfade );
} Else {
Mhandler03.removecallbacks (mtask03 );
}
}
};
/* Fade in special effect runnable */
Private runnable mtask04 = new runnable (){
@ Override
Public void run (){
// Todo auto-generated method stub
If (bifrunscreensaver = true & bfadefalgin = true ){
Intcounter4 ++;
/* Set the transparency of the imageview to light up */
// Mimageviewo1.setalpha (intcounter4*28 );
Mhandler04.postdelayed (mtask04, intervalfade );
System. Out. println ("Fade in->"
+ Integer. tobinarystring (intcounter4 ));
} Else {
Mhandler04.removecallbacks (mtask04 );
}
}
};
/* Restore the visibility of the original layout */
Private void recoveroriginallayout (){
Mtextview01.setvisibility (view. Visible );
Medittext01.setvisibility (view. Visible );
Mimageviewo1.setvisibility (view. Gone );
System. Out. println ("recoveroriginallayout --------> ");
}
/* Hide the visibility of the layout of the original application */
Private void hideoriginallayout (){
Mtextview01.setvisibility (view. Invisible );
Medittext01.setvisibility (view. Invisible );
System. Out. println ("hideoriginallayout --------> ");
}
/* Start screensaver */
Private void showscreensaver (){
System. Out. println ("intdrable -------->" + intdrable );
If (intdrable> = 4 ){
Intdrable = 0;
}
Displaymetrics dm = new displaymetrics ();
Getwindowmanager (). getdefaultdisplay (). getmetrics (DM );
Screenwidth = DM. widthpixels;
Screenheight = DM. heightpixels;
Bitmap BMP = bitmapfactory. decoderesource (getresources (),
Screendrable [intdrable]);
/* Matrix ratio */
Float scalewidth = screenwidth/BMP. getwidth ();
Float scaleheigth = screenheight/BMP. getheight ();
Matrix matrix = new matrix ();
/* Use matrix. postscale to set the dimension resize */
Bitmap ressizebitmap = bitmap. createbitmap (BMP, 0, 0, BMP. getwidth (),
BMP. getheight (), matrix, true );
/* Create a drawable to enlarge the image file to full screen */
Bitmapdrawable mynewbitmapdrawable = new bitmapdrawable (ressizebitmap );
Mimageviewo1.setimagedrawable (mynewbitmapdrawable );
/* Make imageview visible */
Mimageviewo1.setvisibility (view. Visible );
/* Set the image ID for several seconds at each interval, which takes effect only after the next runnable */
If (intcounter2% intsecondstochange) = 0 ){
Intdrable ++;
}
}
Public void onuserwakeupevent (){
If (bifrunscreensaver = true ){
Resetscreensaverlistener ();
Public void updateuseractiontime (){
Date timenow = new date (system. currenttimemillis ());
Timeperiod = timenow. gettime ()-lastupdatetime. gettime ();
Lastupdatetime. settime (timenow. gettime ());
System. Out. println ("updateuseractiontime --------> ");
}
Public void resetscreensaverlistener (){
/* Delete an existing runnable */
Mhandler01.removecallbacks (mtask01 );
Mhandler02.removecallbacks (mtask02 );
Date timenow = new date (system. currenttimemillis ());
/* Obtain the system time millis for clicking the button event */
Timeperiod = timenow. gettime ()-lastupdatetime. gettime ();
Lastupdatetime. settime (timenow. gettime ());
/* For runnable2, cancel Screen Saver */
Bifrunscreensaver = false;
/* Reset the counter of runnable1 and runnable1 */
Intcounter1 = 0;
Intcounter2 = 0;
/* Restore the original layout visable */
Recoveroriginallayout ();
/* Re-postdelayed the new runnable */
Mhandler01.postdelayed (mtask01, intervalkeypadesaver );
System. Out. println ("resetscreensaverlistener --------> ");
}
Public Boolean onkeydown (INT keycode, keyevent event ){
System. Out. println ("onkeydown --------> ");
If (bifrunscreensaver = true & keycode! = 4 ){
Onuserwakeupevent ();
} Else
Updateuseractiontime ();
Return super. onkeydown (keycode, event );
}
Public Boolean ontouchevent (motionevent event ){
System. Out. println ("ontouchevent --------> ");
If (bifrunscreensaver = true ){
Onuserwakeupevent ();
} Else
Updateuseractiontime ();
Return super. ontouchevent (event );
}
Protected void onresume (){
System. Out. println ("onresume --------> ");
Mhandler01.postattime (mtask01, intervalkeypadesaver );
Super. onresume ();
}
Protected void onpause (){
System. Out. println ("onpause --------> ");
Mhandler01.removecallbacks (mtask01 );
Mhandler02.removecallbacks (mtask02 );
Super. onpause ();
}
}
Below is main. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Orientation = "vertical">
<Imageview
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: Id = "@ + ID/myimageview1"
Android: scaletype = "fitcenter"
Android: layout_gravity = "center"
/>
<Textview
Android: Id = "@ + ID/mytextview1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: visible = "true"
Android: text = "@ string/Hello"/>
<Edittext
Android: Id = "@ + ID/myedittext1"
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
Android: text = ""/>
</Linearlayout>