无法使用 invalidate() 重绘我的画布位图;

Posted

技术标签:

【中文标题】无法使用 invalidate() 重绘我的画布位图;【英文标题】:Can't redraw my canvas bitmap with invalidate(); 【发布时间】:2016-10-17 07:05:53 【问题描述】:

我的游戏需要帮助! 我正在用位图绘制我的 playermodel,我想用 ontouch 事件更改模型,但它不起作用,我不知道,我做错了什么:( 首先是我的玩家类,我正在创建位图:

public class Player extends GameObject
private Bitmap spritesheet;
private String scoreText;
public int score;
private double dya;
private boolean up;
private boolean playing;
private Animation animation = new Animation();
private long startTime;
public boolean jumpable = false;
public boolean jumphigh;
public boolean falldown;
private int boden;
private boolean sliden;

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

    x = 100;
    y = GamePanel.HEIGHT - GamePanel.HEIGHT / 4 +26 ;
    //y = GamePanel.HEIGHT - GamePanel.HEIGHT / 4 - 100;
    dy = 0;
    score = 0;
    height = h;
    width = w;
    boden = GamePanel.HEIGHT - GamePanel.HEIGHT / 4 -100;

    Bitmap[] image = new Bitmap[numFrames];
    spritesheet = res;

    for (int i = 0; i < image.length; i++)
    
        image[i] = Bitmap.createBitmap(spritesheet, i*width, 0, width, height);
    
    animation.setFrames(image);
    animation.setDelay(40);
    startTime = System.nanoTime();


public void setUp(boolean b)up = b;

public void update()

    //score
    long elapsed = (System.nanoTime()-startTime)/1000000;
    if(elapsed>100)
    
        score = score+7;
        startTime = System.nanoTime();
        scoreText = ""+score ;
    
    animation.update();
    if(up == true && jumpable == true)
        jumphigh = true;
      //  dy = -5;
       // System.out.println(up);
    else
        jumphigh = false;

    
    if (jumphigh == true)
        dy = -5;
    
    if (falldown == true)
        dy = 5;
    

    if(dy>14)dy = 14;
    if(dy<-14)dy = -14;
    y += dy*2;

    if (y >= boden)
        y = boden;
        jumpable = true;
        falldown = false;
        dy = 0;
    else if (y <= 60)
        y= 60;
        up = false ;
        jumphigh = false;
        falldown = true;
        dy = 0;

    
    if (y < GamePanel.HEIGHT - GamePanel.HEIGHT / 4 -100)
        jumpable = false;

    

public  void restart()
    x = 100;
    y = GamePanel.HEIGHT - GamePanel.HEIGHT / 4 - 100;
    dy = 0;
    score = 0;


public void draw(Canvas canvas)

    canvas.drawBitmap(animation.getImage(),x,y,null);

    Paint paint = new Paint();
    paint.setTypeface(Typeface.create(Typeface.SANS_SERIF,Typeface.NORMAL));
    paint.setColor(Color.BLACK);
    paint.setTextSize(50);
    canvas.drawText(scoreText, 50 , 50 , paint);



public void setY(Canvas canvas)
    y = GamePanel.HEIGHT - GamePanel.HEIGHT / 4 -20 ;
    boden = GamePanel.HEIGHT - GamePanel.HEIGHT / 4 -20 ;
    draw(canvas);

public int getScore()return score;
public void setSliden() sliden = true; 
public boolean getPlaying()return playing;
public void setPlaying(boolean b)playing = b;
public void resetP() x = 100;
    y = GamePanel.HEIGHT - GamePanel.HEIGHT / 4 - 100;
    dy = 0;
public void resetDYA()dya = 0;
public void resetScore()score = 0;

这是我调用位图的游戏面板代码(我只给你这个问题所需的部分):

private Player player;

private int PlayerSprite = R.drawable.test;;
private int Playerhight = 120;
private int Playerwight = 115;
private int PlayerFrames = 8;

@Override
public void surfaceCreated(SurfaceHolder holder) 
    player = new Player(BitmapFactory.decodeResource(getResources(), PlayerSprite), Playerwight, Playerhight, PlayerFrames);

    invalidate();
    thread.setRunning(true);
    thread.start();


@Override
public boolean onTouchEvent(MotionEvent event) 
    if(event.getAction()==MotionEvent.ACTION_DOWN)

            PlayerSprite = R.drawable.sliden;
            Playerhight = 52;
            Playerwight = 130;
            PlayerFrames  = 1;

        return true;
    
    if(event.getAction()==MotionEvent.ACTION_UP)
    
        player.setUp(false);
        PlayerSprite = R.drawable.test;
        Playerhight = 120;
        Playerwight = 115;
        PlayerFrames  = 8;
        return true;
    

    return super.onTouchEvent(event);


public void update() 
    if (player.getPlaying() == true && State == STATE.INGAME) 
        player.update();

@Override
public void draw(Canvas canvas) 
    super.draw(canvas);
    final float scaleFactorX = getWidth() / (WIDTH*1.f);
    final float scaleFactorY = getHeight() / (HEIGHT*1.f);
    if (canvas != null && State == STATE.INGAME) 
        final int savedState = canvas.save();
        canvas.scale(scaleFactorX, scaleFactorY);
        player.draw(canvas);
        canvas.restoreToCount(savedState);
        invalidate();
    

我只想通过 onTouch 事件更改播放器的图像,但它不起作用。

【问题讨论】:

【参考方案1】:

只需在onTouchEvent(MotionEvent event)() 中拨打invalidate() 即可查看更改。

不要在 onDraw() 中调用 invalidate,这会导致永久循环并降低应用的性能。

【讨论】:

以上是关于无法使用 invalidate() 重绘我的画布位图;的主要内容,如果未能解决你的问题,请参考以下文章

如何在 SWIFT 中重绘我的视图?

Android 4+ html5画布不重绘

调用ondraw后重置画布,然后再在android中调用它

Invalidate()函数

仅在数据更改时刷新 DataTable

Winforms:如何加速 Invalidate()?