HarmonyOS鸿蒙Next中悬赏30积分麻烦帮写一个像这样的图片渲染列表样式
HarmonyOS鸿蒙Next中悬赏30积分麻烦帮写一个像这样的图片渲染列表样式
数据结构如下:
数据结构如下,渲染后让图片如上图那样,显示9张图片后图片展示区域可以上下滑动:
{"ArryData":[
{"fiename":"IMG_091.jpeg","Path":"file://media/Photo/10,"Pathurl":"/data/storage/el2/base/haps/entry/temp/IMG_091.jpeg","duration":0,"size":"554.63KB","byteSize":567937,"width":2519,"height":2519,"title":"IMG_091","mimeType":"image","dateAddedTime":1758602451568},{"fiename":"IMG_092.jpeg","Path":"file://media/Photo/11,"Pathurl":"/data/storage/el2/base/haps/entry/temp/IMG_092.jpeg","duration":0,"size":"554.63KB","byteSize":567937,"width":2519,"height":2519,"title":"IMG_092","mimeType":"image","dateAddedTime":1758602451568},{"fiename":"IMG_093.jpeg","Path":"file://media/Photo/10,"Pathurl":"/data/storage/el2/base/haps/entry/temp/IMG_093.jpeg","duration":0,"size":"554.63KB","byteSize":567937,"width":2519,"height":2519,"title":"IMG_093","mimeType":"image","dateAddedTime":1758602451568}
]}
更多关于HarmonyOS鸿蒙Next中悬赏30积分麻烦帮写一个像这样的图片渲染列表样式的实战教程也可以访问 https://www.itying.com/category-93-b0.html
3 回复
// ImageItem.ets
export default class ImageItem {
fiename: string
Path: string
Pathurl: string
duration: number
size: string
byteSize: number
width: number
height: number
title: string
mimeType: string
dateAddedTime: number
constructor(fiename: string, Path: string, Pathurl: string, duration: number,
size: string, byteSize: number, width: number, height: number,
title: string, mimeType: string, dateAddedTime: number) {
this.fiename = fiename
this.Path = Path
this.Pathurl = Pathurl
this.duration = duration
this.size = size
this.byteSize = byteSize
this.width = width
this.height = height
this.title = title
this.mimeType = mimeType
this.dateAddedTime = dateAddedTime
}
}
// ImageGridPage.ets
import ImageItem from './ImageItem'
@Entry
@Component
struct ImageGridPage {
// 模拟你的JSON数据
@State images: ImageItem[] = [
new ImageItem(
"IMG_091.jpeg",
"file://media/Photo/10",
"/data/storage/el2/base/haps/entry/temp/IMG_091.jpeg",
0, "554.63KB", 567937, 2519, 2519, "IMG_091", "image", 1758602451568
),
new ImageItem(
"IMG_092.jpeg",
"file://media/Photo/11",
"/data/storage/el2/base/haps/entry/temp/IMG_092.jpeg",
0, "554.63KB", 567937, 2519, 2519, "IMG_092", "image", 1758602451568
),
new ImageItem(
"IMG_093.jpeg",
"file://media/Photo/10",
"/data/storage/el2/base/haps/entry/temp/IMG_093.jpeg",
0, "554.63KB", 567937, 2519, 2519, "IMG_093", "image", 1758602451568
)
]
// 添加图片
addImage() {
const newIndex = this.images.length + 91
const newImage = new ImageItem(
`IMG_${newIndex}.jpeg`,
`file://media/Photo/${newIndex - 90}`,
`/data/storage/el2/base/haps/entry/temp/IMG_${newIndex}.jpeg`,
0, "554.63KB", 567937, 2519, 2519, `IMG_${newIndex}`, "image", Date.now()
)
this.images.push(newImage)
}
// 删除图片
deleteImage(index: number) {
this.images.splice(index, 1)
}
// 移动图片(交换位置)
moveImage(fromIndex: number, toIndex: number) {
const item = this.images[fromIndex]
this.images.splice(fromIndex, 1)
this.images.splice(toIndex, 0, item)
}
build() {
Column({ space: 10 }) {
// 添加按钮
Button("添加图片")
.width('100%')
.height(50)
.backgroundColor('#007AFF')
.onClick(() => this.addImage())
// 九宫格
Grid() {
ForEach(this.images, (item: ImageItem, index: number) => {
GridItem() {
Stack() {
Image(item.Pathurl)
.objectFit(ImageFit.Cover)
.width('100%')
.height('100%')
// 删除按钮
Button("×")
.width(30)
.height(30)
.backgroundColor('#FF3B30')
.borderRadius(15)
.position({ x: '85%', y: '15%' })
.onClick(() => this.deleteImage(index))
}
}
.onDragStart(() => {
return JSON.stringify({ index })
})
.onDrop((event: DragEvent, extraParams: string) => {
const from = JSON.parse(extraParams).index as number
this.moveImage(from, index)
})
}, item => item.fiename)
}
.columnsTemplate('1fr 1fr 1fr')
.rowsGap(10)
.columnsGap(10)
.padding(10)
}
.width('100%')
.height('100%')
.backgroundColor('#F5F5F5')
}
}
更多关于HarmonyOS鸿蒙Next中悬赏30积分麻烦帮写一个像这样的图片渲染列表样式的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS Next中,可通过ArkUI的List组件实现图片渲染列表。使用@State
装饰器管理数据源,@Builder
构建列表项。示例代码结构:
@Entry
@Component
struct ImageList {
@State imageData: string[] = [...]
build() {
List({ space: 12 }) {
ForEach(this.imageData, (item: string) => {
ListItem() {
ImageItemBuilder({ url: item })
}
})
}
}
}
@Builder
function ImageItemBuilder($$: { url: string }) {
Image($$.url)
.width(120)
.height(80)
.borderRadius(8)
}
需配合资源管理器配置图片路径,列表项支持滑动加载和缓存优化。
在HarmonyOS Next中实现图片渲染列表,可以使用List
组件配合GridRow
和GridCol
实现九宫格布局。以下是示例代码:
import { List, GridRow, GridCol, Image } from '@kit.arkui'
@Entry
@Component
struct ImageGridList {
private data: ArrayData[] = [ // 你的数据源
{
"fiename": "IMG_091.jpeg",
"Pathurl": "/data/storage/el2/base/haps/entry/temp/IMG_091.jpeg"
},
// ... 其他8个数据项
]
build() {
List({ space: 12 }) {
GridRow({ columns: 3, gutter: 8 }) {
ForEach(this.data, (item: ArrayData) => {
GridCol({ span: 1 }) {
Image(item.Pathurl)
.width('100%')
.aspectRatio(1)
.objectFit(ImageFit.Cover)
.borderRadius(8)
}
})
}
.padding(12)
}
.scrollBar(BarState.Auto)
}
}
interface ArrayData {
fiename: string;
Pathurl: string;
}
关键点说明:
GridRow
设置为3列布局,每个GridCol
占1列Image
组件使用aspectRatio(1)
保持正方形比例objectFit(ImageFit.Cover)
确保图片填充不变形List
组件默认支持垂直滚动,超过9张图片时可滑动
注意:实际路径可能需要使用ResourceManager
或媒体库API获取有效资源路径。