我创建了一个位图图像,它是一个圆圈,因为我想对其进行动画处理,所以我将其转换为 bitmapdrawable,然后将其添加到动画可绘制对象中。但由于这个原因,圆形已变为椭圆形。 .
那我该怎么办呢?
是否有任何其他方法可以仅对位图文件进行动画处理。 ?
提前致谢..
最佳答案
如果您使用的是 Canvas,我建议保存一个指向当前位图的指针并将所有其他位图加载到一个数组中。
说,
Bitmap[] frames = new Bitmap[10] //10 frames
Bitmap frame[0] = BitmapFactory.decodeResource(getResources(), R.drawable.circlefram1);
Bitmap frame[1] = BitmapFactory.decodeResource(getResources(), R.drawable.circlefram2);
...
通过指向您感兴趣的帧来选择当前帧。
Bitmap currentBitmap = frame[3]; // 4th frame
因此,当您调用 drawBitmap(currentBitmap) 时,它只会绘制您感兴趣的帧。您可以通过为帧动画分配 fps,每隔这么多帧更改位图。
如果您只想缩放或旋转位图(旋转一个圆圈?),调整位图大小的最佳方法是使用 createScaledBitmap,然后使用矩阵旋转。
对于缩放,您可以像这样将任何位图加载到内存中
Bitmap circleBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.circle);
如果你想重新缩放圆(或任何位图),你可以这样做:
Bitmap scaledCircle = Bitmap.createScaledBitmap(circleBitmap, dstWidth, dstHeight, filter);
其中 dstWidth 和 dstHeight 是目标目标宽高,您可以通过缩放原始宽高来预先计算。
int scaledHeight = circleBitmap.getHeight()/2;
int scaledWidth = circleBitmap.getWidth()/2;
最后,您通常会使用这样的 Canvas 绘制此位图
canvas.drawBitmap(bitmap)
为了旋转,创建一个矩阵
Matrix mat;
mat.postRotate(degrees); // Rotate the matrix
Bitmap rotatedBitmap = Bitmap.createBitmap(originalBitmap, x, y, width, height, mat, filter);
最后
canvas.drawBitmap(rotatedBitmap);
请记住,Canvas 对于游戏或任何实时的东西来说都很慢!
希望对您有所帮助。
关于android - 在android中动画位图图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11482955/