Enable hardware decoding to improve GIF loading performance when the @ohos/gif-drawable library is used to decode GIF images.
// code-linter.json5
{
"rules": {
"@performance/gif-hardware-decoding-check": "warn",
}
} // @ohos/gif-drawable can be downloaded and installed by running ohpm install @ohos/gif-drawable@2.1.0.
import { GIFComponentV2, ResourceLoader } from '@ohos/gif-drawable'
@Entry
@ComponentV2
struct GifDrawableNoReport0 {
@Local model2:GIFComponentV2.ControllerOptions = new GIFComponentV2.ControllerOptions();
@Local gifAutoPlay:boolean = false;
@Local gifReset:boolean = false;
aboutToAppear(): void {
this.model2.destroy();
let model22:GIFComponentV2.ControllerOptions = new GIFComponentV2.ControllerOptions();
// Call the setOpenHardware API and set the value to true to enable hardware decoding.
model22.setOpenHardware(true);
model22.setSpeedFactor(1);
ResourceLoader.downloadDataWithContext(this.getUIContext().getHostContext(), {
url: 'https://example.com/test.gif'
}, (sucBuffer: ArrayBuffer) => {
model22.loadBuffer(sucBuffer, () => {
console.log('Successfully loaded and parsed the image, triggering the callback for rendering.')
this.gifAutoPlay = true;
// Assign new user configuration parameters to the component data to implement the subsequent GIF animation effect.
this.model2 = model22;
})
}, (err: string) => {
// Handle service logic based on the returned error message (display a failure placeholder image, reload, load another image, and more).
})
}
build() {
Row() {
GIFComponentV2({model:this.model2!!, autoPlay:this.gifAutoPlay!!, resetGif: this.gifReset!!})
}
}
} // @ohos/gif-drawable can be downloaded and installed by running ohpm install @ohos/gif-drawable@2.1.0.
import { GIFComponentV2, ResourceLoader } from '@ohos/gif-drawable'
@Entry
@ComponentV2
struct GifDrawableReport0 {
// Call the setOpenHardware API and set the value to true to enable hardware decoding.
// If model0 does not call the setOpenHardware API, an alarm is generated.
@Local model0: GIFComponentV2.ControllerOptions = new GIFComponentV2.ControllerOptions();
@Local gifAutoPlay:boolean = false;
@Local gifReset:boolean = false;
aboutToAppear(): void {
this.model0.destroy();
// If model00 does not call the setOpenHardware API, an alarm is generated.
let model00: GIFComponentV2.ControllerOptions = new GIFComponentV2.ControllerOptions();
model00.setSpeedFactor(1);
ResourceLoader.downloadDataWithContext(this.getUIContext().getHostContext(), {
url: 'https://example.com/test.gif'
}, (sucBuffer: ArrayBuffer) => {
model00.loadBuffer(sucBuffer, () => {
console.log('Successfully loaded and parsed the image, triggering the callback for rendering.')
this.gifAutoPlay = true;
// Assign new user configuration parameters to the component data to implement the subsequent GIF animation effect.
this.model0 = model00;
})
}, (err: string) => {
// Handle service logic based on the returned error message (display a failure placeholder image, reload, load another image, and more).
})
}
build() {
Row() {
GIFComponentV2({model:this.model0!!, autoPlay:this.gifAutoPlay!!, resetGif: this.gifReset!!})
}
}
} plugin:@performance/recommended plugin:@performance/all
For details about how to configure Code Linter check rules, see Code Linter.