绘制高分辨率的动画与Android的高帧率 [英] Draw high res animations with high frame rate on Android
问题描述
我已经得到了30+的单位图(320x240像素),我想在导致动画Android设备全屏显示一个接一个。目前我实现使用的ImageView和一个定时器,用于设置下一帧,然后发送一个消息,将适用于下一帧的动画。由此产生的帧率非常低:其中, 2帧。
I've got 30+ single bitmaps (320x240 pixels) that I would like to display one after another in full screen on Android devices resulting in an animation. Currently I implemented the animation using an ImageView and a Timer that sets the next frame and then sends a message that will apply the next frame. The resulting frame rate is very low: < 2 fps.
定时器:的
animationTimer.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
Drawable frame = getNextFrame();
if (frame != null) {
Message message = animationFrameHandler.obtainMessage(1, frame);
animationFrameHandler.sendMessage(message);
}
}
}, 0, (int) (1000.0d / fps));
的处理程序:的
final Handler animationFrameHandler = new Handler() {
@Override
public void handleMessage(Message message) {
setImageDrawable((Drawable) message.obj);
}
};
因为我想达到的帧速率高达每秒30帧我必须使用另一种机制,听到的 Canvas.drawBitmapMesh()和的OpenGL
如果可能,我想避免使用OpenGL。
If possible I would like to avoid using OpenGL.
非常感谢您分享您的经验!
Thank you very sharing your experiences!
推荐答案
我现在的工作方式如下:
My now working approach is the following:
在开始动画之前,加载每一帧分成名单,其中,位图&GT;
。重要提示:呼叫的System.gc()
如果您收到的OutOfMemoryError
秒 - 这确实有助于装载更多的位图到记忆。然后有一个线程运行的帖子下一帧到一个视图实例然后更新它的画布。
Before starting the animation, load every frame into a List<Bitmap>
. Important: Call System.gc()
if you're getting OutOfMemoryError
s – that really helps loading more bitmaps into the memory. Then have a thread running that posts the next frame to a View instance that then update it's canvas.
加载帧并启动动画
// Loading the frames before starting the animation
List<Bitmap> frames = new ArrayList<Bitmap>();
for (int i = 0; i < 30; i++) {
// Load next frame (e. g. from drawable or assets folder)
frames.add(...);
// Do garbage collection every 3rd frame; really helps loading all frames into memory
if (i %% 3 == 0) {
System.gc();
}
}
// Start animation
frameIndex = 0;
animationThread.start();
线程应用下架
private final class AnimationThread extends Thread {
@Override
public void run() {
while (!isInterrupted()) {
// Post next frame to be displayed
animationView.postFrame(frames.get(frameIndex));
// Apply next frame (restart if last frame has reached)
frameIndex++;
if (frameIndex >= frames.size()) {
frameIndex = 0;
}
try {
sleep(33); // delay between frames in msec (33 msec mean 30 fps)
} catch (InterruptedException e) {
break;
}
}
}
}
动画视图
class AnimationView extends View {
Bitmap frame = null;
public void postFrame(Bitmap frame) {
Message message = frameHandler.obtainMessage(0, frame);
frameHandler.sendMessage(message);
}
protected final Handler frameHandler = new Handler() {
@Override
public void handleMessage(Message message) {
if (message.obj != null) {
frame = (Bitmap) message.obj;
} else {
frame = null;
}
invalidate();
}
}
@Override
protected void onDraw(Canvas canvas) {
if (frame == null) return;
canvas.drawARGB(0, 0, 0, 0);
canvas.drawBitmap(frame, null, null, null);
}
}
这篇关于绘制高分辨率的动画与Android的高帧率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!