支持进度按钮UI控件
Access rate limit exceeded. Please log in to continue.
ohos_process-button
ohos_process-button is a custom UI component library developed for OpenHarmony applications, providing three different styles of progress button components to display operation progress and status feedback during user interactions.
Introduction
This project provides a set of feature-rich, easy-to-use progress button components that can meet the progress display needs in various application scenarios. The components are designed to be clean and beautiful, support high customization, and provide a good user experience for developers. For the upstream project, see android-process-button.
Features
1. Multiple Types of Progress Buttons
- ActionProcessButton: An action-style progress button drawn on Canvas. Use
progressto switch among normal, loading, complete, and error states. WhenprogressStyleis PROGRESS, a progress bar fills left to right in a strip about 6% of the button height at the bottom, while the main area keeps the normal background; when ENDLESS, a four-color cycling animation plays in the loading region, with colors adjustable via the controller. Suited for a bottom-bar style of precise progress or indeterminate looping load feedback. - SubmitProcessButton: Also Canvas-based; progress expands left to right across the full drawable width of the button (
progressWidthscales withprogress). In the loading state, gradients or a customGradientDrawablespan horizontally with progress. Use for form submit, file upload, payment confirmation, and other cases that call for a left-to-right sweep filling the whole visible area. - GenerateProcessButton: 100% width in layout; progress uses
progressHeight(scales withprogressas a fraction of canvas height) to grow vertically and fill upward—contrasting with Submit’s horizontal fill. Suited for content generation, queued tasks, accumulation, and other flows where height growth conveys progress; supports multi-state text, gradient drawables, shape, corners, and more.
2. Rich Customization Options
- Support for customizing basic properties such as button color, text, size, etc.
- Support for customizing progress bar style, color, height, etc.
- Support for text and style configuration in different states
- Provide controller classes for external control
3. Animation Effects
- ActionProcessButton supports four-color cycling animation effects
- Support for custom speed and effects
Installation
# Install via ohpm
ohpm install @ohos/process_button
Usage Instructions
1. Import Components
import { ActionProcessButton, SubmitProcessButton, GenerateProcessButton } from '@ohos/process_button';
2. Basic Usage
ActionProcessButton Example
@Entry
@Component
struct ActionProcessButtonExample {
@State progress: number = 0;
build() {
Column() {
ActionProcessButton({
progress: this.progress,
loadingText: 'Loading...',
completeText: 'Complete!',
colorNormal: '#ff33b5e5',
colorProgress: '#ffb534e3',
colorComplete: '#ff99cc00'
})
.width(300)
.height(80)
Button('Update Progress')
.onClick(() => {
this.progress += 10;
if (this.progress > 100) {
this.progress = 0;
}
})
}
.padding(20)
}
}
SubmitProcessButton Example
@Entry
@Component
struct SubmitProcessButtonExample {
@State progress: number = 0;
build() {
Column() {
SubmitProcessButton({
progress: this.progress,
loadingText: 'Submitting...',
completeText: 'Submitted!'
})
.width(300)
.height(80)
Button('Update Progress')
.onClick(() => {
this.progress += 20;
if (this.progress > 100) {
this.progress = 0;
}
})
}
.padding(20)
}
}
GenerateProcessButton Example
@Entry
@Component
struct GenerateProcessButtonExample {
@State progress: number = 0;
build() {
Column() {
GenerateProcessButton({
progress: this.progress,
loadingText: 'Generating...',
completeText: 'Generated!'
})
.width(300)
.height(80)
Button('Update Progress')
.onClick(() => {
this.progress += 15;
if (this.progress > 100) {
this.progress = 0;
}
})
}
.padding(20)
}
}
3. Using Controllers
@Entry
@Component
struct ButtonControllerExample {
private controller: ActionProcessButtonController = new ActionProcessButtonController();
build() {
Column() {
ActionProcessButton({
controller: this.controller,
loadingText: 'Loading...'
})
.width(300)
.height(80)
Button('Start Progress')
.onClick(() => {
this.controller.setProgress(50);
})
Button('Complete')
.onClick(() => {
this.controller.setProgress(100);
})
Button('Reset')
.onClick(() => {
this.controller.setProgress(0);
})
}
.padding(20)
}
}
Interface Description
1. Common Properties
All progress button components share the following common properties:
| Property Name | Type | Default Value | Description |
|---|---|---|---|
| loadingText | string | 'Loading...' | Progress state text |
| completeText | string | 'Complete!' | Success state text |
| errorText | string | 'Error!' | Error state text |
| colorNormal | string | '#ff33b5e5' | Normal state color |
| colorProgress | string | '#ffb534e3' | Progress state color |
| colorComplete | string | '#ff99cc00' | Success state color |
| colorError | string | '#ffff4444' | Error state color |
2. Enumeration Types
Orientation (gradient direction)
export enum Orientation {
TOP_BOTTOM, // 0°
TR_BL, // 45°
RIGHT_LEFT, // 90°
BR_TL, // 135°
BOTTOM_TOP, // 180°
BL_TR, // 225°
LEFT_RIGHT, // 270°
TL_BR // 315°
}
ShapeType (progress region outline)
export enum ShapeType {
RECTANGLE, // Rectangle
CIRCLE, // Circle
RING, // Ring
LINE // Line
}
GradientType (gradient kind)
export enum GradientType {
LINEAR_GRADIENT, // Linear gradient
RADIAL_GRADIENT, // Radial gradient
SWEEP_GRADIENT // Sweep gradient
}
ProgressStyle (progress display style)
export enum ProgressStyle {
PROGRESS, // Regular progress bar style
ENDLESS // Four-color cycling effect style
}
3. GradientDrawable Interface
3.1 ColorStop Interface
export interface ColorStop {
offset: number; // Offset (0-1)
color: string; // Color value
}
3.2 GradientDrawable Class Methods
| Method Name | Parameters | Return Value | Description |
|---|---|---|---|
| setShape | shape: ShapeType | void | Set shape type |
| getShape | - | ShapeType | Get shape type |
| setGradientType | type: GradientType | void | Set gradient type |
| getGradientType | - | GradientType | Get gradient type |
| setColorStops | stops: ColorStop[] | void | Set color stop list |
| setColors | colors: string[] | null | void | Set gradient colors |
| setOrientation | orientation: Orientation | void | Set gradient direction |
| getOrientation | - | Orientation | Get gradient direction |
| setGradientRadius | radius: number | void | Set gradient radius |
| getGradientRadius | - | number | Get gradient radius |
| setGradientCenter | centerX: number, centerY: number | void | Set gradient center coordinates |
| getGradientCenterX | - | number | Get gradient center X coordinate |
| getGradientCenterY | - | number | Get gradient center Y coordinate |
| setStrokeWidth | width: number | void | Set stroke width |
| setStrokeColor | color: string | void | Set stroke color |
| setLineDash | segments: number[] | void | Set dashed line style |
| setAlpha | alpha: number | void | Set transparency |
| getAlpha | - | number | Get transparency |
| setColorFilter | filter: string | void | Set color filter |
| getColorFilter | - | string | Get color filter |
| setCornerRadius | radius: number | number[] | void | Set corner radius |
| getCornerRadius | - | number | number[] | Get corner radius |
| setSize | width: number, height: number | void | Set the size of the drawn shape |
| setThicknessRatio | thicknessRatio: number | void | Set the thickness ratio of the ring |
| setInnerRadius | innerRadius: number | void | Set the inner radius value of the ring |
| setInnerRadiusRatio | innerRadiusRatio: number | void | Set the inner radius ratio of the ring |
| setThickness | thickness: number | void | Set the thickness value of the ring |
| getColors | - | string[] | Get gradient colors |
4. Controller Interfaces
4.1 ActionProcessButtonController
| Method Name | Parameters | Return Value | Description |
|---|---|---|---|
| setProgress | progress: number | void | Set progress |
| getProgress | - | number | Get current progress value |
| setColorProgress | color: string | void | Set progress state color |
| setColorComplete | color: string | void | Set complete state |
| setColorError | color: string | void | Set error state color |
| setColorPressed | color: string | void | Set pressed state color |
| getMaxProgress | - | number | Get maximum progress value |
| getMinProgress | - | number | Get minimum progress value |
| setLoadingText | loadingText: string | void | Set loading state text |
| setCompleteText | completeText: string | void | Set complete state text |
| setErrorText | errorText: string | void | Set error state text |
| getLoadingText | - | string | Get loading state text |
| getCompleteText | - | string | Get complete state text |
| getErrorText | - | string | Get error state text |
| setProgressDrawable | drawable: GradientDrawable | undefined | void | Set progress state Drawable |
| setCompleteDrawable | drawable: GradientDrawable | undefined | void | Set complete state Drawable |
| setErrorDrawable | drawable: GradientDrawable | undefined | void | Set error state Drawable |
| setMode | mode: ProgressStyle | void | Set progress mode |
| setColorScheme | color1: string, color2: string, color3: string, color4: string | void | Set colors for four-color cycling animation |
| getProgressDrawable | - | GradientDrawable | undefined | Get progress state Drawable |
| getCompleteDrawable | - | GradientDrawable | undefined | Get complete state Drawable |
| getErrorDrawable | - | GradientDrawable | undefined | Get error state Drawable |
4.2 SubmitProcessButtonController
| Method Name | Parameters | Return Value | Description |
|---|---|---|---|
| setProgress | progress: number | void | Set progress |
| getProgress | - | number | Get current progress value |
| setColorProgress | color: string | void | Set progress state color |
| setColorComplete | color: string | void | Set complete state |
| setColorError | color: string | void | Set error state color |
| setColorPressed | color: string | void | Set pressed state color |
| getMaxProgress | - | number | Get maximum progress value |
| getMinProgress | - | number | Get minimum progress value |
| setLoadingText | loadingText: string | void | Set loading state text |
| setCompleteText | completeText: string | void | Set complete state text |
| setErrorText | errorText: string | void | Set error state text |
| getLoadingText | - | string | Get loading state text |
| getCompleteText | - | string | Get complete state text |
| getErrorText | - | string | Get error state text |
| setProgressDrawable | drawable: GradientDrawable | undefined | void | Set progress state Drawable |
| setCompleteDrawable | drawable: GradientDrawable | undefined | void | Set complete state Drawable |
| setErrorDrawable | drawable: GradientDrawable | undefined | void | Set error state Drawable |
| getProgressDrawable | - | GradientDrawable | undefined | Get progress state Drawable |
| getCompleteDrawable | - | GradientDrawable | undefined | Get complete state Drawable |
| getErrorDrawable | - | GradientDrawable | undefined | Get error state Drawable |
4.3 GenerateProcessButtonController
| Method Name | Parameters | Return Value | Description |
|---|---|---|---|
| setProgress | progress: number | void | Set progress |
| getProgress | - | number | Get current progress value |
| setColorProgress | color: string | void | Set progress state color |
| setColorComplete | color: string | void | Set complete state |
| setColorError | color: string | void | Set error state color |
| setColorPressed | color: string | void | Set pressed state color |
| getMaxProgress | - | number | Get maximum progress value |
| getMinProgress | - | number | Get minimum progress value |
| setLoadingText | loadingText: string | void | Set loading state text |
| setCompleteText | completeText: string | void | Set complete state text |
| setErrorText | errorText: string | void | Set error state text |
| getLoadingText | - | string | Get loading state text |
| getCompleteText | - | string | Get complete state text |
| getErrorText | - | string | Get error state text |
| setProgressDrawable | drawable: GradientDrawable | undefined | void | Set progress state Drawable |
| setCompleteDrawable | drawable: GradientDrawable | undefined | void | Set complete state Drawable |
| setErrorDrawable | drawable: GradientDrawable | undefined | void | Set error state Drawable |
| getProgressDrawable | - | GradientDrawable | undefined | Get progress state Drawable |
| getCompleteDrawable | - | GradientDrawable | undefined | Get complete state Drawable |
| getErrorDrawable | - | GradientDrawable | undefined | Get error state Drawable |
Directory Structure
|---- ohos_process-button
| |---- entry # Example code folder
| |---- library # Component library folder
| | |---- Index.ets # External interface
| | |---- src/main/ets/components/
| | | |---- ActionProcessButton.ets # ActionProcessButton component
| | | |---- SubmitProcessButton.ets # SubmitProcessButton component
| | | |---- GenerateProcessButton.ets # GenerateProcessButton component
| | | |---- controller/
| | | | |---- ActionProcessButtonController.ets # ActionProcessButton controller
| | | | |---- SubmitProcessButtonController.ets # SubmitProcessButton controller
| | | | |---- GenerateProcessButtonController.ets # GenerateProcessButton controller
| | | |---- utils/
| | | | |---- GradientDrawable.ets # Gradient drawing utility class
| |---- .gitignore
| |---- build-profile.json5
| |---- hvigorfile.ts
| |---- oh-package.json5
| |---- README.md # Project description document
Constraints and Limitations
Verified in the following versions:
- DevEco Studio 5.1.1 Release,SDK:API12(5.0.0.66)
Contributing
If you find any issues during use, you can submit an Issue , and of course, we also welcome PRs.
Open Source License
This project is licensed under MIT License.