全部产品

屏幕亮度

更新时间:2020-07-07 19:42:16

my.setKeepScreenOn(OBJECT)

说明:基础库 1.3.0 及以上版本支持该接口,低版本需要做兼容处理,操作参见 小程序基础库说明。mPaaS 10.1.32 及以上版本支持该接口。


此接口用于设置是否保持屏幕长亮状态。仅在当前小程序生效,离开小程序后失效。

入参

参数 类型 必填 说明
keepScreenOn Boolean 是否保持屏幕长亮状态
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

代码示例

  1. <!-- API-DEMO page/API/screen/screen.axml-->
  2. <view class="page">
  3. <view class="page-description">屏幕亮度 API</view>
  4. <view class="page-section">
  5. <view class="page-section-title">设置是否保持屏幕长亮状态</view>
  6. <view class="page-section-demo">
  7. <switch checked="{{status}}" onChange="switchKeepScreenOn"/>
  8. </view>
  9. </view>
  10. <view class="page-section">
  11. <view class="page-section-title">设置屏幕亮度</view>
  12. <view class="page-section-demo">
  13. <slider value="{{brightness}}" max="1" min="0" onChange="sliderChange" step="0.02"/>
  14. </view>
  15. </view>
  16. <view class="page-section">
  17. <view class="page-section-title">获取屏幕亮度</view>
  18. <view class="page-section-demo">
  19. <button type="primary" onTap="getBrightness">获取屏幕亮度</button>
  20. </view>
  21. </view>
  22. </view>
  1. // API-DEMO page/API/screen/screen.js
  2. Page({
  3. data: {
  4. status: false,
  5. brightness: 1,
  6. },
  7. onLoad() {
  8. my.getScreenBrightness({
  9. success: res => {
  10. this.setData({
  11. brightness: res.brightness
  12. })
  13. },
  14. })
  15. },
  16. sliderChange(e) {
  17. my.setScreenBrightness({
  18. brightness: e.detail.value,
  19. success: (res) => {
  20. this.setData({
  21. brightness: e.detail.value,
  22. })
  23. }
  24. })
  25. },
  26. switchKeepScreenOn(e) {
  27. my.setKeepScreenOn({
  28. keepScreenOn: e.detail.value,
  29. success: (res) => {
  30. this.setData({
  31. status: e.detail.value,
  32. })
  33. }
  34. })
  35. },
  36. getBrightness() {
  37. my.getScreenBrightness({
  38. success: res => {
  39. my.alert({
  40. content: `当前屏幕亮度:${res.brightness}`
  41. });
  42. }
  43. })
  44. }
  45. });

my.getScreenBrightness(OBJECT)

说明:基础库 1.4.0 及以上版本支持该接口,低版本需要做兼容处理,操作参见 小程序基础库说明。mPaaS 10.1.32 及以上版本支持该接口。


此接口用于获取屏幕亮度。

入参

参数 类型 必填 说明
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

代码示例

  1. <!-- API-DEMO page/API/screen/screen.axml-->
  2. <view class="page">
  3. <view class="page-description">屏幕亮度 API</view>
  4. <view class="page-section">
  5. <view class="page-section-title">设置是否保持屏幕长亮状态</view>
  6. <view class="page-section-demo">
  7. <switch checked="{{status}}" onChange="switchKeepScreenOn"/>
  8. </view>
  9. </view>
  10. <view class="page-section">
  11. <view class="page-section-title">设置屏幕亮度</view>
  12. <view class="page-section-demo">
  13. <slider value="{{brightness}}" max="1" min="0" onChange="sliderChange" step="0.02"/>
  14. </view>
  15. </view>
  16. <view class="page-section">
  17. <view class="page-section-title">获取屏幕亮度</view>
  18. <view class="page-section-demo">
  19. <button type="primary" onTap="getBrightness">获取屏幕亮度</button>
  20. </view>
  21. </view>
  22. </view>
  1. // API-DEMO page/API/screen/screen.js
  2. Page({
  3. data: {
  4. status: false,
  5. brightness: 1,
  6. },
  7. onLoad() {
  8. my.getScreenBrightness({
  9. success: res => {
  10. this.setData({
  11. brightness: res.brightness
  12. })
  13. },
  14. })
  15. },
  16. sliderChange(e) {
  17. my.setScreenBrightness({
  18. brightness: e.detail.value,
  19. success: (res) => {
  20. this.setData({
  21. brightness: e.detail.value,
  22. })
  23. }
  24. })
  25. },
  26. switchKeepScreenOn(e) {
  27. my.setKeepScreenOn({
  28. keepScreenOn: e.detail.value,
  29. success: (res) => {
  30. this.setData({
  31. status: e.detail.value,
  32. })
  33. }
  34. })
  35. },
  36. getBrightness() {
  37. my.getScreenBrightness({
  38. success: res => {
  39. my.alert({
  40. content: `当前屏幕亮度:${res.brightness}`
  41. });
  42. }
  43. })
  44. }
  45. });

my.setScreenBrightness(OBJECT)

说明:基础库 1.4.0 及以上版本支持该接口,低版本需要做兼容处理,操作参见 小程序基础库说明。mPaaS 10.1.32 及以上版本支持该接口。


此接口用于设置屏幕亮度。

入参

参数 类型 必填 说明
brightness Number 需要设置的屏幕亮度,取值范围为 0-1
success Function 接口调用成功的回调函数
fail Function 接口调用失败的回调函数
complete Function 接口调用结束的回调函数(调用成功、失败都会执行)

代码示例

  1. <!-- API-DEMO page/API/screen/screen.axml-->
  2. <view class="page">
  3. <view class="page-description">屏幕亮度 API</view>
  4. <view class="page-section">
  5. <view class="page-section-title">设置是否保持屏幕长亮状态</view>
  6. <view class="page-section-demo">
  7. <switch checked="{{status}}" onChange="switchKeepScreenOn"/>
  8. </view>
  9. </view>
  10. <view class="page-section">
  11. <view class="page-section-title">设置屏幕亮度</view>
  12. <view class="page-section-demo">
  13. <slider value="{{brightness}}" max="1" min="0" onChange="sliderChange" step="0.02"/>
  14. </view>
  15. </view>
  16. <view class="page-section">
  17. <view class="page-section-title">获取屏幕亮度</view>
  18. <view class="page-section-demo">
  19. <button type="primary" onTap="getBrightness">获取屏幕亮度</button>
  20. </view>
  21. </view>
  22. </view>
  1. // API-DEMO page/API/screen/screen.js
  2. Page({
  3. data: {
  4. status: false,
  5. brightness: 1,
  6. },
  7. onLoad() {
  8. my.getScreenBrightness({
  9. success: res => {
  10. this.setData({
  11. brightness: res.brightness
  12. })
  13. },
  14. })
  15. },
  16. sliderChange(e) {
  17. my.setScreenBrightness({
  18. brightness: e.detail.value,
  19. success: (res) => {
  20. this.setData({
  21. brightness: e.detail.value,
  22. })
  23. }
  24. })
  25. },
  26. switchKeepScreenOn(e) {
  27. my.setKeepScreenOn({
  28. keepScreenOn: e.detail.value,
  29. success: (res) => {
  30. this.setData({
  31. status: e.detail.value,
  32. })
  33. }
  34. })
  35. },
  36. getBrightness() {
  37. my.getScreenBrightness({
  38. success: res => {
  39. my.alert({
  40. content: `当前屏幕亮度:${res.brightness}`
  41. });
  42. }
  43. })
  44. }
  45. });