简体   繁体   English

WPF Spritesheet动画

[英]WPF spritesheet animation

I am trying to create an animated sprite from a spritesheet in wpf. 我试图从WPF中的Spritesheet创建动画Sprite。 The current technique I am using is breaking down an image of containing all the frames of the animation into separate images and just swapping them out. 我当前使用的技术是将包含动画所有帧的图像分解为单独的图像,然后将它们交换出来。

However this is costly on memory and was wondering if there is a way to just draw a section of an image (specify the source rectangle) rather than splitting the image up. 但是,这在内存上成本很高,并且想知道是否有一种方法可以仅绘制图像的一部分(指定源矩形)而不是拆分图像。

You can definitely do this. 您绝对可以做到这一点。 In order for this to be supported by WPF's built-in animation system you need to animate a dependency property . 为了使WPF的内置动画系统能够支持此功能,您需要设置依赖项属性的动画。 In your case it seems you want to animate the SourceRect property of a CroppedBitmap using a RectAnimationUsingKeyFrames with the DiscreteRectKeyFrame class. 在您的情况下,您似乎想使用带有DiscreteRectKeyFrame类的RectAnimationUsingKeyFramesCroppedBitmapSourceRect属性设置动画。

If you're displaying the image using an Image element then you would set it's Source property to a CroppedBitmap , then the cropped bitmap would have its Source property set to a BitmapImage . 如果您使用的是显示图像Image元素,那么你会设置它的Source属性设置为CroppedBitmap ,然后裁剪位图将有它的Source属性设置为BitmapImage The cropped bitmap wraps the standard bitmap and adds cropping functionality. 裁剪后的位图将包装标准位图并添加裁剪功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM