跳到主要内容

useBrightness

屏幕亮度

何时使用

当需要获取或设置屏幕亮度

API

const [brightness, setBrightness] = useBrightness(keepon?: boolean);

参数说明

参数说明类型默认值
keepon是否常亮boolean-

返回值说明

返回值说明类型
brightness屏幕亮度值,范围 0 ~ 1,0 最暗,1 最亮number
setBrightness设置屏幕亮度值(0~1)(value) => Promise<General.CallbackResult>

代码演示

device/useBrightness/index
import React from 'react';
import { useBrightness } from 'taro-hooks';

import DemoContent from '@src/components/DemoContent';
import { Cell, Progress, Slider } from '@taroify/core';

export default () => {
const [brightness, setBrightness] = useBrightness();

return (
<DemoContent>
<Cell.Group clickable>
<Cell
title="当前亮度"
brief={<Progress percent={(brightness || 0) * 100} />}
>
{brightness}
</Cell>
<Cell
title="设置亮度"
brief={
<Slider
min={0}
max={1}
step={0.1}
defaultValue={brightness || 0}
onChange={setBrightness}
/>
}
></Cell>
</Cell.Group>
</DemoContent>
);
};

Hook 支持度

微信小程序H5ReactNative
✔️✔️