Using java Syntax Highlighting
- import java.util.Random;
- 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.graphics.Paint;
- import android.graphics.PointF;
- import android.graphics.Rect;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.Surface;
- import android.view.View;
- public class DrawingTestActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(new DrawingTestView(getApplication()));
- }
- private static class DrawingTestView extends View {
- protected final int WIDTH = 320;
- protected final int HEIGHT = 240;
- protected final int NBALLS = 2;
- protected final float MAXBALLSIZE = 20.0f;
- protected PointF mBallPositions[];
- protected float mBallSizes[];
- protected Bitmap mBitmap;
- protected int[] mPixels;
- protected Random mRnd = new Random();
- public DrawingTestView(Context c) {
- super(c);
- mBitmap = Bitmap.createBitmap(WIDTH, HEIGHT, false);
- mPixels = new int[WIDTH * HEIGHT];
- mBallPositions = new PointF[NBALLS];
- mBallSizes = new float[NBALLS];
- for (int i = 0; i < NBALLS; i++) {
- mBallPositions[i] = new PointF(
- mRnd.nextFloat() * WIDTH,
- mRnd.nextFloat()* HEIGHT
- );
- mBallSizes[i] = mRnd.nextFloat() * MAXBALLSIZE;
- }
- }
- @Override
- protected void onDraw(Canvas canvas) {
- long sTime, uTime, cTime;
- sTime = System.currentTimeMillis();
- // Update pixel buffer
- for (int x = 0; x < WIDTH; x++) {
- for(int y = 0; y < HEIGHT; y++) {
- float fs = 0;
- for(int n = 0; n < NBALLS; n++) {
- fs += mBallSizes[n] /
- ((mBallPositions[n].x - x) * (mBallPositions[n].x - x)) +
- ((mBallPositions[n].y - y) * (mBallPositions[n].y - y));
- }
- int color = (fs > 0.5) ? 255 : 0;
- mPixels[x + (y * WIDTH)] = color;
- }
- }
- uTime = System.currentTimeMillis() - sTime;
- // Draw pixel buffer
- mBitmap.setPixels(mPixels, 0, WIDTH, 0, 0, WIDTH, HEIGHT);
- // Draw to canvas
- canvas.drawBitmap(mBitmap, 0, 0, null);
- cTime = System.currentTimeMillis() - sTime;
- // Invalidate for next redraw
- invalidate(0, 0, WIDTH, HEIGHT);
- Log.v("onDraw timing", "Frame:\tUT: " + uTime +"ms.\tCT:" + cTime + "ms.");
- }
- }
- }
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
It's meant to draw some metaballs (sqrt removed from equation to try speed things up).
In the emulator the update loop is taking around 2 seconds and the calls to setPixels and drawBitmap are taking a further 2 seconds... Now 0.25 fps is pretty/very rubbish.
Is there a faster way of doing this?


