【问题标题】:Change onDraw Bitmap when touching the display触摸显示屏时更改 onDraw Bitmap
【发布时间】:2013-02-15 14:16:07
【问题描述】:

我对我的代码有疑问。我希望每次有人触摸显示屏时我的位图都会发生变化。

protected boolean processTouch(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {

        player.setGraphic(BitmapFactory.decodeResource(Game.res, R.drawable.sprite2));

    } 
    return true;
}

在我的 player.java 文件中我放了:

public class Player extends Sprite
{
public Player(int x, int y)
{
    super(BitmapFactory.decodeResource(Game.res, R.drawable.sprite1), x-350, y, 50, 17, 5, 2);

}
public void update()
{
getGraphic();
}

public Bitmap getGraphic() 
{
    return bitmap;
}
public void setGraphic(Bitmap bitmap)
{
    this.bitmap = bitmap;
}


private Bitmap bitmap;}

我的 Sprite 类包含:

 public Sprite(Bitmap bitmap, int x, int y, int width, int height, int fps, int frameCount) 
{
    this.bitmap = bitmap;

}
public Bitmap getGraphic() 
{
    return bitmap;
}
private Bitmap bitmap;

然后我想显示它:

canvas.drawBitmap(s.getGraphic(), s.getSourceRect(), destRect, null);

但是,当我尝试启动游戏时,它崩溃了。我该如何解决这个问题?

编辑

日志:

02-15 15:29:51.174: I/ActivityManager(18563): Displayed com.example.nic_beta/.Game: +488ms
02-15 15:29:51.197: W/dalvikvm(29635): threadid=11: thread exiting with uncaught exception (group=0x419cb930)
02-15 15:29:51.197: E/AndroidRuntime(29635): FATAL EXCEPTION: Thread-23004
02-15 15:29:51.197: E/AndroidRuntime(29635): java.lang.NullPointerException
02-15 15:29:51.197: E/AndroidRuntime(29635):    at android.graphics.Canvas.throwIfRecycled(Canvas.java:1025)
02-15 15:29:51.197: E/AndroidRuntime(29635):    at android.graphics.Canvas.drawBitmap(Canvas.java:1127)
02-15 15:29:51.197: E/AndroidRuntime(29635):    at com.example.nic_beta.Panel.onDraw(Panel.java:70)
02-15 15:29:51.197: E/AndroidRuntime(29635):    at com.example.nic_beta.RunnerThread.run(Panel.java:187)
02-15 15:29:51.213: W/ActivityManager(18563):   Force finishing activity com.example.nic_beta/.Game
02-15 15:29:51.260: W/ActivityManager(18563):   Force finishing activity com.example.nic_beta/.Menu

【问题讨论】:

  • 如果发生崩溃,请发布 logcat。此外,每次触摸时解码位图会非常昂贵,最好在开始时解码一次并将其设置为触摸时预解码的位图。

标签: android bitmap touch-event ondraw


【解决方案1】:

对于 throwIfRecycled 错误,this is a possible solution

但是,正如 Gabe 所说,您应该在创建应用时解码一次位图,然后在需要时绘制它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-22
    • 2014-06-24
    • 1970-01-01
    • 2012-06-01
    • 1970-01-01
    相关资源
    最近更新 更多