網站首頁 編程語言 正文
補間可以實現兩個圖形之間顏色、形狀、大小、位置等的線性變化。
例如A...AB...BC...C,其中A、B、C是三幅圖片,兩個A的寬分別是10cm和50cm,兩個A之間共5幀,那么使用補間操作后,A圖片的寬分別是10cm、20cm、30cm、40cm、50cm,B和C圖片的寬度計算同理。對于A...ABC...C或者A...ABBC...C這種情況,B不進行補間操作。
下面新建一個控制臺處理程序,添加圖片類ImageClass.cs。
public class ImageClass { ? ? //寬 ? ? public int Width { get; set; } ? ? //高 ? ? public int Height { get; set; } ? ? //模擬判斷是否是同一張圖片 ? ? public string Path { get; set; } ? ? public ImageClass(int _width,int _height,string _path) ? ? { ? ? ? ? Width = _width; ? ? ? ? Height = _height; ? ? ? ? Path = _path; ? ? } }
新建圖片幀類ImgFrameClass.cs。
public class ImgFrameClass { ? ? public ImageClass FramesImg { get; set; } ? ? public int Frames { get; set; }//圖片位于的幀數 ? ? ? public ImgFrameClass(ImageClass _frameImg, int _frames) ? ? { ? ? ? ? FramesImg = _frameImg; ? ? ? ? Frames = _frames; ? ? } }
新建補間算法類,需要引用Newtonsoft.Json。
using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; ? namespace TweenDemo { ? ? public class Utility ? ? { ? ? ? ? public static ListSetTween(List _imgFrameList) ? ? ? ? { ? ? ? ? ? ? List imgFrameResultList = new List (); ? ? ? ? ? ? List imgFrameList = DeepCopyWithSerialization(_imgFrameList); ? ? ? ? ? ? //定義兩個游標,初始化為相鄰游標 ? ? ? ? ? ? int b = 0, a = 1; ? ? ? ? ? ? int len = imgFrameList.Count; ? ? ? ? ? ? //存在相同元素的個數 ? ? ? ? ? ? int count = 0; ? ? ? ? ? ? string samePath = string.Empty; ? ? ? ? ? ? while (a < len) ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ImgFrameClass itemb = imgFrameList[b]; ? ? ? ? ? ? ? ? ImgFrameClass itema = imgFrameList[a]; ? ? ? ? ? ? ? ? ? while (b >= 0 && a < len && (imgFrameList[b].FramesImg.Path == imgFrameList[a].FramesImg.Path)) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? samePath = imgFrameList[b].FramesImg.Path; ? ? ? ? ? ? ? ? ? ? while (a < len && (imgFrameList[a].FramesImg.Path == samePath)) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? a++; ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? count = count + 2; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? if (count != 0) ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ImgFrameClass tweenStartItem = imgFrameList[b]; ? ? ? ? ? ? ? ? ? ? ImgFrameClass tweenStopItem = imgFrameList[a - 1]; ? ? ? ? ? ? ? ? ? ? //添加初始圖片 ? ? ? ? ? ? ? ? ? ? imgFrameResultList.Add(tweenStartItem); ? ? ? ? ? ? ? ? ? ? ? ImageClass tweenStartImg = DeepCopyWithSerialization(tweenStartItem.FramesImg); ? ? ? ? ? ? ? ? ? ? ImageClass tweenStopImg = DeepCopyWithSerialization(tweenStopItem.FramesImg); ? ? ? ? ? ? ? ? ? ? double tweenFrame = tweenStopItem.Frames - tweenStartItem.Frames; ? ? ? ? ? ? ? ? ? ? double tweenImgW = (double)(tweenStopImg.Width - tweenStartImg.Width) / tweenFrame; ? ? ? ? ? ? ? ? ? ? double tweenImgH = (double)(tweenStopImg.Height - tweenStartImg.Height) / tweenFrame; ? ? ? ? ? ? ? ? ? ? ? int coutStart = tweenStartItem.Frames; ? ? ? ? ? ? ? ? ? ? int coutStop = tweenStopItem.Frames; ? ? ? ? ? ? ? ? ? ? //插入補間圖片 ? ? ? ? ? ? ? ? ? ? for (int i = coutStart + 1; i < coutStop; i++) ? ? ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? ? ? ImageClass tweenAddImg = new ImageClass((int)(tweenStartImg.Width + tweenImgW * (i - coutStart)), (int)(tweenStartImg.Height + tweenImgH * (i - coutStart)),samePath); ? ? ? ? ? ? ? ? ? ? ? ? imgFrameResultList.Add(new ImgFrameClass(tweenAddImg,i)); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? ? //添加末尾圖片 ? ? ? ? ? ? ? ? ? ? imgFrameResultList.Add(tweenStopItem); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? else ? ? ? ? ? ? ? ? { ? ? ? ? ? ? ? ? ? ? imgFrameResultList.Add(imgFrameList[b]); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? //不滿足則正常移動游標,都向前移動一個,相同元素的個數置0 ? ? ? ? ? ? ? ? b = a++; ? ? ? ? ? ? ? ? count = 0; ? ? ? ? ? ? } ? ? ? ? ? ? return imgFrameResultList; ? ? ? ? } ? ? ? ? ? public static T DeepCopyWithSerialization (T obj) ? ? ? ? { ? ? ? ? ? ? string json = JsonConvert.SerializeObject(obj); ? ? ? ? ? ? T copy = JsonConvert.DeserializeObject (json); ? ? ? ? ? ? return copy; ? ? ? ? } ? ? } }
模擬生成AAAAABBBBCBB結構的數據,Main函數如下:
static void Main(string[] args) { ? ? //模擬生成測試數據 ? ? ListimgFrameList = new List (); ? ? imgFrameList.Add(new ImgFrameClass(new ImageClass(10, 10, "A"),1)); ? ? imgFrameList.Add(new ImgFrameClass(new ImageClass(50, 50, "A"), 5)); ? ? imgFrameList.Add(new ImgFrameClass(new ImageClass(10, 10, "B"), 6)); ? ? imgFrameList.Add(new ImgFrameClass(new ImageClass(80, 80, "B"), 9)); ? ? imgFrameList.Add(new ImgFrameClass(new ImageClass(10, 10, "C"), 10)); ? ? imgFrameList.Add(new ImgFrameClass(new ImageClass(10, 10, "B"), 11)); ? ? imgFrameList.Add(new ImgFrameClass(new ImageClass(30, 30, "B"), 12)); ? ? ? List imgFrameResultList = Utility.SetTween(imgFrameList); ? ? foreach (ImgFrameClass item in imgFrameResultList) ? ? { ? ? ? ? Console.WriteLine(string.Format("Img{0},width:{1},height:{2}", item.FramesImg.Path, item.FramesImg.Width, item.FramesImg.Height)); ? ? } ? ? Console.ReadLine(); }
運行結果:
原文鏈接:https://blog.csdn.net/dnazhd/article/details/88423844
相關推薦
- 2022-06-12 如何在React項目中使用AntDesign_React
- 2022-09-29 React路由攔截模式及withRouter示例詳解_React
- 2022-01-12 2022年了--你還不會手寫promise? --_-- promise的實現 第一版
- 2022-07-23 詳解NumPy中的線性關系與數據修剪壓縮_python
- 2022-11-13 PyTorch模型的保存與加載方法實例_python
- 2022-04-12 報錯(! [rejected] master -> master (fetch fir
- 2022-11-01 Kotlin?ContentProvider使用方法介紹_Android
- 2022-10-05 C語言二叉樹的概念結構詳解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支