C#使用游標(biāo)實(shí)現(xiàn)補(bǔ)間函數(shù)
補(bǔ)間可以實(shí)現(xiàn)兩個(gè)圖形之間顏色、形狀、大小、位置等的線性變化。
例如A...AB...BC...C,其中A、B、C是三幅圖片,兩個(gè)A的寬分別是10cm和50cm,兩個(gè)A之間共5幀,那么使用補(bǔ)間操作后,A圖片的寬分別是10cm、20cm、30cm、40cm、50cm,B和C圖片的寬度計(jì)算同理。對(duì)于A...ABC...C或者A...ABBC...C這種情況,B不進(jìn)行補(bǔ)間操作。
下面新建一個(gè)控制臺(tái)處理程序,添加圖片類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; }//圖片位于的幀數(shù)
?
? ? public ImgFrameClass(ImageClass _frameImg, int _frames)
? ? {
? ? ? ? FramesImg = _frameImg;
? ? ? ? Frames = _frames;
? ? }
}新建補(bǔ)間算法類,需要引用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 List<ImgFrameClass> SetTween(List<ImgFrameClass> _imgFrameList)
? ? ? ? {
? ? ? ? ? ? List<ImgFrameClass> imgFrameResultList = new List<ImgFrameClass>();
? ? ? ? ? ? List <ImgFrameClass> imgFrameList = DeepCopyWithSerialization(_imgFrameList);
? ? ? ? ? ? //定義兩個(gè)游標(biāo),初始化為相鄰游標(biāo)
? ? ? ? ? ? int b = 0, a = 1;
? ? ? ? ? ? int len = imgFrameList.Count;
? ? ? ? ? ? //存在相同元素的個(gè)數(shù)
? ? ? ? ? ? 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;
? ? ? ? ? ? ? ? ? ? //插入補(bǔ)間圖片
? ? ? ? ? ? ? ? ? ? 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]);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //不滿足則正常移動(dòng)游標(biāo),都向前移動(dòng)一個(gè),相同元素的個(gè)數(shù)置0
? ? ? ? ? ? ? ? b = a++;
? ? ? ? ? ? ? ? count = 0;
? ? ? ? ? ? }
? ? ? ? ? ? return imgFrameResultList;
? ? ? ? }
?
? ? ? ? public static T DeepCopyWithSerialization<T>(T obj)
? ? ? ? {
? ? ? ? ? ? string json = JsonConvert.SerializeObject(obj);
? ? ? ? ? ? T copy = JsonConvert.DeserializeObject<T>(json);
? ? ? ? ? ? return copy;
? ? ? ? }
? ? }
}模擬生成AAAAABBBBCBB結(jié)構(gòu)的數(shù)據(jù),Main函數(shù)如下:
static void Main(string[] args)
{
? ? //模擬生成測(cè)試數(shù)據(jù)
? ? List<ImgFrameClass> imgFrameList = new List<ImgFrameClass>();
? ? 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<ImgFrameClass> 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();
}運(yùn)行結(jié)果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#中DateTimePicker默認(rèn)值顯示為空的問題
這篇文章主要介紹了C#中DateTimePicker默認(rèn)值顯示為空的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
C#實(shí)現(xiàn)對(duì)數(shù)組進(jìn)行隨機(jī)排序類實(shí)例
這篇文章主要介紹了C#實(shí)現(xiàn)對(duì)數(shù)組進(jìn)行隨機(jī)排序類,實(shí)例分析了C#數(shù)組與隨機(jī)數(shù)操作技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-03-03
漢字轉(zhuǎn)拼音縮寫示例代碼(Silverlight和.NET 將漢字轉(zhuǎn)換成為拼音)
本篇文章主要介紹了漢字轉(zhuǎn)拼音縮寫示例代碼(Silverlight和.NET 將漢字轉(zhuǎn)換成為拼音) 需要的朋友可以過來參考下,希望對(duì)大家有所幫助2014-01-01
c# 使用WebRequest實(shí)現(xiàn)多文件上傳
這篇文章主要介紹了c# 使用WebRequest實(shí)現(xiàn)多文件上傳的方法,幫助大家更好的理解和學(xué)習(xí)使用c#,感興趣的朋友可以了解下2021-03-03

