简体   繁体   English

创建其他图像的一部分的图像

[英]Create image of part of an other image

I'm trying to draw a sprite sheet animation on the screen this worked fine before when I was using this code. 我正在尝试在屏幕上绘制Sprite Sheet动画,这在使用此代码之前效果很好。

    canvas.drawColor(Color.TRANSPARENT);
    Rect src = new Rect(CurrentSprite.x * SheetSize.x, CurrentSprite.y * SheetSize.y, (CurrentSprite.x * SheetSize.x) + SheetSize.x, (CurrentSprite.y * SheetSize.y) + SheetSize.y);
    Rect dst = new Rect(X, Y, X + SheetSize.x, Y + SheetSize.y);
    canvas.drawBitmap(bmp, src, dst, null);

However I now need to rotate the image before I draw it on the screen. 但是,现在我需要先旋转图像,然后再将其绘制在屏幕上。 So I changed the code so I'm making a local copy of the sprite. 因此,我更改了代码,以制作Sprite的本地副本。 I'm going to draw this is however here I get a problem. 我要画这个,但是这里有一个问题。 I'm using the Bitmap.createBitmap method but it seems this method does nothing because when I look at the screen the complete sprite sheet is drawn on the screen and sometimes I'm getting OutOfMemoryException (understandable because the full image is quite large). 我正在使用Bitmap.createBitmap方法,但该方法似乎无能为力,因为当我看着屏幕时,会在屏幕上绘制完整的Sprite表,有时会遇到OutOfMemoryException(这是可以理解的,因为整个图像非常大)。

    canvas.drawColor(Color.TRANSPARENT);
    Matrix matrix = new Matrix();
    matrix.postRotate(Degrees);
    Bitmap tempAnimationBmp = Bitmap.createBitmap(bmp, CurrentSprite.x * SheetSize.x, CurrentSprite.y * SheetSize.y, (CurrentSprite.x * SheetSize.x) + SheetSize.x, (CurrentSprite.y * SheetSize.y) + SheetSize.y, matrix, true);
    canvas.drawBitmap(tempAnimationBmp, X, Y, null);
    tempAnimationBmp.recycle();

Why do I get the full sprite sheet? 为什么我可以获得完整的Sprite表?

I'd recommend using one of the DrawableContainer s instead of trying to create a bunch of bitmaps. 我建议使用DrawableContainer之一,而不是尝试创建一堆位图。 Drawables are really easy to use - you just set the dimensions, pass it a canvas and it'll draw itself to the canvas at that location. 可绘制对象真的很容易使用-只需设置尺寸,将其传递给画布,它就会在该位置将自身绘制到画布上。 The great thing about Drawables is that they're also very easy to make, so you could create a custom SpriteDrawable class which takes a resource from the system and only draws regions of it to the canvas. Drawables的伟大之处在于它们也很容易制作,因此您可以创建一个自定义SpriteDrawable类,该类从系统中获取资源,仅将其区域绘制到画布上。

By working on this flow instead of trying to create a bunch of bitmaps you'll save a ton of memory and probably a lot of headache as you try to work more with Android's graphics system. 通过处理此流程而不是尝试创建一堆位图,您将节省大量内存,并且在尝试与Android的图形系统一起使用时可能会很头疼。

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

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