3

我创建了一个位图图像,它是一个圆形,而不是我想要对其进行动画处理,因此我将其转换为 bitmapdrawable 并将其添加到动画 drawable 中。但由于这个原因,圆形已变为椭圆形...

所以我该怎么做 ?

有没有其他方法可以只为位图文件设置动画。?

提前致谢..

4

2 回答 2

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);
...

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

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.drawBitmap(bitmap)

对于旋转,创建一个矩阵

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

最后

canvas.drawBitmap(rotatedBitmap);

请记住,画布对于游戏或任何实时的东西来说都很慢!

希望能帮助到你。

于 2012-07-14T10:36:32.027 回答
4

不,你不能bitmapandroid animation framwork. 您可以直接为Views 或s 以及从和ViewGroup派生的所有类设置动画。ViewViewGroup

调用包含位图的viewImageView

TranslateAnimation slide = new TranslateAnimation(view.getX(), view.getX()  + 100, view.getY(), view.getY()  + 100 );   
slide.setDuration(1000);
view.startAnimation(slide) 

应该从当前位置翻译ImageViewby100px

于 2012-07-14T10:35:31.317 回答