flutter开发实战-美颜前后对比图效果实现

2024-05-24 21:12

本文主要是介绍flutter开发实战-美颜前后对比图效果实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

flutter开发实战-美颜前后对比图效果实现
在这里插入图片描述

最近使用代码中遇到了图片前后对比,这里使用的是CustomClipper来实现

一、CustomClipper

我们实现CustomClipper子类来实现美颜后的图片裁剪功能

getClip()是用于获取剪裁区域的接口,由于图片大小是60×60,
我们返回剪裁区域为Rect.fromLTWH(10.0, 15.0, 40.0, 30.0),即图片中部40×30像素的范围。
shouldReclip() 接口决定是否重新剪裁。
如果在应用中,剪裁区域始终不会发生变化时应该返回false,这样就不会触发重新剪裁,避免不必要的性能开销。
如果剪裁区域会发生变化(比如在对剪裁区域执行一个动画),那么变化后应该返回true来重新执行剪裁。

二、使用CustomClipper来实现美颜前后对比图效果

美颜前后对比图,原图展示,美颜后的图片根据手势拖动距离进行裁剪
美颜之后的图裁剪,设置Clip.hardEdge。

ClipRect(clipper: compareCustomClipper,clipBehavior: Clip.hardEdge,child: CachedNetworkImage(imageUrl: "https://qiniu.example.com/64c2fba1-81ff-41dc-b32e-6920b0677f8c0",fit: BoxFit.cover,width: widget.width,height: widget.height,),),

手势拖动,更新compareCustomClipper

void onHorizontalDragDown(DragDownDetails details) {print("onHorizontalDragDown");startOffsetX = details.localPosition.dx;print("onHorizontalDragDown startOffsetX:${startOffsetX}");}void onHorizontalDragStart(DragStartDetails details) {print("onHorizontalDragStart");}void onHorizontalDragUpdate(DragUpdateDetails details) {print("onHorizontalDragUpdate");double curOffsetX = details.localPosition.dx;double distance = curOffsetX - startOffsetX;print("onHorizontalDragUpdate curOffsetX:${curOffsetX}, startOffsetX:${startOffsetX}, distance:${distance}");offsetX = widget.width! + distance;if (offsetX > widget.width!) {offsetX = widget.width!;}if (offsetX < 0) {offsetX = 0;}compareCustomClipper = CompareCustomClipper(Rect.fromLTWH(offsetX, 0.0, (widget.width ?? 0) - offsetX, widget.height ?? 0));setState(() {});}

完整代码如下


import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';class ComparePicPage extends StatefulWidget {const ComparePicPage({super.key});@overrideState<ComparePicPage> createState() => _ComparePicPageState();
}class _ComparePicPageState extends State<ComparePicPage> {@overrideWidget build(BuildContext context) {Size size = MediaQuery.of(context).size;return Scaffold(appBar: AppBar(title: const Text('ComparePicPage'),),body: Center(child: ComparePicWidget(width: 320,height: 480,),),);}
}// 自定义裁剪CustomClipper
class CompareCustomClipper extends CustomClipper<Rect> {CompareCustomClipper(this.rect);Rect rect;// Rect getClip(Size size) => Rect.fromLTWH(0.0, 15.0, 40.0, 30.0);@overrideRect getClip(Size size) => rect;@overridebool shouldReclip(CustomClipper<Rect> oldClipper) => true;
}/// 图片美颜前后对比
class ComparePicWidget extends StatefulWidget {const ComparePicWidget({super.key,this.width,this.height,});final double? width;final double? height;@overrideState<ComparePicWidget> createState() => _ComparePicWidgetState();
}class _ComparePicWidgetState extends State<ComparePicWidget> {// 定义一个裁剪CompareCustomClipper? compareCustomClipper;double offsetX = 0;double startOffsetX = 0;@overridevoid initState() {// TODO: implement initStateoffsetX = widget.width ?? 0;compareCustomClipper = CompareCustomClipper(Rect.fromLTWH(offsetX, 0.0, (widget.width ?? 0) - offsetX, widget.height ?? 0));super.initState();}@overridevoid dispose() {// TODO: implement disposesuper.dispose();}void onHorizontalDragDown(DragDownDetails details) {print("onHorizontalDragDown");startOffsetX = details.localPosition.dx;print("onHorizontalDragDown startOffsetX:${startOffsetX}");}void onHorizontalDragStart(DragStartDetails details) {print("onHorizontalDragStart");}void onHorizontalDragUpdate(DragUpdateDetails details) {print("onHorizontalDragUpdate");double curOffsetX = details.localPosition.dx;double distance = curOffsetX - startOffsetX;print("onHorizontalDragUpdate curOffsetX:${curOffsetX}, startOffsetX:${startOffsetX}, distance:${distance}");offsetX = widget.width! + distance;if (offsetX > widget.width!) {offsetX = widget.width!;}if (offsetX < 0) {offsetX = 0;}compareCustomClipper = CompareCustomClipper(Rect.fromLTWH(offsetX, 0.0, (widget.width ?? 0) - offsetX, widget.height ?? 0));setState(() {});}@overrideWidget build(BuildContext context) {return Container(width: widget.width,height: widget.height,decoration: BoxDecoration(color: Colors.black,borderRadius: const BorderRadius.all(Radius.circular(10),),border: Border.all(color: Colors.transparent,width: 0,style: BorderStyle.solid,),),clipBehavior: Clip.none,child: Stack(alignment: Alignment.center,clipBehavior: Clip.none,children: [// 原图ClipRect(clipBehavior: Clip.hardEdge,child: CachedNetworkImage(imageUrl: "https://qiniu.example.com/Fsgjbe7O8Z5x83_Aff8-Qage9bpc8e.png",fit: BoxFit.cover,width: widget.width,height: widget.height,),),// 美颜之后的图ClipRect(clipper: compareCustomClipper,clipBehavior: Clip.hardEdge,child: CachedNetworkImage(imageUrl: "https://qiniu.example.com/64c2fba1-81ff-41dc-b32e-6920b0677f833c",fit: BoxFit.cover,width: widget.width,height: widget.height,),),// linePositioned(left: offsetX + 26.5 + (-27.5),child: buildLine(context),),Positioned(left: offsetX + (-27.5),child: buildCustomButton(context),),// tipPositioned(left: 20,top: 20,child: buildCompareTip(context, "原图"),),Positioned(right: 20,top: 20,child: buildCompareTip(context, "美颜后"),),],),);}Widget buildLine(BuildContext context) {return Image.asset("assets/images/line.png",width: 2,height: 576,fit: BoxFit.cover,);}Widget buildCustomButton(BuildContext context) {return GestureDetector(onHorizontalDragDown: (DragDownDetails details) {onHorizontalDragDown(details);},onHorizontalDragStart: (DragStartDetails details) {onHorizontalDragStart(details);},onHorizontalDragUpdate: (DragUpdateDetails details) {onHorizontalDragUpdate(details);},child: Image.asset("assets/images/move_button.png",width: 55,height: 55,fit: BoxFit.cover,),);}Widget buildCompareTip(BuildContext context, String title) {return Container(width: 60,height: 30,alignment: Alignment.center,decoration: BoxDecoration(color: Colors.black.withOpacity(0.35),borderRadius: const BorderRadius.all(Radius.circular(20),),),child: Text(title,maxLines: 1,overflow: TextOverflow.ellipsis,textAlign: TextAlign.center,style: const TextStyle(fontSize: 13,fontWeight: FontWeight.w600,fontStyle: FontStyle.normal,color: Colors.white,decoration: TextDecoration.none,),),);}
}

三、小结

flutter开发实战-美颜前后对比图效果实现

学习记录,每天不停进步。

这篇关于flutter开发实战-美颜前后对比图效果实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/999539

相关文章

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

PostgreSQL中MVCC 机制的实现

《PostgreSQL中MVCC机制的实现》本文主要介绍了PostgreSQL中MVCC机制的实现,通过多版本数据存储、快照隔离和事务ID管理实现高并发读写,具有一定的参考价值,感兴趣的可以了解一下... 目录一 MVCC 基本原理python1.1 MVCC 核心概念1.2 与传统锁机制对比二 Postg

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

C++高效内存池实现减少动态分配开销的解决方案

《C++高效内存池实现减少动态分配开销的解决方案》C++动态内存分配存在系统调用开销、碎片化和锁竞争等性能问题,内存池通过预分配、分块管理和缓存复用解决这些问题,下面就来了解一下... 目录一、C++内存分配的性能挑战二、内存池技术的核心原理三、主流内存池实现:TCMalloc与Jemalloc1. TCM

OpenCV实现实时颜色检测的示例

《OpenCV实现实时颜色检测的示例》本文主要介绍了OpenCV实现实时颜色检测的示例,通过HSV色彩空间转换和色调范围判断实现红黄绿蓝颜色检测,包含视频捕捉、区域标记、颜色分析等功能,具有一定的参考... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间

Python并行处理实战之如何使用ProcessPoolExecutor加速计算

《Python并行处理实战之如何使用ProcessPoolExecutor加速计算》Python提供了多种并行处理的方式,其中concurrent.futures模块的ProcessPoolExecu... 目录简介完整代码示例代码解释1. 导入必要的模块2. 定义处理函数3. 主函数4. 生成数字列表5.

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部