本示例介绍使用绘制组件中的Polygon组件配合使用显式动画以及borderRadius实现投票pk组件。
使用说明
// 使用Column绘制出胶囊块
Column()// 设置左上和左下两个角为圆角
.borderRadius({
topLeft: $r("app.integer.voting_component_fillet_radius"),
bottomLeft: $r("app.integer.voting_component_fillet_radius"),
topRight: $r("app.integer.voting_component_right_angle_radius"),
bottomRight: $r("app.integer.voting_component_right_angle_radius")
})
.backgroundColor(Constants.LEFT_COLOR)
.opacity(this.fillOpacity)// 动态变化透明度
.width(this.leftOptionWidth)// 选项宽度
.height($r("app.integer.voting_component_option_background_height"))
Stack() {
// TODO:知识点3:使用绘制组件Polygon投票组件中间的平行四边形空隙效果
Polygon()
.points(this.points)
.fill(Color.White)
.stroke(Color.White)
.antiAlias(true)
.width($r("app.integer.voting_component_polygon_width"))
.height($r("app.integer.voting_component_polygon_height"))
// 点击前,空隙宽度稍微大一些,且其中有PK二字
Stack() {
if (!this.isClick) {
Text() {
Span($r("app.string.voting_component_mid_text_left"))
.fontColor(Constants.LEFT_COLOR)
Span($r("app.string.voting_component_mid_text_right"))
.fontColor(Constants.RIGHT_COLOR)
}
.fontSize($r("app.integer.voting_component_mid_text_font_size"))
.fontStyle(FontStyle.Italic)
.fontWeight(Constants.MID_TEXT_FONT_WEIGHT)
.textAlign(TextAlign.Center)
}
}
}
// TODO:知识点4:因为Polygon是以一个矩形框为基准来绘制的,因此会受到这个矩形框的影响,使用position以及markAnchor来偏移,以抵消前述影响
.position({ x: this.leftOptionWidth })
.markAnchor({ x: $r("app.string.voting_component_mid_gap_mark_anchor") })
欢迎大家关注公众号<程序猿百晓生>,可以了解到以下内容:
1.OpenHarmony开发基础
2.OpenHarmony北向开发环境搭建
3.鸿蒙南向开发环境的搭建
4.鸿蒙生态应用开发白皮书V2.0 & V3.0
5.鸿蒙开发面试真题(含参考答案)
6.TypeScript入门学习手册
7.OpenHarmony 经典面试题(含参考答案)
8.OpenHarmony设备开发入门【最新版】
9.沉浸式剖析OpenHarmony源代码
10.系统定制指南
11.【OpenHarmony】Uboot 驱动加载流程
12.OpenHarmony构建系统--GN与子系统、部件、模块详解
13.ohos开机init启动流程
14.鸿蒙版性能优化指南
.......
// 定义动画
animateParam: AnimateParam = {
duration: Constants.ANIMATE_DURATION,
curve: Curve.EaseOut
}
/**
* 投票后改变属性
*
* @param option 投了左边还是右边
*/
changeState(option: string) {
// 投票后将点击状态置为已点击,实现投票只能投一次的效果
this.isClick = true;
// 左下角文字提示投票已选择的选项
this.notice = '已选择"' + option + '"';
// 投票后设置透明度,实现颜色变浅的效果
this.fillOpacity = Constants.END_FILL_OPACITY;
// 根据投票人数来计算选项两边的比例
const leftOptionPercent: number = this.leftOptionChoose / (this.leftOptionChoose + this.rightOptionChoose) * Constants.PERCENTAGE;
// TODO:知识点1:使用显式动画,只有在宽度变化时生效
animateTo(this.animateParam, () => {
this.leftOptionWidth = leftOptionPercent.toFixed(0) + '%';
this.rightOptionWidth = (Constants.PERCENTAGE - leftOptionPercent).toFixed(0) + '%';
this.points = Constants.END_POINTS;
});
}
不涉及
votingcomponent // har类型
|---constants
| |---Constants.ets // 常量类
|---view
| |---VotingComponent.ets // 视图层-投票组件页面
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。