【问题标题】:TiledLayer equivalent in Android [duplicate]Android中的TiledLayer等价物[重复]
【发布时间】:2009-11-13 11:29:57
【问题描述】:

为了绘制风景、带图案的背景等,我们在 J2ME 中使用了 TiledLayer。有没有对应的android。 android 是否提供了在布局 XML 中设置此类平铺模式的选项?

【问题讨论】:

    标签: android tiles


    【解决方案1】:

    您可以将此类用作等效的“jme 的 TiledLayer 类”:

    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);
                }
            }
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-03-09
      • 2011-09-12
      • 2014-02-15
      • 2012-05-19
      • 2020-02-14
      • 2011-11-20
      • 2016-01-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多