• 下载
  • 社区

my.getScreenBrightness

版本要求基础库 1.4.0  更高版本若版本较低,建议做 兼容处理


获取屏幕亮度。


扫码体验

屏幕亮度qr.jpeg


效果示例

设置屏幕亮度.gif


示例代码

<!-- API-DEMO page/API/screen/screen.axml-->
<view class="page">
  <view class="page-description">屏幕亮度 API</view>
  <view class="page-section">
    <view class="page-section-title">设置是否保持屏幕长亮状态</view>
    <view class="page-section-demo">
      <switch checked="{{status}}" onChange="switchKeepScreenOn"/>
    </view>
  </view>
  <view class="page-section">
    <view class="page-section-title">设置屏幕亮度</view>
    <view class="page-section-demo">
      <slider value="{{brightness}}" max="1" min="0" onChange="sliderChange" step="0.02"/>
    </view>
  </view>
  <view class="page-section">
    <view class="page-section-title">获取屏幕亮度</view>
    <view class="page-section-demo">
      <button type="primary" onTap="getBrightness">获取屏幕亮度</button>
    </view>
  </view>
</view>


// API-DEMO page/API/screen/screen.js
Page({
  data: {
    status: false,
    brightness: 1,
  },
  onLoad() {
    my.getScreenBrightness({
      success: res => {
        this.setData({
          brightness: res.brightness
        })
      },
    })
  },
  sliderChange(e) {
    my.setScreenBrightness({
      brightness: e.detail.value,
      success: (res) => {
        this.setData({
          brightness: e.detail.value,
        })
      }
    })
  },
  switchKeepScreenOn(e) {
    my.setKeepScreenOn({
      keepScreenOn: e.detail.value,
      success: (res) => {
        this.setData({
          status: e.detail.value,
        })
      }
    })
  },
  getBrightness() {
    my.getScreenBrightness({
      success: res => {
        my.alert({
          content: `当前屏幕亮度:${res.brightness}`
        });
      }
    })
  }
});


入参

Object 类型,属性如下:

属性

类型

必填

说明

success

Function

接口调用成功的回调函数。

fail

Function

接口调用失败的回调函数。

complete

Function

接口调用结束的回调函数(调用成功、失败都会执行)。