Live wallpaper drawing issue.

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

Live wallpaper drawing issue.

Postby punky » Tue Apr 27, 2010 1:35 am

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!

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.example.helloandroid;
  3.  
  4.  
  5.  
  6. import java.text.SimpleDateFormat;
  7.  
  8. import java.util.Date;
  9.  
  10.  
  11.  
  12. import android.graphics.Canvas;
  13.  
  14. import android.graphics.Color;
  15.  
  16. import android.graphics.Paint;
  17.  
  18. import android.os.Handler;
  19.  
  20. import android.service.wallpaper.WallpaperService;
  21.  
  22. import android.view.SurfaceHolder;
  23.  
  24.  
  25.  
  26.  
  27.  
  28.  
  29.  
  30. public class HelloAndroid extends WallpaperService {
  31.  
  32.  
  33.  
  34.      private final Handler handler = new Handler();
  35.  
  36.  
  37.  
  38.         @Override
  39.  
  40.          public void onCreate() {
  41.  
  42.              super.onCreate();
  43.  
  44.          }
  45.  
  46.        
  47.  
  48.          @Override
  49.  
  50.          public void onDestroy() {
  51.  
  52.              super.onDestroy();
  53.  
  54.          }
  55.  
  56.  
  57.  
  58.          
  59.  
  60.         @Override
  61.  
  62.          public Engine onCreateEngine() {
  63.  
  64.              return new ClockEngine();
  65.  
  66.          }
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.      class ClockEngine extends Engine {
  75.  
  76.          
  77.  
  78.           private final Paint mPaint = new Paint();
  79.  
  80.           private boolean mVisible;
  81.  
  82.           private final Runnable writeTimeRunnable = new Runnable() {
  83.  
  84.                public void run() {
  85.  
  86.                     drawFrame();
  87.  
  88.                }
  89.  
  90.           };
  91.  
  92.  
  93.  
  94.        
  95.  
  96.           ClockEngine() {
  97.  
  98.             final Paint paint = mPaint;
  99.  
  100.             paint.setColor(Color.YELLOW);
  101.  
  102.             paint.setAntiAlias(true);
  103.  
  104.             paint.setStrokeWidth(2);
  105.  
  106.             paint.setStrokeCap(Paint.Cap.ROUND);
  107.  
  108.             paint.setStyle(Paint.Style.STROKE);
  109.  
  110.             paint.setTextSize(80);
  111.  
  112.            
  113.  
  114.  
  115.  
  116.         }
  117.  
  118.          
  119.  
  120.         @Override
  121.  
  122.         public void onCreate(SurfaceHolder surfaceHolder) {
  123.  
  124.             super.onCreate(surfaceHolder);
  125.  
  126.         }
  127.  
  128.        
  129.  
  130.         @Override
  131.  
  132.         public void onSurfaceCreated(SurfaceHolder holder) {
  133.  
  134.             super.onSurfaceCreated(holder);
  135.  
  136.         }
  137.  
  138.        
  139.  
  140.         @Override
  141.  
  142.         public void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height) {
  143.  
  144.             super.onSurfaceChanged(holder, format, width, height);
  145.  
  146.  
  147.  
  148.             drawFrame();
  149.  
  150.         }
  151.  
  152.        
  153.  
  154.         @Override
  155.  
  156.         public void onOffsetsChanged(float xOffset, float yOffset,
  157.  
  158.                 float xStep, float yStep, int xPixels, int yPixels) {
  159.  
  160.             drawFrame();
  161.  
  162.         }
  163.  
  164.        
  165.  
  166.         @Override
  167.  
  168.         public void onDestroy() {
  169.  
  170.             super.onDestroy();
  171.  
  172.             handler.removeCallbacks(writeTimeRunnable);
  173.  
  174.         }
  175.  
  176.        
  177.  
  178.         @Override
  179.  
  180.         public void onSurfaceDestroyed(SurfaceHolder holder) {
  181.  
  182.             super.onSurfaceDestroyed(holder);
  183.  
  184.             mVisible = false;
  185.  
  186.             handler.removeCallbacks(writeTimeRunnable);
  187.  
  188.         }
  189.  
  190.        
  191.  
  192.  
  193.  
  194.         @Override
  195.  
  196.         public void onVisibilityChanged(boolean visible) {
  197.  
  198.             mVisible = visible;
  199.  
  200.             if (visible) {
  201.  
  202.                 drawFrame();
  203.  
  204.             } else {
  205.  
  206.                 handler.removeCallbacks(writeTimeRunnable);
  207.  
  208.             }
  209.  
  210.         }
  211.  
  212.  
  213.  
  214.  
  215.  
  216.      public void drawFrame() {
  217.  
  218.  
  219.  
  220.     final SurfaceHolder holder = getSurfaceHolder();
  221.  
  222.  
  223.  
  224.     Canvas c = null;
  225.  
  226.    
  227.  
  228.     try {
  229.  
  230.         c = holder.lockCanvas();
  231.  
  232.         if (c != null) {
  233.  
  234.             // draw something
  235.  
  236.             writeTime(c);
  237.  
  238.         }
  239.  
  240.     } finally {
  241.  
  242.         if (c != null) holder.unlockCanvasAndPost(c);
  243.  
  244.     }
  245.  
  246.  
  247.  
  248.     // Reschedule the next redraw
  249.  
  250.     handler.removeCallbacks(writeTimeRunnable);
  251.  
  252.     if (mVisible) {
  253.  
  254.         handler.postDelayed(writeTimeRunnable, 1000);
  255.  
  256.     }
  257.  
  258.      
  259.  
  260.      }
  261.  
  262.      
  263.  
  264.      public void writeTime(Canvas c)
  265.  
  266.      {
  267.  
  268.          
  269.  
  270.           String hour=new SimpleDateFormat("H").format(new Date());
  271.  
  272.           String min=new SimpleDateFormat("mm").format(new Date());
  273.  
  274.           String sec=new SimpleDateFormat("ss").format(new Date());  
  275.  
  276.  
  277.  
  278.           c.drawText(hour + ":" + min + ":" + sec, 150, 150, mPaint);
  279.  
  280.          
  281.  
  282.      }
  283.  
  284.      
  285.  
  286.      }
  287.  
  288. }
Parsed in 0.048 seconds, using GeSHi 1.0.8.4
punky
Freshman
Freshman
 
Posts: 2
Joined: Thu Apr 22, 2010 5:43 pm

Top

Postby punky » Tue Apr 27, 2010 5:01 pm

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.
punky
Freshman
Freshman
 
Posts: 2
Joined: Thu Apr 22, 2010 5:43 pm

Top

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: Yahoo [Bot] and 3 guests