Android快速像素操作
我正在尝试编写一些代码,让我在屏幕上绘制像素数组。然而,我在 Arm cortex A8 600mhz、320x240 像素下的性能非常糟糕。
有人可以向我解释瓶颈在哪里和/或如何解决它吗?改变高度和宽度也几乎没有帮助。我注意到 Java 中的 Swing 有类似的行为。我正在使用下面的代码:
package com.pixeldraw;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Debug;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new Panel(this));
Debug.startMethodTracing("pixeldraw");
}
@Override
protected void onDestroy() {
super.onDestroy();
Debug.stopMethodTracing();
}
class Panel extends SurfaceView implements SurfaceHolder.Callback {
private TutorialThread _thread;
private Bitmap _buffer;
private ArrayList<GraphicObject> _graphics = new ArrayList<GraphicObject>();
private int width = 40;
private int height = 40;
public Panel(Context context) {
super(context);
getHolder().addCallback(this);
_thread = new TutorialThread(getHolder(), this);
setFocusable(true);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
synchronized (_thread.getSurfaceHolder()) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
GraphicObject graphic = new GraphicObject(BitmapFactory.decodeResource(getResources(), R.drawable.icon));
graphic.getCoordinates().setX((int) event.getX() - graphic.getGraphic().getWidth() / 2);
graphic.getCoordinates().setY((int) event.getY() - graphic.getGraphic().getHeight() / 2);
_graphics.add(graphic);
}
return true;
}
}
long lastTime = System.currentTimeMillis();
int CLOCKS_PER_SEC = 500;
float timeCnt = 0;
@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
width = getWidth()/2;
height = getHeight()/2;
long frameTime = System.currentTimeMillis();
// The elapsed seconds per frame will almost always be less than 1.0.
float elapsedSeconds = (float)(frameTime - lastTime) / CLOCKS_PER_SEC;
int colors[] = new int[width*height];
for (int x = 0; x<width ; x++) {
for (int y = 0; y<height ; y++){
int r = (int)(timeCnt/5*255);
int b = 0;
int g = 0;
int a = 255;
colors[x + y * width] = (a << 24) | (r << 16) | (g << 8) | b;
}
}
for (int x = 0; x<10 ; x++) {
for (int y = 0; y<10; y++){
int r = 0;
int b = 255;
int g = 0;
int a = 255;
colors[(int)(timeCnt*32)+x + y * width] = (a << 24) | (r << 16) | (g << 8) | b;
}
}
_buffer = Bitmap.createBitmap(colors, width, height, Bitmap.Config.RGB_565);
canvas.drawBitmap(_buffer, 0, 0, null);
timeCnt += elapsedSeconds;
if (timeCnt > 5) timeCnt = 0;
// Update the last time counter so that we can use it next frame.
lastTime = frameTime;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
_thread.setRunning(true);
_thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// simply copied from sample application LunarLander:
// we have to tell thread to shut down & wait for it to finish, or else
// it might touch the Surface after we return and explode
boolean retry = true;
_thread.setRunning(false);
while (retry) {
try {
_thread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
}
class TutorialThread extends Thread {
private SurfaceHolder _surfaceHolder;
private Panel _panel;
private boolean _run = false;
public TutorialThread(SurfaceHolder surfaceHolder, Panel panel) {
_surfaceHolder = surfaceHolder;
_panel = panel;
}
public void setRunning(boolean run) {
_run = run;
}
public SurfaceHolder getSurfaceHolder() {
return _surfaceHolder;
}
@Override
public void run() {
Canvas c;
while (_run) {
c = null;
try {
c = _surfaceHolder.lockCanvas(null);
synchronized (_surfaceHolder) {
_panel.onDraw(c);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
class GraphicObject {
/**
* Contains the coordinates of the graphic.
*/
public class Coordinates {
private int _x = 100;
private int _y = 0;
public int getX() {
return _x + _bitmap.getWidth() / 2;
}
public void setX(int value) {
_x = value - _bitmap.getWidth() / 2;
}
public int getY() {
return _y + _bitmap.getHeight() / 2;
}
public void setY(int value) {
_y = value - _bitmap.getHeight() / 2;
}
public String toString() {
return "Coordinates: (" + _x + "/" + _y + ")";
}
}
private Bitmap _bitmap;
private Coordinates _coordinates;
public GraphicObject(Bitmap bitmap) {
_bitmap = bitmap;
_coordinates = new Coordinates();
}
public Bitmap getGraphic() {
return _bitmap;
}
public Coordinates getCoordinates() {
return _coordinates;
}
}
}
I'm trying to write some code that let me draw an array of pixels to the screen. However I get very louzy performance on my Arm cortex A8 600mhz at 320x240 pixels.
Can someone explain to me where the bottleneck is and/or how to fix it? Changing the height and width barely helps too. I noticed similar behaviour with Swing in Java. I'm using code below:
package com.pixeldraw;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Debug;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new Panel(this));
Debug.startMethodTracing("pixeldraw");
}
@Override
protected void onDestroy() {
super.onDestroy();
Debug.stopMethodTracing();
}
class Panel extends SurfaceView implements SurfaceHolder.Callback {
private TutorialThread _thread;
private Bitmap _buffer;
private ArrayList<GraphicObject> _graphics = new ArrayList<GraphicObject>();
private int width = 40;
private int height = 40;
public Panel(Context context) {
super(context);
getHolder().addCallback(this);
_thread = new TutorialThread(getHolder(), this);
setFocusable(true);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
synchronized (_thread.getSurfaceHolder()) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
GraphicObject graphic = new GraphicObject(BitmapFactory.decodeResource(getResources(), R.drawable.icon));
graphic.getCoordinates().setX((int) event.getX() - graphic.getGraphic().getWidth() / 2);
graphic.getCoordinates().setY((int) event.getY() - graphic.getGraphic().getHeight() / 2);
_graphics.add(graphic);
}
return true;
}
}
long lastTime = System.currentTimeMillis();
int CLOCKS_PER_SEC = 500;
float timeCnt = 0;
@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
width = getWidth()/2;
height = getHeight()/2;
long frameTime = System.currentTimeMillis();
// The elapsed seconds per frame will almost always be less than 1.0.
float elapsedSeconds = (float)(frameTime - lastTime) / CLOCKS_PER_SEC;
int colors[] = new int[width*height];
for (int x = 0; x<width ; x++) {
for (int y = 0; y<height ; y++){
int r = (int)(timeCnt/5*255);
int b = 0;
int g = 0;
int a = 255;
colors[x + y * width] = (a << 24) | (r << 16) | (g << 8) | b;
}
}
for (int x = 0; x<10 ; x++) {
for (int y = 0; y<10; y++){
int r = 0;
int b = 255;
int g = 0;
int a = 255;
colors[(int)(timeCnt*32)+x + y * width] = (a << 24) | (r << 16) | (g << 8) | b;
}
}
_buffer = Bitmap.createBitmap(colors, width, height, Bitmap.Config.RGB_565);
canvas.drawBitmap(_buffer, 0, 0, null);
timeCnt += elapsedSeconds;
if (timeCnt > 5) timeCnt = 0;
// Update the last time counter so that we can use it next frame.
lastTime = frameTime;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
_thread.setRunning(true);
_thread.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// simply copied from sample application LunarLander:
// we have to tell thread to shut down & wait for it to finish, or else
// it might touch the Surface after we return and explode
boolean retry = true;
_thread.setRunning(false);
while (retry) {
try {
_thread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}
}
class TutorialThread extends Thread {
private SurfaceHolder _surfaceHolder;
private Panel _panel;
private boolean _run = false;
public TutorialThread(SurfaceHolder surfaceHolder, Panel panel) {
_surfaceHolder = surfaceHolder;
_panel = panel;
}
public void setRunning(boolean run) {
_run = run;
}
public SurfaceHolder getSurfaceHolder() {
return _surfaceHolder;
}
@Override
public void run() {
Canvas c;
while (_run) {
c = null;
try {
c = _surfaceHolder.lockCanvas(null);
synchronized (_surfaceHolder) {
_panel.onDraw(c);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
class GraphicObject {
/**
* Contains the coordinates of the graphic.
*/
public class Coordinates {
private int _x = 100;
private int _y = 0;
public int getX() {
return _x + _bitmap.getWidth() / 2;
}
public void setX(int value) {
_x = value - _bitmap.getWidth() / 2;
}
public int getY() {
return _y + _bitmap.getHeight() / 2;
}
public void setY(int value) {
_y = value - _bitmap.getHeight() / 2;
}
public String toString() {
return "Coordinates: (" + _x + "/" + _y + ")";
}
}
private Bitmap _bitmap;
private Coordinates _coordinates;
public GraphicObject(Bitmap bitmap) {
_bitmap = bitmap;
_coordinates = new Coordinates();
}
public Bitmap getGraphic() {
return _bitmap;
}
public Coordinates getCoordinates() {
return _coordinates;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据我们公司的经验 - 在 Android 上,近全屏位图上的逐像素操作非常慢。我们对某种自定义动画进行的最慢的操作之一是通过drawColor()将背景设为黑色,这正是逐像素填充屏幕......
所以在你的情况下,罪魁祸首可能是
但确实- 进行分析,你就会知道。通过分析,您可以深入了解 onDraw 并查看哪个方法花费最多时间。我们就是这样发现的。
From our company experience - pixel-by-pixel operations on near-full-screen bitmaps are extremely slow on android. One of the slowest operations we had on a custom animation of some sort was to make the background black by drawColor() which is exactly doing pixel-by-pixel filling of the screen...
So in your case probably the culprit is
But indeed - do profiling and you will know. With profiling you can drill down onDraw and see which method takes most time. This is how we found out.
准确...告诉一些数字,而不是非常糟糕
移动昂贵的绘图代码之外的操作
int 颜色[] = new int[宽度*高度];
_buffer = Bitmap.createBitmap(colors, width, height, Bitmap.Config.RGB_565);
使用分析http://developer.android.com/guide/ Development/debugging/debugging-tracing.html
Be precise... tell some numbers instead of very louzy
Move expensive operations outside of your drawing code
int colors[] = new int[width*height];
_buffer = Bitmap.createBitmap(colors, width, height, Bitmap.Config.RGB_565);
Use profiling http://developer.android.com/guide/developing/debugging/debugging-tracing.html