开发者

How can I efficiently draw many pixels on a Canvas?

I'm making my first game using Java on Android. I need to draw a lot of pixels which together should create a line. My first approach was to make a large array of booleans, create a loop, and draw a pixel when the associated boolean was true.

It wasn't a good idea of course (the array is about 200x300). Now I remember only the position of the first pixel of the line, and every next pixel has to remember his follower. It works pretty well, but when the line gets longer (but still not very long), the efficiency is bad (<20 fps after 4000 frames).

This is the function that I use to draw a line (only one for now). Can anybody help me improve its efficiency?

public void drawLine(Canvas canvas, int beginx, int beginy) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.RED);
    paint.setStrokeWidth(3);

    int x = beginx;
    int y = beg开发者_JAVA百科iny;
    while(C.mGrid[x][y].nx != -1) {
        //canvas.drawLine(x, y, C.mGrid[x][y].nx, C.mGrid[x][y].ny, paint);
        canvas.drawPoint(x, y, paint);
        Grid temp = C.mGrid[x][y];
        if ((C.mGrid[x][y].nx == x) && (C.mGrid[x][y].ny == y)) break;
        x = temp.nx;
        y = temp.ny;
    }
}

and Grid.java:

package com.qwak.achtung;

public float x = 0,y = 0;
public int px = -1, py = -1, nx = -1, ny = -1;

public Grid(float x, float y) {
    this.x = x;
    this.y = y;
}

public void set(int px, int py, int nx, int ny) {
    this.px = px;
    this.py = py;
    this.nx = nx;
    this.ny = ny;
}

public void setp(int px, int py) {
    this.px = px;
    this.py = py;
}

public void setn(int nx, int ny) {
    this.nx = nx;
    this.ny = ny;
}

PS: It looks like this http://c.wrzuta.pl/wi10559/11f7d10b00110e504e25ebd3/0/andek 14 is fps (on my phone (samsung Spica) it run better - 40 but after a while it decreases to 20 and even less) and 983 is number of frames at all.


There is a drawLine method in the canvas object.

Use the example here: How to draw a line in android

canvas.drawLine(0, 0, 20, 20, paint);

If you want to draw a curve. Find the function of the curve. A Parabola for example is x=y^2. You can get points from the curve: 1 = 1, 2 = 4, 3 = 9, 4 = 16... etc.. If your drawing pixel by pixel you can plug in your x and get your y and draw it.

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setColor(Color.RED); paint.setStrokeWidth(3);

for(int i = beginx; i < CanvasWidth; i++)
{
    int x = i;
    int y = i * i; //x=y^2
    canvas.drawPoint(x, y, paint);
}

To keep a record of points that were visited you could do the following:

class Point
{
    int x;
    int y;
}


List<Point> points = new List<Point>();

onMove(int newX, int newY)
{
    Point p = new Point();
    p.x = newX;
    p.y = newY;

    points.add(p);
}


onDraw()
{
    for(Point p : points)
    {
        canvas.drawPoint(p.x, p.y, paint);
    }
}


You want to look into the bresenham algorithm. A bresenham algorithm is a method to draw or rasterize a line. It's a bit different from the subdivision of a grid in a certain angle for example a morton-curve. It's a bit like compute the scalar product for every angle like recall here Traversing a 2D array in an angle.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜