码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2014-11-18 14:43:19      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   http   io   color   ar   os   sp   

(转自:http://blog.csdn.net/piaozhiye/article/details/6544450)

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

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

 

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

 

测试补充.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" />
10 
11 </RelativeLayout>

 

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

标签:android   style   blog   http   io   color   ar   os   sp   

原文地址:http://www.cnblogs.com/jenson138/p/4105681.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!