在Android中对位图图像进行动画处理

aag*_*m94 3 animation geometry android bitmap

我创建了一个位图图像,它是一个圆形,并且比我想对其进行动画处理,因此我将其转换为bitmapdrawable,然后将其添加到动画drawable中。但是由于此,圆形变成了椭圆形...

所以我该怎么做 ?

是否有其他方法可以仅对位图文件进行动画处理。?

提前致谢..

Red*_*rav 5

如果您使用的是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);
...
Run Code Online (Sandbox Code Playgroud)

通过指向您感兴趣的框架来选择currentFrame。

Bitmap currentBitmap = frame[3]; // 4th frame
Run Code Online (Sandbox Code Playgroud)

因此,当您调用drawBitmap(currentBitmap)时,它只会绘制您感兴趣的帧。您可以通过为帧动画分配fps,每隔这么多帧更改位图。

如果只想缩放或旋转位图(旋转圆?),则调整位图大小的最佳方法是使用createScaledBitmap,然后使用矩阵旋转。

对于缩放,您可以像这样将任何位图加载到内存中

Bitmap circleBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.circle);
Run Code Online (Sandbox Code Playgroud)

如果您希望圆(或任何位图)重新缩放,请执行以下操作:

Bitmap scaledCircle = Bitmap.createScaledBitmap(circleBitmap, dstWidth, dstHeight, filter);
Run Code Online (Sandbox Code Playgroud)

其中dstWidth和dstHeight是目标目标宽度和高度,您可以事先通过缩放原始宽度和高度来计算它们。

int scaledHeight = circleBitmap.getHeight()/2;
int scaledWidth = circleBitmap.getWidth()/2;
Run Code Online (Sandbox Code Playgroud)

最后,您通常会使用这样的画布绘制此位图

canvas.drawBitmap(bitmap)
Run Code Online (Sandbox Code Playgroud)

为了旋转,创建一个矩阵

Matrix mat;
mat.postRotate(degrees); // Rotate the matrix
Bitmap rotatedBitmap = Bitmap.createBitmap(originalBitmap, x, y, width, height, mat, filter);
Run Code Online (Sandbox Code Playgroud)

最后

canvas.drawBitmap(rotatedBitmap);
Run Code Online (Sandbox Code Playgroud)

请记住,画布对于游戏或任何实时性都很慢!

希望能帮助到你。