2009-11-13 105 views
0

爲了繪製風景,背景和圖案等,我們在J2ME中使用了TiledLayer。有沒有一個Android的對應。 android是否提供了在佈局XML中設置這種平鋪模式的選項?TiledLayer等效於Android

回答

2

你可以使用這個類作爲等價物 「的TiledLayer類JME的」:

package net.obviam.walking; 

import java.util.ArrayList; 

import com.kilobolt.framework.Graphics; 
import com.kilobolt.framework.Image; 
import com.kilobolt.framework.implementation.AndroidImage; 

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

public class TiledLayer { 

    public static final int DIR_LEFT = 0; 
    public static final int DIR_RIGHT = 1; 
    public static final int DIR_UP = 2; 
    public static final int DIR_DOWN = 3; 

    private int cellWidth; 
    private int cellHeight; 
    private int yPosition = 0, xPosition = 0; 
    private int[][] grid; 
    private Image image; 
    private Bitmap tileArray[]; 
    private int[] tileXPositions; 
    private int[] tileYPositions; 
    private ArrayList animatedTiles; 
    private int numberOfTiles; 
    private int numberOfColumns; 
    private int numberOfRows; 
    private int gridColumns; 
    private int gridRows, width, height; 
    int tileCounter; 

    public TiledLayer(Image image, int columns, int rows, int tileWidth, 
      int tileHeight, int width, int height) { 
     this.grid = new int[columns][rows]; 
     this.gridColumns = columns; 
     this.gridRows = rows; 
     this.width = columns * tileWidth; 
     this.height = rows * tileHeight; 
     this.animatedTiles = new ArrayList(); 
     this.cellWidth = tileWidth; 
     this.cellHeight = tileHeight; 
     int r = image.getHeight()/tileHeight; 
     int c = image.getWidth()/tileWidth; 
     tileArray = new Bitmap[(r * c) + 1]; 
     setStaticTileSet(image, tileWidth, tileHeight); 
    } 

    public TiledLayer(Bitmap image, int columns, int rows, int tileWidth, 
      int tileHeight, int width, int height) { 
     this.grid = new int[columns][rows]; 
     this.gridColumns = columns; 
     this.gridRows = rows; 
     this.width = columns * tileWidth; 
     this.height = rows * tileHeight; 
     this.animatedTiles = new ArrayList(); 
     this.cellWidth = tileWidth; 
     this.cellHeight = tileHeight; 
     int r = image.getHeight()/tileHeight; 
     int c = image.getWidth()/tileWidth; 
     tileArray = new Bitmap[(r * c) + 1]; 
     setStaticTileSet(image, tileWidth, tileHeight); 
    } 

    public void setStaticTileSet(Image image, int tileWidth, int tileHeight) { 
     tileArray[0] = Bitmap.createBitmap(tileWidth, tileHeight, 
       Config.ARGB_4444); 
     int rows = image.getHeight()/tileHeight; 
     int columns = image.getWidth()/tileWidth; 
     numberOfTiles = rows * columns; 
     for (int i = 0; i < rows; i++) { 
      yPosition = i * tileHeight; 
      for (int j = 0; j < columns; j++) { 
       xPosition = j * tileWidth; 
       tileCounter++; 
       tileArray[tileCounter] = Bitmap.createBitmap(
         ((AndroidImage) image).bitmap, xPosition, yPosition, 
         tileWidth, tileHeight); 
      } 
     } 
     if (gridColumns * gridRows < this.numberOfTiles) { 
      // clear the grid, when there are not as many tiles as in the 
      // previous set: 
      for (int i = 0; i < this.grid.length; i++) { 
       for (int j = 0; j < this.grid[i].length; j++) { 
        this.grid[i][j] = 0; 
       } 
      } 
     } 
    } 

    public void setStaticTileSet(Bitmap image, int tileWidth, int tileHeight) { 
     tileArray[0] = Bitmap.createBitmap(tileWidth, tileHeight, 
       Config.ARGB_4444); 
     int rows = image.getHeight()/tileHeight; 
     int columns = image.getWidth()/tileWidth; 
     numberOfTiles = rows * columns; 
     for (int i = 0; i < rows; i++) { 
      yPosition = i * tileHeight; 
      for (int j = 0; j < columns; j++) { 
       xPosition = j * tileWidth; 
       tileCounter++; 
       tileArray[tileCounter] = Bitmap.createBitmap(image, xPosition, 
         yPosition, tileWidth, tileHeight); 
      } 
     } 

     if (gridColumns * gridRows < this.numberOfTiles) { 
      // clear the grid, when there are not as many tiles as in the 
      // previous set: 
      for (int i = 0; i < this.grid.length; i++) { 
       for (int j = 0; j < this.grid[i].length; j++) { 
        this.grid[i][j] = 0; 
       } 
      } 
     } 
    } 

    public int createAnimatedTile(int staticTileIndex) { 
     if (staticTileIndex >= this.numberOfTiles) { 

      throw new IllegalArgumentException("invalid static tile index: " 
        + staticTileIndex + " (there are only [" 
        + this.numberOfTiles + "] tiles available."); 

     } 
     this.animatedTiles.add(new Integer(staticTileIndex)); 
     return -1 * (this.animatedTiles.size() - 1); 
    } 

    public void setAnimatedTile(int animatedTileIndex, int staticTileIndex) { 
     if (staticTileIndex >= this.numberOfTiles) { 

     } 
     int animatedIndex = (-1 * animatedTileIndex) - 1; 
     this.animatedTiles.set(animatedIndex, new Integer(staticTileIndex)); 
    } 

    public int getAnimatedTile(int animatedTileIndex) { 
     int animatedIndex = (-1 * animatedTileIndex) - 1; 
     Integer animatedTile = (Integer) this.animatedTiles.get(animatedIndex); 
     return animatedTile.intValue(); 
    } 

    public void setCell(int col, int row, int tileIndex) { 
     if (tileIndex >= this.numberOfTiles) { 

      throw new IllegalArgumentException("invalid static tile index: " 
        + tileIndex + " (there are only [" + this.numberOfTiles 
        + "] tiles available."); 

     } 
     this.grid[col][row] = tileIndex; 
    } 

    public int getCell(int col, int row) { 
     return this.grid[col][row]; 
    } 

    public boolean checkTileCollision(Sprite sp, int dir, int speed) { 
     int curRow, curCollumn; 

     int leftTile = 0, rightTile = 0, upTile = 0, downTile = 0; 

     curRow = sp.getY()/cellHeight; 
     curCollumn = sp.getX()/cellWidth; 

     leftTile = grid[curCollumn - 1][curRow]; 
     rightTile = grid[curCollumn + 1][curRow]; 
     upTile = grid[curCollumn][curRow - 1]; 
     downTile = grid[curCollumn][curRow + 1]; 

     if (dir == DIR_RIGHT 
       && (sp.getX() + sp.getWidth() + speed) <= (curCollumn + 1) 
         * cellWidth) 
      return false; 
     else if (dir == DIR_RIGHT 
       && ((sp.getX() + sp.getWidth() + speed)) > (curCollumn + 1) 
         * cellWidth && rightTile != 1) 
      return true; 
     else if (dir == DIR_LEFT 
       && (sp.getX() - speed) >= (curCollumn) * cellWidth) 
      return false; 
     else if (dir == DIR_LEFT 
       && (sp.getX() - speed) < (curCollumn) * cellWidth 
       && leftTile != 1) 
      return true; 
     else if (dir == DIR_UP && (sp.getY() - speed) >= (curRow) * cellHeight) 
      return false; 
     else if (dir == DIR_UP && (sp.getY() - speed) < (curRow) * cellHeight 
       && upTile != 1) 
      return true; 
     else if (dir == DIR_DOWN 
       && (sp.getY() + sp.getHeight() + speed) <= (curRow + 1) 
         * cellHeight) 
      return false; 
     else if (dir == DIR_DOWN 
       && (sp.getY() + sp.getHeight() + speed) > (curRow + 1) 
         * cellHeight && downTile != 1) 
      return true; 
     else 
      return false; 
    } 

    public void fillCells(int col, int row, int numCols, int numRows, 
      int tileIndex) { 
     if (tileIndex >= this.numberOfTiles) { 

      throw new IllegalArgumentException("invalid static tile index: " 
        + tileIndex + " (there are only [" + this.numberOfTiles 
        + "] tiles available."); 

     } 
     int endCols = col + numCols; 
     int endRows = row + numRows; 
     for (int i = col; i < endCols; i++) { 
      for (int j = row; j < endRows; j++) { 
       this.grid[i][j] = tileIndex; 
      } 
     } 
    } 

    public final int getCellWidth() { 
     return this.cellWidth; 
    } 

    public final int getCellHeight() { 
     return this.cellHeight; 
    } 

    public final int getColumns() { 
     return this.gridColumns; 
    } 

    public final int getRows() { 
     return this.gridRows; 
    } 

    public final void paint(Graphics g) { 

     for (int i = 0; i < this.gridRows; i++) { 
      for (int j = 0; j < this.gridColumns; j++) { 
       Bitmap bmp = tileArray[grid[j][i]]; 
       g.drawImage(bmp, j * cellWidth, i * cellHeight); 
      } 
     } 
    } 

    public final void paint(Canvas c) { 

     for (int i = 0; i < this.gridRows; i++) { 
      for (int j = 0; j < this.gridColumns; j++) { 
       Bitmap bmp = tileArray[grid[j][i]]; 
       c.drawBitmap(bmp, j * cellWidth, i * cellHeight, null); 
       // g.drawImage(bmp, j*cellWidth,i*cellHeight); 
      } 
     } 

    } 
}