代码拉取完成,页面将自动刷新
本示例介绍使用绘制组件中的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") })
// 定义动画
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 // 视图层-投票组件页面
本场景依赖了路由模块来注册路由。
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。