开发者

LiveWallpaper with SurfaceHolder.lockCanvas(Rect dirty)

I would like to ask about a problem that has been addressed here once or twice, but none of information I found could help me overcome the problem I faced a few days ago.

I want to make a live wallpaper for android using canvases - it is not graphically complicated enough to require OpenGL. For simplicity assume it consists of solid background and two smaller rectangles. Drawing consists of three separate stages (in single thread):

  1. backgroundDraw() requests entire canvas lock and draws on it solid color
  2. draw1() requests partial (Rect r1) lock and draws only on locked rectangle
  3. draw2() requests partial (Rect r2) lock and draws only on locked rectangle

I tested it on multiple Android versions (both emulators and devices): 2.1, 2.2, 2.3.3. It seems to be working properly only on the latter one (here: http://home.elka.pw.edu.pl/~pgawron/include/Android/android_233.jpg). On previous Android versions SurfaceHolder.lockCanvas(Rect dirty) resizes(!) dirty passed as parameter to size of the full screen and further drawing using it results drawing on the whole screen (here: http://home.elka.pw.edu.pl/~pgawron/include/Android/android_22.jpg). I can in fact see how each rectangle is being ill-drawn (full-screen): whole screen changes it's color very quickly.

Unluckily google could not find for me any proper example of lockCanvas(Rect dirty) usage. Below I attach my complete and only class used for testing purposes. Full eclipse project is accessible just where provided screenshots are placed.

I would be extremely grateful if somebody could finally help me and correct my code (if only the problem is in my code). I really wasted too much time on it.

BR,

petrelli

package sec.polishcode.test;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.SystemClock;
import android.service.wallpaper.WallpaperService;
import android.util.Log;
import android.view.SurfaceHolder;

public class TestLiveWallpaper extends WallpaperService{

@Override
public Engine onCreateEngine() {
    return new MyEngine();
}

class MyEngine extends Engine implements SurfaceHolder.Callback {

    private final String LOGTAG = MyEngine.class.getSimpleName();
    private Paint backgroundPaint = new Paint();
    private Paint mPaint1 = new Paint();
    private Paint mPaint2 = new Paint();
    private long lastVisibilityOnChange;

    private final Rect r1 = new Rect(20, 20, 60, 280);
    private final Rect r2 = new Rect(70, 20, 110, 280);

    public MyEngine() {

        getSurfaceHolder().addCallback(this);

        backgroundPaint.setColor(Color.YELLOW);
        mPaint1.setColor(Color.LTGRAY);
        mPaint2.setColor(Color.MAGENTA);
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2,
            int arg3) {
        drawSurface();
    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
        Log.i(LOGTAG, "surfaceCreated");
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        Log.i(LOGTAG, "surfaceDestroyed");
    }

    @Override
    public void onCreate(SurfaceHolder surfaceHolder) {
        super.onCreate(surfaceHolder);

        setTouchEventsEnabled(true);
    }

    @Override
    public void onVisibilityChanged(boolean visible) {
        if (!visible)
            return;

        lastVisibilityOnChange = SystemClock.elapsedRealtime();
        drawSurface();
    }

    @Override
    public void onOffsetsChanged(float xOffset, float yOffset, float xStep,
            float yStep, int xPixels, int yPixels) {

        if (SystemClock.elapsedRealtime() - lastVisibil开发者_C百科ityOnChange > 30)
            return;

        Log.i(LOGTAG, "onOffsetsChanged filtered");
        drawSurface();
    }

    private void drawSurface() {
        backgroundDraw();
        draw1();
        draw2();
    }

    private void backgroundDraw() {
        final SurfaceHolder holder = getSurfaceHolder();

        Canvas c = null;
        try {
            c = holder.lockCanvas();
            if (c != null) {
                c.drawRect(holder.getSurfaceFrame(), backgroundPaint);
            }
        } finally {
            if (c != null)
                holder.unlockCanvasAndPost(c);
        }
    }

    private void draw1() {
        final SurfaceHolder holder = getSurfaceHolder();

        Canvas c = null;
        try {
            c = holder.lockCanvas(r1);
            if (c != null) {
                c.drawRect(r1, mPaint1);
            }
        } finally {
            if (c != null)
                holder.unlockCanvasAndPost(c);
        }
    }

    private void draw2() {
        final SurfaceHolder holder = getSurfaceHolder();

        Canvas c = null;
        try {
            c = holder.lockCanvas(r2);
            if (c != null) {
                c.drawRect(r2, mPaint2);
            }
        } finally {
            if (c != null)
                holder.unlockCanvasAndPost(c);
        }
    }
}
}


lockCanvas(Rect dirty) is pretty simple. Remember that on Android surfaces are double-buffered by default. This means that you need to not only repaint the dirty region of the current surface, but also the dirty region of the previous surface to make it work properly. This is why lockCanvas() will resize the rectangle you pass: it tells what the real dirty region is. The dirty region might also change because the surface was discarded and recreated, etc. The correct way to use lockCanvas(Rect) is to pass your dirty rectangle and then check its new values and honor them.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜