科技图库是一款基于鸿蒙系统(HarmonyOS)开发的高品质图片浏览应用,专注于展示精选科技主题图片。应用采用现代化的瀑布流布局,为用户提供流畅、直观的浏览体验,让科技之美尽收眼底。
应用基于鸿蒙系统的ArkTS和ArkUI框架开发,充分利用了鸿蒙生态的先进特性:
为科技爱好者提供高质量的科技主题图片,包括数据可视化、编程代码、高科技实验室、人工智能、未来城市等多种主题,激发创意灵感。
import promptAction from '@ohos.promptAction';
// 定义图片数据接口
interface ImageItem {
url: string;
width: number;
height: number;
title: string;
}
// 图片预览对话框参数接口
interface ImagePreviewDialogParams {
imageUrl: string;
title: string;
onClose: () => void;
}
@Entry
@Component
struct Index {
// 模拟图片数据
private images: ImageItem[] = [
{
url: 'https://images.unsplash.com/photo-1518770660439-4636190af475?auto=format&fit=crop&w=800&h=1200&q=80',
width: 800,
height: 1200,
title: '科技数据可视化'
},
{
url: 'https://images.unsplash.com/photo-1526374965328-7f61d4dc18c5?auto=format&fit=crop&w=800&h=600&q=80',
width: 800,
height: 600,
title: '数字代码'
},
{
url: 'https://images.unsplash.com/photo-1550751827-4bd374c3f58b?auto=format&fit=crop&w=800&h=1000&q=80',
width: 800,
height: 1000,
title: '高科技实验室'
},
{
url: 'https://images.unsplash.com/photo-1558346490-a72e53ae2d4f?auto=format&fit=crop&w=800&h=1400&q=80',
width: 800,
height: 1400,
title: '人工智能概念'
},
{
url: 'https://images.unsplash.com/photo-1515879218367-8466d910aaa4?auto=format&fit=crop&w=800&h=800&q=80',
width: 800,
height: 800,
title: '编程代码特写'
},
{
url: 'https://images.unsplash.com/photo-1563770660941-20978e870e26?auto=format&fit=crop&w=800&h=1100&q=80',
width: 800,
height: 1100,
title: '未来城市'
},
{
url: 'https://images.unsplash.com/photo-1573164713988-8665fc963095?auto=format&fit=crop&w=800&h=900&q=80',
width: 800,
height: 900,
title: '智能手表科技'
},
{
url: 'https://images.unsplash.com/photo-1504384308090-c894fdcc538d?auto=format&fit=crop&w=800&h=700&q=80',
width: 800,
height: 700,
title: '网络安全概念'
},
{
url: 'https://images.unsplash.com/photo-1531297484001-80022131f5a1?auto=format&fit=crop&w=800&h=1300&q=80',
width: 800,
height: 1300,
title: '虚拟现实技术'
},
{
url: 'https://images.unsplash.com/photo-1488229297570-58520851e868?auto=format&fit=crop&w=800&h=1000&q=80',
width: 800,
height: 1000,
title: '数据中心'
},
{
url: 'https://images.unsplash.com/photo-1535223289827-42f1e9919769?auto=format&fit=crop&w=800&h=1200&q=80',
width: 800,
height: 1200,
title: '机器人技术'
},
{
url: 'https://images.unsplash.com/photo-1607252650355-f7fd0460ccdb?auto=format&fit=crop&w=800&h=900&q=80',
width: 800,
height: 900,
title: '量子计算概念'
}
]
@State selectedImage: string = ''
@State selectedTitle: string = ''
@State isLoading: boolean = false
dialogController: CustomDialogController = new CustomDialogController({
builder: ImagePreviewDialog({
imageUrl: this.selectedImage,
title: this.selectedTitle,
onClose: () => {
this.dialogController.close()
}
}),
alignment: DialogAlignment.Center,
customStyle: true,
autoCancel: false
})
aboutToAppear(): void {
// 页面加载时的初始化操作
this.isLoading = true
setTimeout(() => {
this.isLoading = false
}, 1000) // 模拟网络加载
}
build() {
Stack({ alignContent: Alignment.Center }) {
Column() {
// 页面标题
Row() {
Text('瀑布流图片展示')
.fontSize(24)
.fontWeight(FontWeight.Bold)
Blank()
Button('关于')
.fontSize(14)
.height(32)
.backgroundColor('#007DFF')
.borderRadius(16)
.onClick(() => {
promptAction.showToast({
message: '瀑布流布局展示与图片预览功能演示',
duration: 2000
})
})
}
.width('100%')
.padding({
top: 20,
bottom: 10,
left: 16,
right: 16
})
// 瀑布流布局
if (!this.isLoading) {
WaterFlow() {
ForEach(this.images, (item: ImageItem, index: number) => {
FlowItem() {
Column() {
Image(item.url)
.width('100%')
.aspectRatio(item.width / item.height)
.borderRadius(8)
.alt('加载中...')
.onError(() => {
// 图片加载失败处理
promptAction.showToast({
message: `图片 ${index + 1} 加载失败`,
duration: 2000
})
})
Text(item.title)
.fontSize(14)
.margin({ top: 4 })
.maxLines(1)
.textOverflow({ overflow: TextOverflow.Ellipsis })
}
.onClick(() => {
this.selectedImage = item.url
this.selectedTitle = item.title
this.dialogController.open()
})
}
.width('100%')
.margin(4)
})
}
.columnsTemplate('1fr 1fr') // 两列布局
.columnsGap(8) // 列间距
.rowsGap(12) // 行间距
.padding(12) // 内边距
.width('100%')
.layoutWeight(1) // 占满剩余空间
} else {
// 加载状态
LoadingProgress()
.width(50)
.height(50)
.color('#007DFF')
}
}
.width('100%')
.height('100%')
}
.width('100%')
.height('100%')
.backgroundColor('#f5f5f5')
}
}
@CustomDialog
struct ImagePreviewDialog {
private imageUrl: string = '';
private title: string = '';
private onClose: () => void = () => {
};
controller?: CustomDialogController;
@State isLoading: boolean = true;
constructor(params: ImagePreviewDialogParams) {
super();
this.imageUrl = params.imageUrl;
this.title = params.title;
this.onClose = params.onClose;
}
build() {
Stack({ alignContent: Alignment.Center }) {
Column() {
// 标题栏
Row() {
Text(this.title)
.fontSize(18)
.fontWeight(FontWeight.Medium)
.fontColor('#FFFFFF')
Blank()
Button() {
Image($r('sys.media.ohos_ic_public_cancel'))
.width(24)
.height(24)
.fillColor('#FFFFFF')
}
.backgroundColor('rgba(0,0,0,0)')
.width(36)
.height(36)
.onClick(() => {
this.controller?.close()
this.onClose()
})
}
.width('100%')
.padding(16)
.backgroundColor('rgba(0,0,0,0.5)')
// 图片展示
Stack({ alignContent: Alignment.Center }) {
Image(this.imageUrl)
.width('100%')
.objectFit(ImageFit.Contain)
.layoutWeight(1)
.onComplete(() => {
this.isLoading = false
})
.onError(() => {
this.isLoading = false
promptAction.showToast({
message: '图片加载失败',
duration: 2000
})
})
if (this.isLoading) {
LoadingProgress()
.width(50)
.height(50)
.color('#FFFFFF')
}
}
.width('100%')
.layoutWeight(1)
}
.width('100%')
.height('100%')
}
.width('100%')
.height('100%')
.backgroundColor('#000000')
.onClick(() => {
// 点击背景关闭
this.controller?.close()
this.onClose()
})
}
}
鸿蒙系统(HarmonyOS)应用开发之实现瀑布流图片展示效果 - 高质量源码分享平台-免费下载各类网站源码与模板及前沿动态资讯