Android Open Source - FlappyCow Background






From Project

Back to project page FlappyCow.

License

The source code is released under:

MIT License

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

/**
 * Manages the Bitmap for the background
 * /*ww  w. j  av a 2 s .  c o  m*/
 * @author Lars Harmsen
 * Copyright (c) <2014> <Lars Harmsen - Quchen>
 */

package com.quchen.flappycow.sprites;

import com.quchen.flappycow.Game;
import com.quchen.flappycow.GameView;
import com.quchen.flappycow.R;
import com.quchen.flappycow.Util;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;

public class Background extends Sprite {

  /** Static bitmap to reduce memory usage */
  public static Bitmap globalBitmap;
  
  public Background(GameView view, Game game) {
    super(view, game);
    if(globalBitmap == null){
      globalBitmap = Util.getDownScaledBitmapAlpha8(game, R.drawable.bg);
    }
    this.bitmap = globalBitmap;
  }

  /**
   * Draws the bitmap to the Canvas.
   * The height of the bitmap will be scaled to the height of the canvas.
   * When the bitmap is scrolled to far to the left, so it won't cover the whole screen,
   * the bitmap will be drawn another time behind the first one.
   */
  @Override
  public void draw(Canvas canvas) {
    double factor = (1.0 * canvas.getHeight()) / bitmap.getHeight();
    
    if(-x > bitmap.getWidth()){
      // The first bitmap is completely out of the screen
      x += bitmap.getWidth();
    }
    
    int endBitmap = Math.min(-x + (int) (canvas.getWidth() / factor), bitmap.getWidth());
    int endCanvas = (int) ((endBitmap + x) * factor) + 1;
    src = new Rect(-x, 0, endBitmap, bitmap.getHeight());
    dst = new Rect(0, 0, endCanvas, canvas.getHeight());
    canvas.drawBitmap(this.bitmap, src, dst, null);
    
    if(endBitmap == bitmap.getWidth()){
      // draw second bitmap
      src = new Rect(0, 0, (int) (canvas.getWidth() / factor), bitmap.getHeight());
      dst = new Rect(endCanvas, 0, endCanvas + canvas.getWidth(), canvas.getHeight());
      canvas.drawBitmap(this.bitmap, src, dst, null);
    }
  }
}




Java Source Code List

com.quchen.flappycow.About.java
com.quchen.flappycow.AccomplishmentBox.java
com.quchen.flappycow.GameOverDialog.java
com.quchen.flappycow.GameView.java
com.quchen.flappycow.Game.java
com.quchen.flappycow.MainActivity.java
com.quchen.flappycow.StartscreenView.java
com.quchen.flappycow.Util.java
com.quchen.flappycow.sprites.Background.java
com.quchen.flappycow.sprites.Coin.java
com.quchen.flappycow.sprites.Cow.java
com.quchen.flappycow.sprites.Frontground.java
com.quchen.flappycow.sprites.NyanCat.java
com.quchen.flappycow.sprites.Obstacle.java
com.quchen.flappycow.sprites.PauseButton.java
com.quchen.flappycow.sprites.PlayableCharacter.java
com.quchen.flappycow.sprites.PowerUp.java
com.quchen.flappycow.sprites.Rainbow.java
com.quchen.flappycow.sprites.Spider.java
com.quchen.flappycow.sprites.Sprite.java
com.quchen.flappycow.sprites.Toast.java
com.quchen.flappycow.sprites.Tutorial.java
com.quchen.flappycow.sprites.WoodLog.java