Android 播放电影时滑动屏幕调整屏幕亮度

发现有一些主流的播放器播放电影时可以通过滑动屏幕调整屏幕亮度,其实实现起来也很容易。 主要根据滑动的方向来设置屏幕亮度,以下这个demo实现向上滑动屏幕变亮,向下滑动则屏幕变暗。当的屏幕达到最亮或最暗(20%)的时候,设备会震动,代码简单,不多说。 同时设备震动需要有权限

<uses-permission android:name="android.permission.VIBRATE" />

 

[java]  view plain copy print ?
  1. package org.piaozhiye.demo;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.os.Vibrator;  
  5. import android.util.Log;  
  6. import android.view.GestureDetector;  
  7. import android.view.MotionEvent;  
  8. import android.view.WindowManager;  
  9. import android.widget.MediaController;  
  10. import android.widget.VideoView;  
  11. public class MainActivity extends Activity {  
  12.     private VideoView myVideoView;  
  13.     private String path = "/mnt/sdcard/video/Wonder Girls_Nobody(英文版).mp4";  
  14.     private String TAG = "MainActivity";  
  15.     private GestureDetector mGestureDetector;  
  16.     private Vibrator vibrator;  
  17.     /** Called when the activity is first created. */  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.main);  
  22.         myVideoView = (VideoView) findViewById(R.id.myvideoview);  
  23.         myVideoView.setVideoPath(path);  
  24.         myVideoView.setMediaController(new MediaController(this));  
  25.         myVideoView.requestFocus();  
  26.         myVideoView.start();  
  27.     }  
  28.     /* 
  29.      *  
  30.      * 设置屏幕亮度 lp = 0 全暗 ,lp= -1,根据系统设置, lp = 1; 最亮 
  31.      */  
  32.     public void setBrightness(float brightness) {  
  33.         WindowManager.LayoutParams lp = getWindow().getAttributes();  
  34.         // if (lp.screenBrightness <= 0.1) {  
  35.         // return;  
  36.         // }  
  37.         lp.screenBrightness = lp.screenBrightness + brightness / 255.0f;  
  38.         if (lp.screenBrightness > 1) {  
  39.             lp.screenBrightness = 1;  
  40.             vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);  
  41.             long[] pattern = { 10200 }; // OFF/ON/OFF/ON...  
  42.             vibrator.vibrate(pattern, -1);  
  43.         } else if (lp.screenBrightness < 0.2) {  
  44.             lp.screenBrightness = (float0.2;  
  45.             vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);  
  46.             long[] pattern = { 10200 }; // OFF/ON/OFF/ON...  
  47.             vibrator.vibrate(pattern, -1);  
  48.         }  
  49.         Log.e(TAG, "lp.screenBrightness= " + lp.screenBrightness);  
  50.         getWindow().setAttributes(lp);  
  51.     }  
  52.     @Override  
  53.     protected void onResume() {  
  54.         mGestureDetector = new GestureDetector(  
  55.                 new GestureDetector.OnGestureListener() {  
  56.                     public boolean onSingleTapUp(MotionEvent e) {  
  57.                         return false;  
  58.                     }  
  59.                     public boolean onDown(MotionEvent e) {  
  60.                         return false;  
  61.                     }  
  62.                     public void onLongPress(MotionEvent e) {  
  63.                     }  
  64.                     public boolean onFling(MotionEvent e1, MotionEvent e2,  
  65.                             float velocityX, float velocityY) {  
  66.                         return true;  
  67.                     }  
  68.                     public boolean onScroll(MotionEvent e1, MotionEvent e2,  
  69.                             float distanceX, float distanceY) {  
  70.                         final double FLING_MIN_DISTANCE = 0.5;  
  71.                         final double FLING_MIN_VELOCITY = 0.5;  
  72.                         if (e1.getY() - e2.getY() > FLING_MIN_DISTANCE  
  73.                                 && Math.abs(distanceY) > FLING_MIN_VELOCITY) {  
  74.                             Log.e(TAG, "up");  
  75.                             setBrightness(20);  
  76.                         }  
  77.                         if (e1.getY() - e2.getY() < FLING_MIN_DISTANCE  
  78.                                 && Math.abs(distanceY) > FLING_MIN_VELOCITY) {  
  79.                             Log.e(TAG, "down");  
  80.                             setBrightness(-20);  
  81.                         }  
  82.                         return true;  
  83.                     }  
  84.                     public void onShowPress(MotionEvent e) {  
  85.                         // TODO Auto-generated method stub  
  86.                     }  
  87.                 });  
  88.         super.onResume();  
  89.     }  
  90.     public boolean onTouchEvent(MotionEvent event) {  
  91.         boolean result = mGestureDetector.onTouchEvent(event);  
  92.         if (!result) {  
  93.             if (event.getAction() == MotionEvent.ACTION_UP) {  
  94.                 // getVideoInfosfromPath(filePath);  
  95.             }  
  96.             result = super.onTouchEvent(event);  
  97.         }  
  98.         return result;  
  99.     }  
  100.     @Override  
  101.     protected void onStop() {  
  102.         if (null != vibrator) {  
  103.             vibrator.cancel();  
  104.         }  
  105.         super.onStop();  
  106.     }  
  107. }  

你可能感兴趣的:(android,velocity,service,Path,float,distance)