• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Live wallpaper drawing issue

punky

Well-Known Member
Hi all.

I'm tryin to create a live wallpaper for a visually-impaired friend that shows the current time.

I've based on off the sdK example and it works fine at first. However after 4 seconds later it starts writing another (different) number on top of the existing number. Its like more threads are being created somewhere or they aren't being cancelled by removeCallbacks. Or the screen isn't being cleared when it should.

Many thanks in advance!

Code:
package com.example.helloandroid; 
 
import java.text.SimpleDateFormat; 
import java.util.Date; 
 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.os.Handler; 
import android.service.wallpaper.WallpaperService; 
import android.view.SurfaceHolder; 
 
 
 
public class HelloAndroid extends WallpaperService { 
 
     private final Handler handler = new Handler(); 
 
        @Override 
         public void onCreate() { 
             super.onCreate(); 
         } 
        
         @Override 
         public void onDestroy() { 
             super.onDestroy(); 
         } 
 
          
        @Override 
         public Engine onCreateEngine() { 
             return new ClockEngine(); 
         } 
 
 
 
     class ClockEngine extends Engine { 
           
          private final Paint mPaint = new Paint(); 
          private boolean mVisible; 
          private final Runnable writeTimeRunnable = new Runnable() { 
               public void run() { 
                    drawFrame(); 
               } 
          }; 
 
        
          ClockEngine() { 
            final Paint paint = mPaint; 
            paint.setColor(Color.YELLOW); 
            paint.setAntiAlias(true); 
            paint.setStrokeWidth(2); 
            paint.setStrokeCap(Paint.Cap.ROUND); 
            paint.setStyle(Paint.Style.STROKE); 
            paint.setTextSize(80); 
            
 
        } 
           
        @Override 
        public void onCreate(SurfaceHolder surfaceHolder) { 
            super.onCreate(surfaceHolder); 
        } 
        
        @Override 
        public void onSurfaceCreated(SurfaceHolder holder) { 
            super.onSurfaceCreated(holder); 
        } 
        
        @Override 
        public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
            super.onSurfaceChanged(holder, format, width, height); 
 
            drawFrame(); 
        } 
        
        @Override 
        public void onOffsetsChanged(float xOffset, float yOffset, 
                float xStep, float yStep, int xPixels, int yPixels) { 
            drawFrame(); 
        } 
        
        @Override 
        public void onDestroy() { 
            super.onDestroy(); 
            handler.removeCallbacks(writeTimeRunnable); 
        } 
        
        @Override 
        public void onSurfaceDestroyed(SurfaceHolder holder) { 
            super.onSurfaceDestroyed(holder); 
            mVisible = false; 
            handler.removeCallbacks(writeTimeRunnable); 
        } 
        
 
        @Override 
        public void onVisibilityChanged(boolean visible) { 
            mVisible = visible; 
            if (visible) { 
                drawFrame(); 
            } else { 
                handler.removeCallbacks(writeTimeRunnable); 
            } 
        } 
 
 
     public void drawFrame() { 
 
    final SurfaceHolder holder = getSurfaceHolder(); 
 
    Canvas c = null; 
    
    try { 
        c = holder.lockCanvas(); 
        if (c != null) { 
            // draw something 
            writeTime(c); 
        } 
    } finally { 
        if (c != null) holder.unlockCanvasAndPost(c); 
    } 
 
    // Reschedule the next redraw 
    handler.removeCallbacks(writeTimeRunnable); 
    if (mVisible) { 
        handler.postDelayed(writeTimeRunnable, 1000); 
    } 
      
     } 
      
     public void writeTime(Canvas c) 
     { 
           
          String hour=new SimpleDateFormat("H").format(new Date()); 
          String min=new SimpleDateFormat("mm").format(new Date()); 
          String sec=new SimpleDateFormat("ss").format(new Date());    
 
          c.drawText(hour + ":" + min + ":" + sec, 150, 150, mPaint); 
           
     } 
      
     } 
}
 
Well I have sorted it. In the writeTime function I removed this line from the Cube example:

c.drawColor(0xff000000);


I thought it set the colour of the brush/lines so removed it, but its actually referencing the Android canvas background and sets the colour of it - basically clearing the old graphics. Put it in and it works.

Hope this helps someone else.
 
Back
Top Bottom