HarmonyOS-鸿蒙app开发 —基于java_智能穿戴_旋转表冠使用旋转事件上报的值

HarmonyOS-鸿蒙app开发 —基于java_智能穿戴_旋转表冠使用旋转事件上报的值

开发者可以使用旋转事件上报的值,或是旋转事件上报的次数来控制具体业务。

当电源键旋转时,每10ms会上报一次旋转幅度。旋转表冠一圈为532个单位,每个单位上报的值是1.347/64。转一圈累计上报532 *(1.347/64)。

接口定义

控件ohos.agp.components.Component定义了Component.setRotationEventListener(Component.RotationEventListener)方法,子类可以重写该方法。

开发说明

用户可以使用旋转事件上报的值,也可以使用旋转事件上报的次数来控制具体业务。

如图所示,实现用旋转表冠控制进度条效果。

在“Project”窗口,打开“entry > src > main > resources > base > layout”,在layout文件夹下创建“ability_main.xml”。

ability_main.xml的实例代码如下:

<?xml version=”1.0″ encoding=”utf-8″?>

<DependentLayout

xmlns:ohos=“http://schemas.huawei.com/res/ohos”

ohos:height=“match_parent”

ohos:width=“match_parent”

ohos:background_element=“#000000”

>

 

<ProgressBar

ohos:id=“$+id:progressbar”

ohos:height=“50vp”

ohos:width=“match_parent”

ohos:center_in_parent=“true”

ohos:focusable=“focus_enable”

ohos:focusable_in_touch=“true”

ohos:margin=“50vp”

ohos:max=“100”

ohos:min=“0”

ohos:progress=“50”

ohos:progress_width=“10vp”

/>

</DependentLayout>

MainAbilitySlice.java的实例代码如下:

import ohos.aafwk.ability.AbilitySlice;

import ohos.aafwk.content.Intent;

import ohos.agp.components.Component;

import ohos.agp.components.ProgressBar;

 

public class MainAbilitySlice extends AbilitySlice {

private static final int FACTOR = 3;

private int rotationEventCount = 0;

 

@Override

public void onStart(Intent intent) {

super.onStart(intent);

super.setUIContent(ResourceTable.Layout_ability_main);

ProgressBar progressbar = findComponentById(ResourceTable.Id_progressbar);

/**

* 响应表冠的前提:获取焦点

*/

progressbar.setBindStateChangedListener(new Component.BindStateChangedListener() {

@Override

public void onComponentBoundToWindow(Component component) {

component.setTouchFocusable(true);

component.requestFocus();

}

 

@Override

public void onComponentUnboundFromWindow(Component component) {

}

});

 

/**

* 表冠每触发3个基本旋转事件将该进度条调整1个单位数值。业务可自行根据控件大小进行调整

*/

progressbar.setRotationEventListener((component, rotationEvent) -> {

if (rotationEvent != null) {

// 表冠向上滚动(顺时针转)axisValue<0,表冠向下滚动(逆时针转)axisValue>0,该值随转动速度成倍增加。

float rotationValue = rotationEvent.getRotationValue();

if (Math.abs(rotationEventCount) == FACTOR) {

progressbar.setProgressValue(progressbar.getProgress() + rotationEventCount / FACTOR);

rotationEventCount = 0;

} else {

rotationEventCount += rotationValue > 0 ? –1 : 1;

}

return true;

}

return false;

});

}

}

0 0 投票数
文章评分
订阅评论
提醒
0 评论
最旧
最新 最多投票
内联反馈
查看所有评论
0
希望看到您的想法,请您发表评论x