Android:如何使用位图显示图像?

时间:2015-06-17 02:59:26

标签: java android bitmap ondraw

我已为游戏创建了一个10x10网格,现在我正在尝试获取图像,让玩家在其中一个网格方块中显示。我有一个Game,Player和Draw课程。在Game类中,设置了播放器的x和y位置,在Player类中,我设置x和y位置的值并创建位图。在Draw类中,我创建了网格并在onDraw方法中调用它。我想知道如何在onDraw中调用位图以便它显示?我不知道这是否有意义,但任何帮助或提示都会很好。

public class Game {

Bitmap image;
int x;
int y;
int height;
int width;


public void setX(int x){

    this.x = x;
}

public void setY(int y){

    this.y = y;
}

public int getX()
{
    return x;
}

public int getY(){
    return y;
}

public int getHeight(){
    return height;
}

public int getWidth(){
    return width;
}

}



public class Player extends Game {

public Player(Bitmap res, int w, int h, Context context){

    image = res;
    x = 300;
    y = 300;
    height = 300;
    width = 300;

    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.sokobanplayer1);
}
}



public class Draw extends View {
Paint red = new Paint();
Paint green = new Paint();

int rectSide = 1000;

public Draw(Context context) {
    super(context);
    red.setColor(Color.RED);
    green.setColor(Color.GREEN);
    red.setStrokeWidth(8);
    green.setStrokeWidth(8);
}

public void drawGrid(Canvas canvas) {

    int width = canvas.getWidth();
    int height = canvas.getHeight();


    float startX = (width / 2) - (rectSide / 2);
    float stopX = (width / 2) + (rectSide / 2);
    float startY = (height / 2) - (rectSide / 2);
    float stopY = (height / 2) + (rectSide / 2);

    for (int i = 0; i < 1100; i += 100) {
        float newY = startY + i;
        canvas.drawLine(startX, newY, stopX, newY, green);
    }

    for (int i = 0; i < 1100; i += 100) {

        float newX = startX + i;
        canvas.drawLine(newX, startY, newX, stopY, green);
    }
}

public void drawPlayer(Bitmap res, int w, int h, Context context){

    Bitmap image = res;
    int x = 300;
    int y = 300;
    int height = 300;
    int width = 300;

    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.sokobanplayer1);
}

@Override
public void onDraw(Canvas canvas) {

    drawGrid(canvas);
    //canvas.getResources(image, x, y, null);
}
}

0 个答案:

没有答案