给图片加上rotate属性,点击按钮实现帧动画的播放,在onFrame方法中实现旋转角度的更新,在动画结束后替换图片资源并且归零旋转角度,步骤如下:
aboutToAppear(): void {
this.result = this.getUIContext().createAnimator(this.options);
this.result.onFrame = (value: number) => {
this.angle = -value * 30;
};
this.result.onFinish = () => {
// 这里图片资源需要修改为开发者自定义的资源
this.imageSource = $r('app.media.img2');
this.angle = 0;
};
} Image(this.imageSource)
.width(200)
.height(200)
.margin({ top: 50 })
.objectFit(ImageFit.Cover)
.rotate({
x: 0,
y: 0,
z: 1,
angle: this.angle,
centerX: '50%',
centerY: '50%',
centerZ: 0,
perspective: 0
}); 完整示例参考如下:
import { AnimatorOptions, AnimatorResult } from '@kit.ArkUI';
@Entry
@Component
struct ImageRotateAnimator {
@State angle: number = 0;
// 这里图片资源需要修改为开发者自定义的资源
@State imageSource: string | Resource = $r('app.media.img1');
result: AnimatorResult | undefined = undefined;
private options: AnimatorOptions = {
duration: 1200,
easing: 'friction',
delay: 0,
fill: 'forwards',
direction: 'normal',
iterations: 1,
begin: 0,
end: 1
};
aboutToAppear(): void {
this.result = this.getUIContext().createAnimator(this.options);
this.result.onFrame = (value: number) => {
this.angle = -value * 30;
};
this.result.onFinish = () => {
// 这里图片资源需要修改为开发者自定义的资源
this.imageSource = $r('app.media.img2');
this.angle = 0;
};
}
build() {
Column() {
Column() {
Image(this.imageSource)
.width(200)
.height(200)
.margin({ top: 50 })
.objectFit(ImageFit.Cover)
.rotate({
x: 0,
y: 0,
z: 1,
angle: this.angle,
centerX: '50%',
centerY: '50%',
centerZ: 0,
perspective: 0
});
}
.borderColor(Color.Blue)
.borderWidth(2)
.width(300)
.height(300);
Button('Click rotate')
.onClick(() => {
this.result?.play();
});
}.width('100%').height('100%');
}
}