(Transferred from: http://blog.csdn.net/piaozhiye/article/details/6544450)
It is also easy to realize that there are some mainstream players that can adjust the screen brightness by swiping the screen while playing a movie. Mainly based on the direction of the slide to set the screen brightness, the following demo implementation to swipe up the screen to lighten, swipe down the screen dimmed. When the screen reaches the brightest or darkest (20%), the device vibrates, the code is simple, not much to say. Simultaneous device vibration requires permission
<uses-permission android:name= "Android.permission.VIBRATE"/>
1[Java] View plaincopyprint?2 PackageOrg.piaozhiye.demo; 3 Importandroid.app.Activity; 4 ImportAndroid.os.Bundle; 5 ImportAndroid.os.Vibrator; 6 ImportAndroid.util.Log; 7 ImportAndroid.view.GestureDetector; 8 Importandroid.view.MotionEvent; 9 ImportAndroid.view.WindowManager; Ten ImportAndroid.widget.MediaController; One ImportAndroid.widget.VideoView; A Public classMainactivityextendsActivity { - PrivateVideoview Myvideoview; - PrivateString Path = "/mnt/sdcard/video/wonder girls_nobody (English version)." MP4 "; the PrivateString TAG = "Mainactivity"; - PrivateGesturedetector Mgesturedetector; - PrivateVibrator Vibrator; - /**Called when the activity is first created.*/ + @Override - Public voidonCreate (Bundle savedinstancestate) { + Super. OnCreate (savedinstancestate); A Setcontentview (R.layout.main); atMyvideoview =(Videoview) Findviewbyid (R.id.myvideoview); - Myvideoview.setvideopath (path); -Myvideoview.setmediacontroller (NewMediacontroller ( This)); - Myvideoview.requestfocus (); - Myvideoview.start (); - } in /* - * to * Set screen Brightness LP = 0 Full Dark, lp=-1, according to system settings, LP = 1; Brightest + */ - Public voidSetbrightness (floatbrightness) { theWindowmanager.layoutparams LP =GetWindow (). GetAttributes (); * //if (lp.screenbrightness <= 0.1) { $ //return; Panax Notoginseng // } -lp.screenbrightness = lp.screenbrightness + brightness/255.0f; the if(Lp.screenbrightness > 1) { +lp.screenbrightness = 1; AVibrator =(Vibrator) Getsystemservice (Vibrator_service); the Long[] pattern = {10, 200};//off/on/off/on ... +Vibrator.vibrate (Pattern,-1); -}Else if(Lp.screenbrightness < 0.2) { $Lp.screenbrightness = (float) 0.2; $Vibrator =(Vibrator) Getsystemservice (Vibrator_service); - Long[] pattern = {10, 200};//off/on/off/on ... -Vibrator.vibrate (Pattern,-1); the } -LOG.E (TAG, "lp.screenbrightness=" +lp.screenbrightness); Wuyi GetWindow (). SetAttributes (LP); the } - @Override Wu protected voidOnresume () { -Mgesturedetector =NewGesturedetector ( About NewGesturedetector.ongesturelistener () { $ Public BooleanOnsingletapup (motionevent e) { - return false; - } - Public BooleanOndown (motionevent e) { A return false; + } the Public voidonlongpress (motionevent e) { - } $ Public Booleanonfling (motionevent E1, motionevent E2, the floatVelocityx,floatvelocityy) { the return true; the } the Public BooleanOnscroll (motionevent E1, motionevent E2, - floatDistancex,floatDistancey) { in Final DoubleFling_min_distance = 0.5; the Final Doublefling_min_velocity = 0.5; the if(E1.gety ()-e2.gety () >fling_min_distance About&& Math.Abs (Distancey) >fling_min_velocity) { theLOG.E (TAG, "Up"); theSetbrightness (20); the } + if(E1.gety ()-E2.gety () <fling_min_distance -&& Math.Abs (Distancey) >fling_min_velocity) { theLOG.E (TAG, "Down"); BayiSetbrightness (-20); the } the return true; - } - Public voidonshowpress (motionevent e) { the //TODO auto-generated Method Stub the } the }); the Super. Onresume (); - } the Public Booleanontouchevent (Motionevent event) { the Booleanresult =mgesturedetector.ontouchevent (event); the if(!result) { 94 if(event.getaction () = =motionevent.action_up) { the //Getvideoinfosfrompath (FilePath); the } theresult =Super. Ontouchevent (event); 98 } About returnresult; - } 101 @Override102 protected voidOnStop () {103 if(NULL!=vibrator) { 104 Vibrator.cancel (); the } 106 Super. OnStop (); 107 } 108}
Test Supplement. XML;
1 <relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android" 2 xmlns: tools= "Http://schemas.android.com/tools" 3 android:layout_width= "Match_parent" 4 android:layout_height= "Match_parent" > 5 6 <videoview 7 Android:id= "@+id/myvideoview" 8 android:layout_width= "match_parent" 9 android: layout_height= "Match_parent"/> </RelativeLayout>
Swipe screen to adjust screen brightness when Android plays a movie (turn)