Dear moderator,

I am really puzzled and perplexed. For about 4 days I have been
unsuccessfully trying to post my first ever question on developer's
mailing list. I have been using thunderbird to send the question at
[email protected] with no success of posting it.
This is the first time i use webpage. If there are still problems with
my question please at least tell me how to make it "postable". If
hopefully now it is ok, before posting it please remove the part you
are currently reading. Below is my question.

Best regards,
petrelli

Hi everyone,

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 it.

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:

   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. 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. 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.

I would be extremely grateful if somebody could finally help me and
correct my code (if only the problem is in my code).
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() - lastVisibilityOnChange
> 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);
            }
        }
    }
}

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to