English 中文(简体)
TiledLayer equivalent in Android [duplicate]
原标题:

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

问题回答

You can use this class as a equivalent "TiledLayer class of 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);
            }
        }

    }
}




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签