Android 播放電影時滑動螢幕調整螢幕亮度

來源:互聯網
上載者:User

標籤: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 播放電影時滑動螢幕調整螢幕亮度(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.