ArkUI eTS上实现"流光按钮"组件

系统 OpenHarmony
在两个Stack堆叠容器中间画一个长一点并且短一点的Rect矩形并添加旋转和嵌入效果,而且最上面的Stack要比最下面的Stack要小一些,这样就能够看到中间旋转的Rect的动态效果。

​想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com​

1、前言

自从上次发贴​​ArkUI eTS上实现"流光按钮"效果 ​​后,觉得效果有了,但不能以后每个项目用到此类型按钮,都要复制代码来修改,这时组件的优点就显示出来了,此贴来说说,如何把上篇流光按钮做成一个组件。

2、效果

ArkUI eTS上实现"流光按钮"组件-开源基础软件社区

3、项目结构

ArkUI eTS上实现"流光按钮"组件-开源基础软件社区

4、组件介绍

组件包含了UI布局,组件属性定义,组件默认值,组件代码有详细说明,可以直接看代码

@Component
export struct StreamerButton {
    // 旋转角度
    @State private angle:number = 0;
    // 旋转速度
    private speed:number = 5;
    // 定时器Id
    private interval:number = 0;
    // 切换按钮前景色状态
    @State private isActive:boolean = false;
    // 定义按钮属性
    public streamerButtonAttribute: StreamerButtonAttribute = null;
    aboutToAppear() {
        // 初始化流光按钮属性对象
        this.streamerButtonAttribute = StreamerButtonAttribute.filter(this.streamerButtonAttribute)
        // 流光按钮旋转
        this.speedChange()
    }
    aboutToDisappear() {
        clearInterval(this.interval)
    }
    build() {
        // 外部堆叠容器
        Stack({ alignContent: Alignment.Center}) {
            // 绘制旋转青色矩形
            Rect().width(this.streamerButtonAttribute.width * 2)
                .height(this.streamerButtonAttribute.height - this.streamerButtonAttribute.border * 2)
                .fill(this.streamerButtonAttribute.streamerColor)
                .rotate({ x: 0, y: 0, z: 1, angle: this.angle })
            // 蒙住青色矩形多余部分
            Stack({ alignContent: Alignment.Center}) {
                // 按钮文本
                Text(this.streamerButtonAttribute.text)
                    .fontSize(this.streamerButtonAttribute.fontSize)
                    .fontColor(this.streamerButtonAttribute.fontColor)
            }
            .width(this.streamerButtonAttribute.width - this.streamerButtonAttribute.border)
            .height(this.streamerButtonAttribute.height - this.streamerButtonAttribute.border)
            .backgroundColor(this.isActive ? this.streamerButtonAttribute.backgroundColor : this.streamerButtonAttribute.foregroundColor)
            .onTouch(() => {
                this.isActive = !this.isActive
            })
            .onClick(() => {
                this.streamerButtonAttribute.clickCallback?.call(this)
            })
            .borderRadius(10)
        }.width(this.streamerButtonAttribute.width)
        .height(this.streamerButtonAttribute.height)
        .backgroundColor(this.streamerButtonAttribute.backgroundColor)
        // 裁剪超出外部堆叠容器内部
        .clip(new Rect({width: this.streamerButtonAttribute.width, height: this.streamerButtonAttribute.height}))
        .borderRadius(10)
    }
    /**
     * 流光旋转函数
     */
    speedChange() {
        var that = this
        that.angle = 0
        this.interval = setInterval(function(){
            that.angle += that.speed
        }, 15)
    }
}
/**
 * 流光按钮属性对象
 * @param streamerButtonAttribute
 */
class StreamerButtonAttribute {
    // 按钮文本
    public text:string = '按钮';
    // 按钮文本大小
    public fontSize:number = 30;
    // 字体颜色
    public fontColor:Color | number | string | Resource = '#FFFFFF';
    // 按钮宽度
    public width:number = 150;
    // 按钮高度
    public height:number = 80;
    // 边框粗细
    public border:number = 6;
    // 按钮前景色
    public foregroundColor:Color | number | string | Resource = '#5a5a5a';
    // 按钮背景色
    public backgroundColor:Color | number | string | Resource = '#ef437f';
    // 流光色
    public streamerColor:Color | number | string | Resource = '#00FFFF';
    // 单击回调函数
    public clickCallback: () => void;
    /**
     * 对非法参数进行过滤
     * @param streamerButtonAttribute
     */
    public static filter(streamerButtonAttribute: StreamerButtonAttribute): StreamerButtonAttribute {
        if (null == streamerButtonAttribute || undefined == streamerButtonAttribute) {
            // 初始化流光按钮属性对象
            streamerButtonAttribute = new StreamerButtonAttribute();
        } else {
            // 初始化流光按钮默认属性对象
            var defaultAttribute: StreamerButtonAttribute = new StreamerButtonAttribute();
            // 如果用户不指定按钮文本,使用默认按钮文本
            if (undefined == streamerButtonAttribute.text) {
                streamerButtonAttribute.text = defaultAttribute.text;
            }
            // 如果用户不指定字体大小,使用默认字体大小
            if (undefined == streamerButtonAttribute.fontSize) {
                streamerButtonAttribute.fontSize = defaultAttribute.fontSize;
            }
            // 如果用户不指定字体颜色,使用默认字体颜色
            if (undefined == streamerButtonAttribute.fontColor) {
                streamerButtonAttribute.fontColor = defaultAttribute.fontColor;
            }
            // 如果用户不指定边框粗细,使用默认边框粗细
            if (undefined == streamerButtonAttribute.border) {
                streamerButtonAttribute.border = defaultAttribute.border;
            }
            // 如果用户不指定宽度,使用默认宽度
            if (undefined == streamerButtonAttribute.width) {
                streamerButtonAttribute.width = defaultAttribute.width;
            }
            // 如果用户不指定高度,使用默认高度
            if (undefined == streamerButtonAttribute.height) {
                streamerButtonAttribute.height = defaultAttribute.height;
            }
            // 如果用户不指定前景色,使用默认前景色
            if (undefined == streamerButtonAttribute.foregroundColor) {
                streamerButtonAttribute.foregroundColor = defaultAttribute.foregroundColor;
            }
            // 如果用户不指定背景色,使用默认背景色
            if (undefined == streamerButtonAttribute.backgroundColor) {
                streamerButtonAttribute.backgroundColor = defaultAttribute.backgroundColor;
            }
            // 如果用户不指定流光色,使用默认流光色
            if (undefined == streamerButtonAttribute.streamerColor) {
                streamerButtonAttribute.streamerColor = defaultAttribute.streamerColor;
            }
        }
        // 返回属性对象
        return streamerButtonAttribute;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.

5、使用组件

如何使用自定义组件,首先引用自定义组件文件,如下:

import {StreamerButton} from '../common/StreamerButton.ets'
  • 1.

使用如下:

StreamerButton()
  • 1.

具体使用自定义组件,提供参数,请看代码详情

import {StreamerButton} from '../common/StreamerButton.ets'
@Entry
@Component
struct Sample {

    build() {
        Flex({direction: FlexDirection.Column, justifyContent: FlexAlign.SpaceAround,
            alignContent: FlexAlign.Center, alignItems: ItemAlign.Center}) {
            // 默认按钮
            StreamerButton()
            // 修改按钮字体颜色
            StreamerButton({
                streamerButtonAttribute: {
                    fontColor: '#FF0000',
                    text: '确定',
                    foregroundColor: Color.Orange
                }
            })
            // 自定义按钮
            StreamerButton({
                streamerButtonAttribute: {
                    text: '自定义',
                    fontSize: 40,
                    fontColor: '#00ff00',
                    border: 10,
                    width: 200,
                    height: 100,
                    foregroundColor: '#03a9f4',
                    backgroundColor: '#f441a5',
                    streamerColor: '#ffeb3b',
                    clickCallback: () => {
                        AlertDialog.show({
                            message: '您点击自定义按钮',
                            autoCancel: true
                        })
                    }
                }
            })
        }
        .width('100%')
        .height('100%')
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.

6、总结

使用组件后,主界面简洁明了,不用在关注组件里的具体实现,简单引用就可以得到一个漂亮的流光按钮。

​想了解更多关于开源的内容,请访问:​

​51CTO 开源基础软件社区​

​https://ost.51cto.com​​。

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2022-10-24 14:49:54

ArkUI心电图组件

2022-06-27 14:12:32

css鸿蒙自定义

2022-11-02 16:06:54

ArkUIETS

2022-05-26 14:50:15

ArkUITS扩展

2022-01-25 17:05:44

ArkUI_eTS操作系统鸿蒙

2021-11-26 10:08:57

鸿蒙HarmonyOS应用

2022-10-09 15:13:18

TextPickerArkUI eTS

2022-10-10 14:51:51

ArkUI eTSPieChart组件

2022-09-16 15:34:32

CanvasArkUI

2022-09-05 15:22:27

ArkUIets

2022-02-23 15:07:22

HarmonyOS常用控制ArkUI-eTS

2022-07-26 14:40:42

ArkUIJS

2022-10-17 14:36:09

ArkUI虚拟摇杆组件

2022-02-23 15:36:46

ArkUI-eTS事件监听鸿蒙

2022-07-11 16:26:37

eTS计算鸿蒙

2022-09-15 15:04:16

ArkUI鸿蒙

2022-08-12 19:13:07

etswifi连接操作

2023-03-13 15:03:05

鸿蒙ArkUI

2021-12-10 15:02:47

鸿蒙HarmonyOS应用

2022-07-07 14:01:59

管家服务系统ArkUI eTS
点赞
收藏

51CTO技术栈公众号