【问题标题】:Android: Error while using bitmap to display image using android studioAndroid:使用位图使用android studio显示图像时出错
【发布时间】:2015-08-31 23:26:16
【问题描述】:

我创建了一个 10x10 的网格,现在想将玩家的图像放置在其中一个网格方块内。为此,我认为我必须使用位图?我将把我所有的代码放在下面。为了尝试这样做,我创建了一个游戏类,它设置并获取玩家图像的 x 位置。然后我创建了一个播放器类,我尝试在其中添加图像,使用 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sokobanplayer1); 我在 getResources 下不断收到错误,所以我在游戏中创建了一个获取资源方法,但它没有解决问题。有人可以告诉我如何做到这一点,我的代码如下。

//DRAW CLASS
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);
    }
}

@Override
public void onDraw(Canvas canvas) {

    drawGrid(canvas);
}
}

//GAME CLASS
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 Bitmap getResources(){
    return image;
}

}


//PLAYER CLASS
public class Player extends Game {

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

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

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


//MAIN ACTIVITY
public class MainActivity extends Activity {
Draw draw;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);

    draw = new Draw(this);
    draw.setBackgroundColor(Color.BLUE);
    setContentView(draw);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}


}

【问题讨论】:

  • 传递 context.getResources()
  • 将上下文传递给 Player 类的构造函数
  • 消除了错误但图像不显示在网格上?这是为什么?

标签: java android eclipse image bitmap


【解决方案1】:

用这个替换你的 Player 类:

//PLAYER CLASS
class Player extends Game {

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

        x = 300;
        y = 300;
        height = h;
        width = w;

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-21
    • 2016-05-25
    • 1970-01-01
    • 1970-01-01
    • 2015-07-15
    • 1970-01-01
    • 2015-08-24
    相关资源
    最近更新 更多