Android Open Source - Sertimus Main Canvas






From Project

Back to project page Sertimus.

License

The source code is released under:

GNU General Public License

If you think the Android project Sertimus 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

/*  Sertimus - A live wallpaper featuring a cute Chao.
  /*from  w w w .  j a v a 2s .  co m*/
    Copyright (C) 2013  Kevin Negrin

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>. */

//  MainCanvas.java - "playing board" for moving objects

package gameaddict30.wallpaper.sertimus;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Bitmap.Config;
import android.util.Log;
import android.view.SurfaceHolder;
import gameaddict30.wallpaper.sertimus.events.LiveObjectMotionEvent;
import gameaddict30.wallpaper.sertimus.interfaces.InteractiveLiveObject;
import gameaddict30.wallpaper.sertimus.interfaces.TalkableLiveObject;
import gameaddict30.wallpaper.sertimus.objects.*;

public class MainCanvas {
  private Context appContext;
  private SurfaceHolder holder;

  private ArrayList<TalkBubble> dialogueQueue = new ArrayList<TalkBubble>();
  private ArrayList<LiveObject> foreObjects = new ArrayList<LiveObject>();
  private BackgroundTile background;
  
  private int width, height;

  private Bitmap currentSnapshot;
  private Canvas c;
  
  public MainCanvas(Context c, SurfaceHolder s, int r) {
    appContext = c;
    holder = s;
    
    width = holder.getSurfaceFrame().width();
    height = holder.getSurfaceFrame().height();
    
    background = new BackgroundTile(this, r);
    
    initSnapshot();
  }
  
  private void initSnapshot() {
    initSnapshot(width, height);
  }
  
  private synchronized void initSnapshot(int width, int height) {
    if (currentSnapshot != null) {
      c = null;
      currentSnapshot.recycle();
    }
    
    currentSnapshot = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    c = new Canvas(currentSnapshot);
    
    this.width = width;
    this.height = height;
    
    background.updateObject();
    
    Log.i("Canvas", "Created snapshot: "+String.format("%dX%d", width, height));
  }
  
  public void addForeObject(LiveObject... o) {
    for (LiveObject a : o)
      foreObjects.add(a);
    sortForeObjects();
  }
  
  public Context getAppContext() {
    return appContext;
  }
  
  private void sortForeObjects() {
    for (int x=0,y,minIndex; x < foreObjects.size(); x++) {
      for (y=x+1,minIndex=x; y < foreObjects.size(); y++)
        if (foreObjects.get(y).getY() < foreObjects.get(minIndex).getY())
          minIndex = y;
      
      if (minIndex == x)
        continue;
      
      LiveObject temp = foreObjects.get(x);
      foreObjects.set(x, foreObjects.get(minIndex));
      foreObjects.set(minIndex, temp);
    }
  }
  
  public void updateObjects() {
    /* An interesting pitfall:
     * When using a for-each loop to loop through an ArrayList of LiveObjects,
     * if your LiveObject instance in the ArrayList modifies the same or another
     * ArrayList, it will throw a ConcurrentModificationException. This usually
     * occurs when you're accessing an ArrayList and modifying it through said
     * loop. I thought the best solution in this case would be to use a for-loop
     * and access the individual elements through the ArrayList's get(Object)
     * method instead. */
  for (int x=0; x < dialogueQueue.size(); x++) {
      TalkBubble a = dialogueQueue.get(x);
      
      if (System.currentTimeMillis()-a.getStartTime() >= a.getDuration()) {
        dialogueQueue.remove(x);
        if (a.getHook() instanceof TalkableLiveObject)
          ((TalkableLiveObject)a.getHook()).onFinishTalk();
      } else
        a.updateObject();
    }
    
    for (int x=0; x < foreObjects.size(); x++)
      foreObjects.get(x).updateObject();
    sortForeObjects();
  }
  
  public synchronized Bitmap drawAndReturnSnapshot() {
    background.draw(c);
    
    for (int x=0; x < foreObjects.size(); x++)
      foreObjects.get(x).drawObject(c);
    
    for (int x=0; x < dialogueQueue.size() && dialogueQueue.size() != 0; x++)
      dialogueQueue.get(x).drawObject(c);
    
    return currentSnapshot;
  }
  
  private void resizeSnapshot(int width, int height) {
    initSnapshot(width, height);
  }
  
  public void onTouchEvent(LiveObjectMotionEvent e) {
    for (LiveObject a : foreObjects)
      if (a instanceof InteractiveLiveObject)
        ((InteractiveLiveObject)a).onTouchEvent(e);
  }
  
  public int getWidth() {
    return width;
  }

  public int getHeight() {
    return height;
  }

  public void onSurfaceChanged(int width, int height) {
    if (width != this.width || height != this.height) {
      for (LiveObject a : foreObjects)
        a.onSurfaceChanged(this.width, this.height, width, height);
      resizeSnapshot(width, height);
    }
  }
  
  public SurfaceHolder getHolder() {
    return holder;
  }

  public String getForeobjectList() {
    String listString = "";
    
    for (LiveObject a : foreObjects) {
      listString += a.toString()+"\n";
    }
    
    return listString;
  }

  public void queueTalkBubble(TalkBubble talkBubble) {
    dialogueQueue.add(talkBubble);
  }
}




Java Source Code List

gameaddict30.wallpaper.sertimus.DrawThread.java
gameaddict30.wallpaper.sertimus.LogicThread.java
gameaddict30.wallpaper.sertimus.MainCanvas.java
gameaddict30.wallpaper.sertimus.MainService.java
gameaddict30.wallpaper.sertimus.activities.MyPreferencesActivity.java
gameaddict30.wallpaper.sertimus.activities.SetWallpaperActivity.java
gameaddict30.wallpaper.sertimus.constants.CharacterAnimation.java
gameaddict30.wallpaper.sertimus.events.LiveObjectMotionEvent.java
gameaddict30.wallpaper.sertimus.interfaces.InteractiveLiveObject.java
gameaddict30.wallpaper.sertimus.interfaces.TalkableLiveObject.java
gameaddict30.wallpaper.sertimus.objects.BackgroundTile.java
gameaddict30.wallpaper.sertimus.objects.GrassStrands.java
gameaddict30.wallpaper.sertimus.objects.LiveObject.java
gameaddict30.wallpaper.sertimus.objects.Sertimus.java
gameaddict30.wallpaper.sertimus.objects.TalkBubble.java
gameaddict30.wallpaper.sertimus.utilities.Arithmetic.java
gameaddict30.wallpaper.sertimus.utilities.Strings.java