ui-js-components-images.md 4.6 KB
Newer Older
Z
zengyawen 已提交
1 2
# Image

Z
zengyawen 已提交
3
Image是图片组件,用来渲染展示图片。具体用法请参考[Image API](../reference/arkui-js/js-components-basic-image.md)
Z
zengyawen 已提交
4 5 6 7 8 9 10 11


## 创建Image组件

在pages/index目录下的hml文件中创建一个Image组件。
```
<!-- index.hml -->
<div class="container">
W
wangshuainan1 已提交
12
  <image style="height: 30%;" src="common/images/bg-tv.jpg"> </image>
Z
zengyawen 已提交
13 14 15 16 17 18
</div>
```

```
/* xxx.css */
.container {
W
wangshuainan1 已提交
19 20
  width: 100%;
  height: 100%; 
Z
zengyawen 已提交
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
  flex-direction: column;
  justify-content: center;
  align-items: center;
  background-color: #F1F3F5;
}
```

![zh-cn_image_0000001211227617](figures/zh-cn_image_0000001211227617.png)


## 设置Image样式

通过设置width、height和object-fit属性定义图片的宽、高和缩放样式。


```
<!-- index.hml -->
<div class="container">
  <image src="common/images/bg-tv.jpg"> </image>
</div>
```


```
/* xxx.css */
.container {
W
wangshuainan 已提交
47 48
  width: 100%;
  height: 100%;
Z
zengyawen 已提交
49 50 51
  flex-direction: column;
  align-items: center;
  justify-content: center;
W
wangshuainan1 已提交
52
  background-color:#F1F3F5;
Z
zengyawen 已提交
53 54
}
image{
W
wangshuainan1 已提交
55 56
  width: 80%;  
  height: 500px;
Z
zengyawen 已提交
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
  border: 5px solid saddlebrown;
  border-radius: 20px;
  object-fit: contain;
  match-text-direction:true;
}
```


![zh-cn_image_0000001163532072](figures/zh-cn_image_0000001163532072.png)


## 加载图片

图片成功加载时触发complete事件,返回加载的图源尺寸。加载失败则触发error事件,打印图片加载失败。

```
<!-- index.hml -->
<div class="container" >
  <div>
    <image src="common/images/bg-tv.jpg" oncomplete="imageComplete(1)" onerror="imageError(1)"> </image>
  </div>
  <div>
    <image src="common/images/bg-tv1.jpg" oncomplete="imageComplete(2)" onerror="imageError(2)"> </image>
  </div>
</div>
```

```
/* xxx.css */
.container{
W
wangshuainan 已提交
87 88
  width: 100%;
  height: 100%;
Z
zengyawen 已提交
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
  flex-direction: column;
  justify-content: center;
  align-self: center;
  background-color: #F1F3F5;
}
.container div{
  margin-left: 10%;
  width: 80%;
  height: 300px;
  margin-bottom: 40px;
}
```

```
/* index.js */
import prompt from '@system.prompt';
export default {
  imageComplete(i,e){
    prompt.showToast({
      message: "Image "+i+"'s width"+ e.width+"----Image "+i+"'s height"+e.height,
      duration: 3000,
    })
  },
  imageError(i,e){
    setTimeout(()=>{
      prompt.showToast({
        message: "Failed to load image "+i+".",
        duration: 3000,
      })
    },3000)
  }
}
```

W
wangshuainan 已提交
123 124
![zh-cn_image_images](figures/zh-cn_image_images.gif)

Z
zengyawen 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146


## 场景示例

在本场景中,开发者长按图片后将慢慢隐藏图片,当完全隐藏后再重新显示原始图片。定时器setInterval每隔一段时间改变图片透明度,实现慢慢隐藏的效果,当透明度为0时清除定时器,设置透明度为1。
```
<!-- index.hml -->
<div class="page-container">
  <div class="content">
    <div class="image-container">
      <image class="testimage" src="{{testuri}}" style="display:{{displaytype}};opacity:{{imageopacity}};" onclick="changedisplaytype" onlongpress="changeopacity"> </image>
    </div>
    <div class="text-container">
      <text style="font-size: 37px;font-weight:bold;color:orange;text-align: center;width: 100%;">Touch and hold the image</text>
    </div>
  </div>
</div>
```

```
/* xxx.css */
.page-container {
W
wangshuainan 已提交
147 148
  width: 100%;
  height: 100%;
Z
zengyawen 已提交
149 150 151 152
  flex-direction:column;
  align-self: center;
  justify-content: center;
  background-color:#F1F3F5;
Z
zengyawen 已提交
153
  background-color: #F1F3F5;
Z
zengyawen 已提交
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
}
.content{
  flex-direction:column;
}
.image-container {
  width: 100%;
  height: 300px;
  align-items: center;
  justify-content: center;
}
.text-container {
  margin-top:50px;
  width: 100%;
  height: 60px;
  flex-direction: row;
  justify-content: space-between;
}
.testimage {
W
wangshuainan1 已提交
172 173 174 175
  width: 100%;  height: 400px;   
  object-fit: scale-down;  
  border-radius: 20px;
}
Z
zengyawen 已提交
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
```

```
/* index.js */
import prompt from '@system.prompt';
export default {
  data: {
    testuri: 'common/images/bg-tv.jpg',
    imageopacity:1,
    timer: null
  },
  changeopacity: function () {
    prompt.showToast({
      message: 'Touch and hold the image.'
    })
    var opval = this.imageopacity * 20
    clearInterval(this.timer);
    this.timer = setInterval(()=>{
      opval--;
      this.imageopacity = opval / 20
      if (opval===0) {
        clearInterval(this.timer)
        this.imageopacity = 1
      }
    },100);
  }
}
```

Z
zengyawen 已提交
205
![zh-cn_image_0000001188771430](figures/zh-cn_image_0000001188771430.gif)
206 207 208 209 210 211 212 213

## 相关实例

针对Image开发,有以下相关实例可供参考:

- [image、image-animator(JS)](https://gitee.com/openharmony/codelabs/tree/master/JSUI/ClickableJsDemo)

- [图片编辑模板](https://gitee.com/openharmony/codelabs/tree/master/Media/ImageEditorTemplate)