赞
踩
本示例介绍使用绘制组件中的Polygon组件配合使用显式动画以及borderRadius实现投票pk组件。
使用说明
// 使用Column绘制出胶囊块
Column()
// 设置左上和左下两个角为圆角
.borderRadius(
{
topLeft: $r("app.integer.fillet_radius"),
bottomLeft: $r("app.integer.fillet_radius"),
topRight: $r("app.integer.right_angle_radius"),
bottomRight: $r("app.integer.right_angle_radius")
}
)
.backgroundColor(Constants.LEFT_COLOR)
.opacity(this.fillOpacity) // 动态变化透明度
.width(this.leftOptionWidth) // 选项宽度
.height($r("app.integer.option_background_height"))
// TODO:知识点3:使用绘制组件Polygon投票组件中间的平行四边形空隙效果 Polygon() .points(this.points) .fill(Color.White) .stroke(Color.White) .antiAlias(true) .width($r("app.integer.polygon_width")) .height($r("app.integer.polygon_height")) // 点击前,空隙宽度稍微大一些,且其中有PK两字 if (!this.isClick) { Text() { Span($r("app.string.mid_text_left")) .fontColor(Constants.LEFT_COLOR) Span($r("app.string.mid_text_right")) .fontColor(Constants.RIGHT_COLOR) } .fontSize($r("app.integer.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.mid_gap_mark_anchor") })
// 定义动画 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 // 视图层-投票组件页面
本场景依赖了路由模块来注册路由。
为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05
https://qr21.cn/FV7h05
https://qr21.cn/FV7h05
https://qr21.cn/FV7h05
https://qr18.cn/F781PH
https://qr18.cn/F781PH
1.项目开发必备面试题
2.性能优化方向
3.架构方向
4.鸿蒙开发系统底层方向
5.鸿蒙音视频开发方向
6.鸿蒙车载开发方向
7.鸿蒙南向开发方向
https://qr21.cn/D2k9D5
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。