1/* 2 * Copyright (c) 2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16/** 17 * 为图片添加变换效果 18 */ 19import { matrix4 } from '@kit.ArkUI'; 20 21@Entry 22@Component 23struct Test { 24 private matrix1 = matrix4.identity() 25 .translate({ x: -400, y: -750 }) 26 .scale({ x: 0.5, y: 0.5 }) 27 .rotate({ 28 x: 2, 29 y: 0.5, 30 z: 3, 31 centerX: 10, 32 centerY: 10, 33 angle: -10 34 }) 35 36 build() { 37 Row() { 38 Column({ space: 50 }) { 39 Column({ space: 5 }) { 40 Image($r('app.media.example')) 41 .border({ width:2, color: Color.Black }) 42 .objectFit(ImageFit.Contain) 43 .width(150) 44 .height(150) 45 Text('图片无变换') 46 .fontSize('25px') 47 } 48 Column({ space: 5 }) { 49 Image($r('app.media.example')) 50 .border({ width:2, color: Color.Black }) 51 .objectFit(ImageFit.None) 52 .translate({ x: 10, y: 10 }) 53 .scale({ x: 0.5, y: 0.5 }) 54 .width(100) 55 .height(100) 56 Text('Image直接变换,默认显示图源左上角。') 57 .fontSize('25px') 58 } 59 Column({ space: 5 }) { 60 Image($r('app.media.example')) 61 .objectFit(ImageFit.MATRIX) 62 .imageMatrix(this.matrix1) 63 .border({ width:2, color: Color.Black }) 64 .width(150) 65 .height(150) 66 Text('通过imageMatrix变换,调整图源位置,实现最佳呈现。') 67 .fontSize('25px') 68 } 69 } 70 .width('100%') 71 } 72 } 73}