Android 贪吃蛇源码分析(2)

/** x轴方向上格子的个数 */
    protected static int mXTileCount;
    /** y轴方向上格子的个数  */
    protected static int mYTileCount;

private static int mXOffset;
    private static int mYOffset;


    /**
     *
     * A hash that maps integer handles specified by the subclasser to the
     * drawable that will be used for that reference
     */
    private Bitmap[] mTileArray;

/**
     * 声明用来存放绘画图像的x,y轴的位置的数组
     * A two-dimensional array of integers in which the number represents the
     * index of the tile that should be drawn at that locations
     */
    private int[][] mTileGrid;

private final Paint mPaint = new Paint();

public TileView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TileView);

mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);
       
        a.recycle();
    }

public TileView(Context context, AttributeSet attrs) {
        super(context, attrs);

TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TileView);

mTileSize = a.getInt(R.styleable.TileView_tileSize, 12);
       
        a.recycle();
    }


   
    /**
     * Rests the internal array of Bitmaps used for drawing tiles, and
     * sets the maximum index of tiles to be inserted
     *
     * @param tilecount
     */
   
    public void resetTiles(int tilecount) {
        mTileArray = new Bitmap[tilecount];
    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        mXTileCount = (int) Math.floor(w / mTileSize);
        mYTileCount = (int) Math.floor(h / mTileSize);

mXOffset = ((w - (mTileSize * mXTileCount)) / 2);
        mYOffset = ((h - (mTileSize * mYTileCount)) / 2);

mTileGrid = new int[mXTileCount][mYTileCount];
        clearTiles();
    }

/**
     * Function to set the specified Drawable as the tile for a particular
     * integer key.
     *
     * @param key
     * @param tile
     */
    public void loadTile(int key, Drawable tile) {
        Bitmap bitmap = Bitmap.createBitmap(mTileSize, mTileSize, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        //指定矩形的大小
        tile.setBounds(0, 0, mTileSize, mTileSize);
        tile.draw(canvas);
       
        mTileArray[key] = bitmap;
    }

/**
     * Resets all tiles to 0 (empty)
     * 清空
     */
    public void clearTiles() {
        for (int x = 0; x < mXTileCount; x++) {
            for (int y = 0; y < mYTileCount; y++) {
                setTile(0, x, y);
            }
        }
    }

/**
     * Used to indicate that a particular tile (set with loadTile and referenced
     * by an integer) should be drawn at the given x/y coordinates during the
     * next invalidate/draw cycle.
     *
     * @param tileindex 图片的索引
     * @param x
     * @param y
     */
    public void setTile(int tileindex, int x, int y) {
        mTileGrid[x][y] = tileindex;
    }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwspjs.html