Android Open Source - CirclesLiveWallpaper_Android Circles Service






From Project

Back to project page CirclesLiveWallpaper_Android.

License

The source code is released under:

GNU General Public License

If you think the Android project CirclesLiveWallpaper_Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.blogspot.techzealous.circleslivewallpaper;
// ww w . ja va 2 s . com
import java.util.ArrayList;
import java.util.Random;

import android.content.SharedPreferences;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.service.wallpaper.WallpaperService;
import android.view.MotionEvent;
import android.view.SurfaceHolder;

import com.blogspot.techzealous.circleslivewallpaper.utils.CirclesConstants;
import com.blogspot.techzealous.circleslivewallpaper.utils.MyCircle;

public class CirclesService extends WallpaperService {

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

  private class CirclesTestEngine extends Engine {
    
    private ArrayList<MyCircle> myCircles;
    private MyCircle myCircle;
    private int surfaceWidth;
    private int surfaceHeight;
    private boolean isCircleVisible;
    private SharedPreferences prefs;
    
    //private float startX;
    private Random circlesRandom;
    private Handler myHandler = new Handler();
    private Runnable myRunnable = new Runnable() {
      public void run() {
        makeCircles();
      }
    };
    private Canvas mainCanvas;
    private SurfaceHolder holder;
    
    /* Initialize when the wallpaper is first created */
    @Override
    public void onCreate(SurfaceHolder surfaceHolder) {
      prefs = PreferenceManager.getDefaultSharedPreferences(CirclesService.this);
      myCircles = new ArrayList<MyCircle>();
      circlesRandom = new Random();
      try {
        mainCanvas = surfaceHolder.lockCanvas();
        if(mainCanvas != null) {
          surfaceWidth = mainCanvas.getWidth();
          surfaceHeight = mainCanvas.getHeight();
          //myHandler.post(myRunnable);
        } else {
          surfaceWidth = 200;
          surfaceHeight = 300;
        }
        
      } finally {
        if(mainCanvas != null) {
          surfaceHolder.unlockCanvasAndPost(mainCanvas);
        }
      }
    }
    
    /* Start drawing on the canvas when the wallpaper is visible and stop when is not visible */
    @Override
    public void onVisibilityChanged(boolean visible) {
      isCircleVisible = visible;
      if(isCircleVisible) {
        myHandler.post(myRunnable);
//        Log.i("CirclesService", "onVisibilityChanged");
      } else {
        myHandler.removeCallbacks(myRunnable);
      }
    }
    
    @Override
    public void onSurfaceCreated(SurfaceHolder holder) {
      super.onSurfaceCreated(holder);
      //myHandler.post(myRunnable);      
    }
    
    @Override
    public void onSurfaceDestroyed(SurfaceHolder surfaceHolder) {
      super.onSurfaceDestroyed(surfaceHolder);
      isCircleVisible = false;
      myHandler.removeCallbacks(myRunnable);
    }
    
    @Override
    public void onSurfaceChanged(SurfaceHolder surfaceHolder, int format, int width, int height) {
      super.onSurfaceChanged(surfaceHolder, format, width, height);
      surfaceWidth = width;
      surfaceHeight = height;
      myHandler.post(myRunnable);
    }
    
//    @Override
//    public void onOffsetsChanged(float xOffset, float yOffset, float xOffsetStep, float yOffsetStep, int xPixelOffset, int yPixelOffset) {
//      for (MyCircle circle : myCircles) {
//        circle.setPositionX(circle.getPositionX() + (xOffset * 10));
//      }
//      Log.i("CirclesService", "xOffset=" + xOffset + "|" + "yOffset=" + yOffset + "|" + "xOffsetStep=" + xOffsetStep + "|" + "yOffsetStep=" + yOffsetStep + "|" + "xPixelOffset=" + xPixelOffset + "|" + "yPixelOffset=" + yPixelOffset); 
//    }
    
    /* Handle touch events - create a circle where the dispaly was touched */
    @Override
    public void onTouchEvent(MotionEvent event) {
      super.onTouchEvent(event);
      
      if(event.getAction() == MotionEvent.ACTION_UP) {
        SurfaceHolder holder = getSurfaceHolder();
        Canvas canvas = null;
        try {
          canvas= holder.lockCanvas();
          if(canvas != null) {
            myCircle = new MyCircle();
            myCircle.setPositionX(event.getX());
            myCircle.setPositionY(event.getY());
            myCircle.setRadius(30);
            myCircle.setColor(generateRandomColor(), generateRandomColor(), generateRandomColor(), generateRandomColor());
            myCircles.add(myCircle);
          }
        } finally {
          if(canvas != null) {
            holder.unlockCanvasAndPost(canvas);
          }
        }
      }
    }
    
    /* Make circles and draw them on screen */
    private void makeCircles() {
      holder = getSurfaceHolder();
      mainCanvas = null;
      try {
        mainCanvas= holder.lockCanvas();
        if(mainCanvas != null) {
          if(myCircles.size() == 0) {
            myCircle = new MyCircle();
            myCircle.setPositionX(generateRandomFloatX());
            myCircle.setPositionY(generateRandomFloatY());
            myCircle.setRadius(generateRandomFloatX());
            myCircle.setColor(generateRandomColor(), generateRandomColor(), generateRandomColor(), generateRandomColor());
            myCircles.add(myCircle);
          }
          
          int circleCount = prefs.getInt(CirclesConstants.PREF_CIRCLE_COUNT, 4);
          
          if(myCircles.size() > circleCount /* && myCircles.get(0).getTransparency() <= 0 */) {
//            Log.i("CirclesService", "before myCircles.remove(0)");
            myCircles.remove(0);
          } else if(myCircles.size() < circleCount){
            myCircle = new MyCircle();
            myCircle.setPositionX(generateRandomFloatX());
            myCircle.setPositionY(generateRandomFloatY());
            myCircle.setRadius(generateInitialRadius());
            myCircle.setColor(generateRandomColor(), generateRandomColor(), generateRandomColor(), generateRandomColor());
            myCircles.add(myCircle);
          }
          drawCircle(mainCanvas);
        }
      } finally {
        if(mainCanvas != null) {
          holder.unlockCanvasAndPost(mainCanvas);
        }
      }
      
      
      myHandler.removeCallbacks(myRunnable);
      if(isCircleVisible) {
        myHandler.postDelayed(myRunnable, prefs.getInt(CirclesConstants.PREF_SPEED, 100));
      }
    }
    
    private void drawCircle(Canvas canvas) {
      canvas.drawColor(Color.argb(255, prefs.getInt(CirclesConstants.PREF_CIRCLES_RED, 0), prefs.getInt(CirclesConstants.PREF_CIRCLES_GREEN, 0), prefs.getInt(CirclesConstants.PREF_CIRCLES_BLUE, 0)));
      
      for(MyCircle circle : myCircles) {
        //canvas.drawColor(Color.argb(getRandomColor(), getRandomColor(), getRandomColor(), getRandomColor()));
        canvas.drawCircle(circle.getPositionX(), circle.getPositionY(), circle.getRadius(), circle.getColor());
        
        circle.setRadius(circle.getRadius() + 3);
        if(circle.getTransparency() < 255 && circle.getVisibilityGrow()) {
          circle.setTransparency(circle.getTransparency() + 20);
        } else {
          circle.setVisibilityGrow(false);
          circle.setTransparency(circle.getTransparency() - 20);
        }
        
        /* when the circle is not visible anymore change its position, color and radius.
         * I.e recycle the circle, no need to remove it from the list and make new one 
         */
        if(circle.getTransparency() <= 0) {
          circle.setPositionX(generateRandomFloatX());
          circle.setPositionY(generateRandomFloatY());
          circle.setRadius(generateInitialRadius());
          circle.setColor(generateRandomColor(), generateRandomColor(), generateRandomColor(), generateRandomColor());
        }
      }
      
    }
    
    /* Generate the starting length of the radius of the circle */
    private int generateInitialRadius() {
      return circlesRandom.nextInt(30);
    }
    
    private int generateRandomColor() {
      return circlesRandom.nextInt(256);
    }
    
    private float generateRandomFloatX() {
      float random = (float) (surfaceWidth / (Math.random()));
      while(random >= surfaceWidth) {
        random = Math.abs(random - surfaceWidth);
      }
      return random;
    }
    
    private float generateRandomFloatY() {
      float random = (float) (surfaceHeight / (Math.random()));
      while(random >= surfaceHeight) {
        random = Math.abs(random - surfaceHeight);
      }
      return random;
    }
  }
}




Java Source Code List

com.blogspot.techzealous.circleslivewallpaper.CirclesBackColorActivity.java
com.blogspot.techzealous.circleslivewallpaper.CirclesCountActivity.java
com.blogspot.techzealous.circleslivewallpaper.CirclesEula.java
com.blogspot.techzealous.circleslivewallpaper.CirclesPreferenceActivity.java
com.blogspot.techzealous.circleslivewallpaper.CirclesService.java
com.blogspot.techzealous.circleslivewallpaper.CirclesSpeedActivity.java
com.blogspot.techzealous.circleslivewallpaper.utils.CirclesConstants.java
com.blogspot.techzealous.circleslivewallpaper.utils.MyCircle.java