Live wallpaper drawing issue


Last Updated:

  1. punky

    punky Well-Known Member This Topic's Starter

    Joined:
    Apr 5, 2010
    Messages:
    116
    Likes Received:
    7
    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 (Text):
    1.  
    2. package com.example.helloandroid;
    3.  
    4. import java.text.SimpleDateFormat;
    5. import java.util.Date;
    6.  
    7. import android.graphics.Canvas;
    8. import android.graphics.Color;
    9. import android.graphics.Paint;
    10. import android.os.Handler;
    11. import android.service.wallpaper.WallpaperService;
    12. import android.view.SurfaceHolder;
    13.  
    14.  
    15.  
    16. public class HelloAndroid extends WallpaperService {
    17.  
    18.      private final Handler handler = new Handler();
    19.  
    20.         @Override
    21.          public void onCreate() {
    22.              super.onCreate();
    23.          }
    24.        
    25.          @Override
    26.          public void onDestroy() {
    27.              super.onDestroy();
    28.          }
    29.  
    30.          
    31.         @Override
    32.          public Engine onCreateEngine() {
    33.              return new ClockEngine();
    34.          }
    35.  
    36.  
    37.  
    38.      class ClockEngine extends Engine {
    39.            
    40.           private final Paint mPaint = new Paint();
    41.           private boolean mVisible;
    42.           private final Runnable writeTimeRunnable = new Runnable() {
    43.                public void run() {
    44.                     drawFrame();
    45.                }
    46.           };
    47.  
    48.        
    49.           ClockEngine() {
    50.             final Paint paint = mPaint;
    51.             paint.setColor(Color.YELLOW);
    52.             paint.setAntiAlias(true);
    53.             paint.setStrokeWidth(2);
    54.             paint.setStrokeCap(Paint.Cap.ROUND);
    55.             paint.setStyle(Paint.Style.STROKE);
    56.             paint.setTextSize(80);
    57.            
    58.  
    59.         }
    60.            
    61.         @Override
    62.         public void onCreate(SurfaceHolder surfaceHolder) {
    63.             super.onCreate(surfaceHolder);
    64.         }
    65.        
    66.         @Override
    67.         public void onSurfaceCreated(SurfaceHolder holder) {
    68.             super.onSurfaceCreated(holder);
    69.         }
    70.        
    71.         @Override
    72.         public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    73.             super.onSurfaceChanged(holder, format, width, height);
    74.  
    75.             drawFrame();
    76.         }
    77.        
    78.         @Override
    79.         public void onOffsetsChanged(float xOffset, float yOffset,
    80.                 float xStep, float yStep, int xPixels, int yPixels) {
    81.             drawFrame();
    82.         }
    83.        
    84.         @Override
    85.         public void onDestroy() {
    86.             super.onDestroy();
    87.             handler.removeCallbacks(writeTimeRunnable);
    88.         }
    89.        
    90.         @Override
    91.         public void onSurfaceDestroyed(SurfaceHolder holder) {
    92.             super.onSurfaceDestroyed(holder);
    93.             mVisible = false;
    94.             handler.removeCallbacks(writeTimeRunnable);
    95.         }
    96.        
    97.  
    98.         @Override
    99.         public void onVisibilityChanged(boolean visible) {
    100.             mVisible = visible;
    101.             if (visible) {
    102.                 drawFrame();
    103.             } else {
    104.                 handler.removeCallbacks(writeTimeRunnable);
    105.             }
    106.         }
    107.  
    108.  
    109.      public void drawFrame() {
    110.  
    111.     final SurfaceHolder holder = getSurfaceHolder();
    112.  
    113.     Canvas c = null;
    114.    
    115.     try {
    116.         c = holder.lockCanvas();
    117.         if (c != null) {
    118.             // draw something
    119.             writeTime(c);
    120.         }
    121.     } finally {
    122.         if (c != null) holder.unlockCanvasAndPost(c);
    123.     }
    124.  
    125.     // Reschedule the next redraw
    126.     handler.removeCallbacks(writeTimeRunnable);
    127.     if (mVisible) {
    128.         handler.postDelayed(writeTimeRunnable, 1000);
    129.     }
    130.      
    131.      }
    132.      
    133.      public void writeTime(Canvas c)
    134.      {
    135.            
    136.           String hour=new SimpleDateFormat("H").format(new Date());
    137.           String min=new SimpleDateFormat("mm").format(new Date());
    138.           String sec=new SimpleDateFormat("ss").format(new Date());    
    139.  
    140.           c.drawText(hour + ":" + min + ":" + sec, 150, 150, mPaint);
    141.            
    142.      }
    143.      
    144.      }
    145. }
     

    Advertisement
  2. punky

    punky Well-Known Member This Topic's Starter

    Joined:
    Apr 5, 2010
    Messages:
    116
    Likes Received:
    7
    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.
     

Share This Page

Loading...