深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
该接口通过图片数据源获取图片,支持本地图片和网络图片的渲染展示。其中,src是图片的数据源
1.加载图片资源
🦋1.1 存档图类型数据源
☀️1.1.1 本地资源
image组件引入本地图片路径,即可显示图片(根目录为ets文件夹)
@entry
@component
struct navigationexample {
build() {
column() {
image(‘1702344909275.jpg’)
.width(200)
}
.height(‘100%’)
.width(‘100%’)
.backgroundcolor(‘#f1f3f5’)
}
}
☀️1.1.2 网络资源
引入网络图片需申请权限ohos.permission.internet,
@entry
@component
struct navigationexample {
build() {
column() {
image(‘https://img1.baidu.com/it/u=3954245725,1121943782&fm=253&fmt=auto&app=120&f=jpeg?w=640&h=400’)
}
.height(‘100%’)
.width(‘100%’)
.backgroundcolor(‘#f1f3f5’)
}
}
☀️1.1.3 resource资源
使用资源格式可以跨包/跨模块引入图片,resources文件夹下的图片都可以通过$r
或$rawfile
资源接口读取到并转换到resource格式
@entry
@component
struct navigationexample {
build() {
column() {
image($r(‘app.media.icon’))
}
.height(‘20%’)
.width(‘20%’)
.backgroundcolor(‘#f1f3f5’)
}
}
@entry
@component
struct navigationexample {
build() {
column() {
image($rawfile(‘1702344909275.jpg’))
}
.height(‘100%’)
.width(‘100%’)
.backgroundcolor(‘#f1f3f5’)
}
}
☀️1.1.4 媒体库
支持file://路径前缀的字符串,用于访问通过媒体库提供的图片路径。
1、调用接口获取图库的照片url
import picker from ‘@ohos.file.picker’;
@entry
@component
struct index {
@state imgdatas: string[] = [];
// 获取照片url集
getallimg() {
let result = new array();
try {
let photoselectoptions = new picker.photoselectoptions();
photoselectoptions.mimetype = picker.photoviewmimetypes.image_type;
photoselectoptions.maxselectnumber = 5;
let photopicker = new picker.photoviewpicker();
photopicker.select(photoselectoptions).then((photoselectresult) => {
this.imgdatas = photoselectresult.photouris;
console.info('photoviewpicker.select successfully, photoselectresult uri: ’ + json.stringify(photoselectresult));
}).catch((err) => {
console.error(photoviewpicker.select failed with. code: ${err.code}, message: ${err.message}
);
});
} catch (err) {
console.error(photoviewpicker failed with. code: ${err.code}, message: ${err.message}
); }
}
// abouttoappear中调用上述函数,获取图库的所有图片url,存在imgdatas中
async abouttoappear() {
this.getallimg();
}
// 使用imgdatas的url加载图片。
build() {
column() {
grid() {
foreach(this.imgdatas, item => {
griditem() {
image(item)
.width(200)
}
}, item => json.stringify(item))
}
}.width(‘100%’).height(‘100%’)
}
}
2、从媒体库获取的url格式通常如下
image(‘file://media/photos/5’)
.width(200)
☀️1.1.5 base64
路径格式为data:image/[png|jpeg|bmp|webp];base64,[base64 data],其中[base64 data]
为base64字符串数据。
图片转换地址:https://c.runoob.com/front-end/59/
代码太长就不展示了,图片如下:
🦋1.2 多媒体像素图
import http from ‘@ohos.net.http’;
import responsecode from ‘@ohos.net.http’;
import image from ‘@ohos.multimedia.image’;
@entry
@component
struct navigationexample {
@state image: pixelmap = undefined;
build() {
column() {
button(“获取网络图片”)
.onclick(() => {
http.createhttp().request(“https://img1.baidu.com/it/u=3954245725,1121943782&fm=253&fmt=auto&app=120&f=jpeg?w=640&h=400”,
(error, data) => {
if (error){
console.error(http reqeust failed with. code: ${error.code}, message: ${error.message}
);
} else {
let code = data.responsecode;
if (responsecode.responsecode.ok === code) {
let res: any = data.result
let imagesource = image.createimagesource(res);
let options = {
alphatype: 0, // 透明度
editable: false, // 是否可编辑
pixelformat: 3, // 像素格式
scalemode: 1, // 缩略值
size: { height: 100, width: 100}
} // 创建图片大小
imagesource.createpixelmap(options).then((pixelmap) => {
console.log(‘进不来’)
this.image = pixelmap
})
}
}
}
)
})
image(this.image)
}
.height(‘100%’)
.width(‘100%’)
.backgroundcolor(‘#f1f3f5’)
}
}
2.显示矢量图
image组件支持显示矢量图(svg格式的图片),并且支持以下标签:svg、rect、circle、ellipse、path、line、polyline、polygon和animate。如果你想改变图片的绘制颜色,可以使用fillcolor属性。
image($r(‘app.media.cloud’)).width(50)
.fillcolor(color.blue)
3.添加属性
🦋3.1 设置图片缩放类型
缩放类型 | 描述 |
---|---|
contain | 图片按比例缩放以适应容器边界,可能会出现空白区域 |
cover | 图片按比例缩放以填充容器,可能会被裁剪 |
auto | 默认值,图片大小保持原始大小 |
fill | 图片拉伸以填充容器,可能会出现失真 |
scaledown | 图片按比例缩小以适应容器边界,但是不会放大图片 |
none | 不进行任何缩放操作,即使图片溢出容器也不会被裁剪或缩放 |
@entry
@component
struct mycomponent {
scroller: scroller = new scroller()
build() {
scroll(this.scroller) {
column(){
row() {
image(
r
(
′
a
p
p
.
m
e
d
i
a
.
i
m
g
2
′
)
)
.
w
i
d
t
h
(
200
)
.
h
e
i
g
h
t
(
150
)
.
b
o
r
d
e
r
(
w
i
d
t
h
:
1
)
.
o
b
j
e
c
t
f
i
t
(
i
m
a
g
e
f
i
t
.
c
o
n
t
a
i
n
)
.
m
a
r
g
i
n
(
15
)
/
/
保持宽高比进行缩小或者放大,使得图片完全显示在显示边界内。
.
o
v
e
r
l
a
y
(
′
c
o
n
t
a
i
n
′
,
a
l
i
g
n
:
a
l
i
g
n
m
e
n
t
.
b
o
t
t
o
m
,
o
f
f
s
e
t
:
x
:
0
,
y
:
20
)
i
m
a
g
e
(
r('app.media.img_2')).width(200).height(150) .border({ width: 1 }) .objectfit(imagefit.contain).margin(15) // 保持宽高比进行缩小或者放大,使得图片完全显示在显示边界内。 .overlay('contain', { align: alignment.bottom, offset: { x: 0, y: 20 } }) image(
r(′app.media.img2′)).width(200).height(150).border(width:1).objectfit(imagefit.contain).margin(15)//保持宽高比进行缩小或者放大,使得图片完全显示在显示边界内。.overlay(′contain′,align:alignment.bottom,offset:x:0,y:20)image(r(‘app.media.img_2’)).width(200).height(150)
.border({ width: 1 })
.objectfit(imagefit.cover).margin(15)
// 保持宽高比进行缩小或者放大,使得图片两边都大于或等于显示边界。
.overlay(‘cover’, { align: alignment.bottom, offset: { x: 0, y: 20 } })
image(katex parse error: expected 'eof', got '}' at position 186: … 0, y: 20 } }) }̲ row() { image(r(‘app.media.img_2’)).width(200).height(150)
.border({ width: 1 })
.objectfit(imagefit.fill).margin(15)
// 不保持宽高比进行放大缩小,使得图片充满显示边界。
.overlay(‘fill’, { align: alignment.bottom, offset: { x: 0, y: 20 } })
image(
r
(
′
a
p
p
.
m
e
d
i
a
.
i
m
g
2
′
)
)
.
w
i
d
t
h
(
200
)
.
h
e
i
g
h
t
(
150
)
.
b
o
r
d
e
r
(
w
i
d
t
h
:
1
)
/
/
保持宽高比显示,图片缩小或者保持不变。
.
o
b
j
e
c
t
f
i
t
(
i
m
a
g
e
f
i
t
.
s
c
a
l
e
d
o
w
n
)
.
m
a
r
g
i
n
(
15
)
.
o
v
e
r
l
a
y
(
′
s
c
a
l
e
d
o
w
n
′
,
a
l
i
g
n
:
a
l
i
g
n
m
e
n
t
.
b
o
t
t
o
m
,
o
f
f
s
e
t
:
x
:
0
,
y
:
20
)
i
m
a
g
e
(
r('app.media.img_2')).width(200).height(150) .border({ width: 1 }) // 保持宽高比显示,图片缩小或者保持不变。 .objectfit(imagefit.scaledown).margin(15) .overlay('scaledown', { align: alignment.bottom, offset: { x: 0, y: 20 } }) image(
r(′app.media.img2′)).width(200).height(150).border(width:1)//保持宽高比显示,图片缩小或者保持不变。.objectfit(imagefit.scaledown).margin(15).overlay(′scaledown′,align:alignment.bottom,offset:x:0,y:20)image(r(‘app.media.img_2’)).width(200).height(150)
.border({ width: 1 })
// 保持原有尺寸显示。
.objectfit(imagefit.none).margin(15)
.overlay(‘none’, { align: alignment.bottom, offset: { x: 0, y: 20 } })
}
}
}
}
}
🦋3.2 图片插值
图片插值(image interpolation)是指通过计算和处理像素点之间的值,生成一个新的像素值并插入到图像中的过程。插值通常用于调整图像的大小,例如将图像从低分辨率放大到高分辨率或从高分辨率缩小到低分辨率。通过插值算法,可以在不失真或失真最小的情况下调整图像大小。常用的插值算法包括双线性插值、双三次插值等。
harmonyos可以使用interpolation属性对图片进行插值,使图片显示得更清晰。
@entry
@component
struct index {
build() {
column() {
row() {
image(
r
(
′
a
p
p
.
m
e
d
i
a
.
i
m
g
2
′
)
)
.
w
i
d
t
h
(
′
40.
i
n
t
e
r
p
o
l
a
t
i
o
n
(
i
m
a
g
e
i
n
t
e
r
p
o
l
a
t
i
o
n
.
n
o
n
e
)
.
b
o
r
d
e
r
w
i
d
t
h
(
1
)
.
o
v
e
r
l
a
y
(
"
i
n
t
e
r
p
o
l
a
t
i
o
n
.
n
o
n
e
"
,
a
l
i
g
n
:
a
l
i
g
n
m
e
n
t
.
b
o
t
t
o
m
,
o
f
f
s
e
t
:
x
:
0
,
y
:
20
)
.
m
a
r
g
i
n
(
10
)
i
m
a
g
e
(
r('app.media.img_2')) .width('40%') .interpolation(imageinterpolation.none) .borderwidth(1) .overlay("interpolation.none", { align: alignment.bottom, offset: { x: 0, y: 20 } }) .margin(10) image(
r(′app.media.img2′)).width(′40.interpolation(imageinterpolation.none).borderwidth(1).overlay("interpolation.none",align:alignment.bottom,offset:x:0,y:20).margin(10)image(r(‘app.media.img_2’))
.width(‘40%’)
.interpolation(imageinterpolation.low)
.borderwidth(1)
.overlay(“interpolation.low”, { align: alignment.bottom, offset: { x: 0, y: 20 } })
.margin(10)
}.width(‘100%’)
.justifycontent(flexalign.center)
row() {
image(
r
(
′
a
p
p
.
m
e
d
i
a
.
i
m
g
2
′
)
)
.
w
i
d
t
h
(
′
40.
i
n
t
e
r
p
o
l
a
t
i
o
n
(
i
m
a
g
e
i
n
t
e
r
p
o
l
a
t
i
o
n
.
m
e
d
i
u
m
)
.
b
o
r
d
e
r
w
i
d
t
h
(
1
)
.
o
v
e
r
l
a
y
(
"
i
n
t
e
r
p
o
l
a
t
i
o
n
.
m
e
d
i
u
m
"
,
a
l
i
g
n
:
a
l
i
g
n
m
e
n
t
.
b
o
t
t
o
m
,
o
f
f
s
e
t
:
x
:
0
,
y
:
20
)
.
m
a
r
g
i
n
(
10
)
i
m
a
g
e
(
r('app.media.img_2')) .width('40%') .interpolation(imageinterpolation.medium) .borderwidth(1) .overlay("interpolation.medium", { align: alignment.bottom, offset: { x: 0, y: 20 } }) .margin(10) image(
r(′app.media.img2′)).width(′40.interpolation(imageinterpolation.medium).borderwidth(1).overlay("interpolation.medium",align:alignment.bottom,offset:x:0,y:20).margin(10)image(r(‘app.media.img_2’))
.width(‘40%’)
.interpolation(imageinterpolation.high)
.borderwidth(1)
.overlay(“interpolation.high”, { align: alignment.bottom, offset: { x: 0, y: 20 } })
.margin(10)
}.width(‘100%’)
.justifycontent(flexalign.center)
}
.height(‘100%’)
}
}
🦋3.3 设置图片重复样式
@entry
@component
struct mycomponent {
build() {
column({ space: 10 }) {
row({ space: 5 }) {
image($r(‘app.media.icon’))
.width(110)
深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!**
[外链图片转存中…(img-o00ftved-1715381999943)]
[外链图片转存中…(img-aoo3sdnw-1715381999944)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上鸿蒙开发知识点,真正体系化!
由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新
发表评论