雪碧旋转在Android中使用Canvas.DrawBitmap。我靠近,我究竟做错了什么? [英] Sprite Rotation in Android using Canvas.DrawBitmap. I am close, what am I doing wrong?

查看:145
本文介绍了雪碧旋转在Android中使用Canvas.DrawBitmap。我靠近,我究竟做错了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个精灵旋转算法(它的不良命名,只是用于测试)。它是如此的接近,绘制它的精灵做旋转。 Everyframe我可以补充+5度,看看我可爱的小精灵绕。问题是,吸引到如今闪烁画布其他的东西。如果我不这样做的旋转正规精灵绘制工作的伟大。我觉得我接近,但我不知道我错过了什么作品。下面是我的两个Draw_Sprite方法,一个刚刚绘制previously资源加载位图传入的画布上。另一种,做一些轮换最好的,我知道如何旋转雪碧SO x的许多degrees..and再画吧。如果我有一个很好的游戏循环,吸引了几个对象,一类是旋转的一种。那么非旋转精灵闪烁,但旋转精灵从来不会。但如果我画的是未旋转精灵首先,一切都很好,但随后的Z排序可能会搞砸了......的方法定义(在UI元素等顶级精灵):

I have this sprite rotating algorithm (its poorly named and just used for testing). It is so close, sprites drawn with it do rotate. Everyframe I can add +5 degrees to it and see my nice little sprite rotate around. The problem is, the other stuff drawn to the canvas now flickers. If I don't do the rotation the regular drawn sprites work great. I think I am close but I just don't know what piece I am missing. Below is my two "Draw_Sprite" methods, one just draws the previously resource loaded bitmap to the canvas passed in. The other one, does some rotation the best I know how to rotate the sprite by so x many degrees..and then draw it. If I have a nice game loop that draws several objects, one type is the rotated kind. Then the non-rotated sprites flicker and yet the rotated sprite never does. Though if I draw the non-rotated sprites first, all is well, but then the Z-Ordering could be messed up (sprites on top of UI elements etc)... The method definitions:

    /*************************************************
 * rotated sprite, ignore the whatever, its for ease of use and testing to have this argument list
 * @param c canvas to draw on.
 * @param whatever ignore 
 * @param rot degrees to rotate
 * @return
 */
public int Draw_Sprite(Canvas c, int whatever, int rot) {   
    //rotating sprite
    Rect src = new Rect(0, 0, width, height);
    Rect dst = new Rect(x, y, x + width, y + height);
    Matrix orig = c.getMatrix();        
    mMatrix = orig;
    orig.setTranslate(0, 0);
    orig.postRotate(rot, x+width/2, y+height/2); 
    c.setMatrix(orig);
    c.drawBitmap(images[curr_frame], src, dst, null);
    c.setMatrix(mMatrix); //set it back so all things afterwards are displayed correctly.

    isScaled=false;
    return 1;

}

/********************************************************
 * draw a regular sprite to canvas c
 * @param c
 * @return
 */
public int Draw_Sprite(Canvas c) {  

    Rect src = new Rect(0, 0, width, height);
    Rect dst = new Rect(x, y, x + width, y + height);       
    c.drawBitmap(images[curr_frame], src, dst, null);

    isScaled=false;
    return 1;
}

而现在的用法:

And now the usage:

void onDraw(Canvas c)
{
    canvas.drawRect( bgRect, bgPaint); //draw the background

    //draw all game objects
      // draw the normal items
    for (GameEntity graphic : _graphics) { 
        graphic.toScreenCoords((int)player_x, (int)player_y);
        if(graphic.getType().equals("planet")) //draw planets
            graphic.Draw_Sprite(canvas); //before the rotation call draws fine
        else
        {
            //rotate all space ships every frame so i see them spinning        
            //test rotation
            mRot +=5;
            if(mRot>=360)
                mRot=0;
            graphic.Draw_Sprite(canvas, 0, mRot); //yes function name will be better in future.  this rotates spins draws fine


        }                           
       }

       thePlayer.Draw_Sprite(canvas); //FLICKERS
       drawUI(canvas);//all things here flickr

}  

所以它不这样做,调用一个旋转平局后,东西都正确绘制。但问题是它flickrs。现在可以说,我应该只是尽我所有的非旋转的东西,保存最后,但zordering将关闭....建议如何解决这一问题zordering或闪烁的?

So it does do it, things after a call to a rotational draw are drawn correctly. But the problem is it flickrs. Now One could say I should just do all my non rotational stuff and save that last, but the zordering would be off.... suggestions as to how to tackle this issue of zordering or the flickering?

推荐答案

尝试旋转和canvas.restore()之后的操作完成之前使用canvas.save()。

Try using canvas.save() before the rotation and canvas.restore() after manipulation is complete.

在画布上进行操作,以改变一个对象被绘制,你必须记住的操作设置画布如何处理起源等的方式......所以,如果你翻译或旋转画布,将用于设定那个帆布寿命。为了避免这一点,你首先调用保存,从而节省画布矩阵的快照,你操纵它之前,那么你运行的所有的更改,然后调用恢复将恢复画布回上次保存的点。否则,所有的修改建立起来,你会得到意想不到的结果。

When performing manipulations on the canvas in order to change the way an object is drawn you have to remember the manipulations set how the canvas handles origins etc... So if you translate or rotate the canvas, that will be set for the lifetime of that canvas. In order to avoid this you first call save, which saves a snapshot of the canvas matrix before you manipulate it, then you run all your changes, then call restore which will restore the canvas back to the last saved point. Otherwise all your changes build up and you get unintended results.

这篇关于雪碧旋转在Android中使用Canvas.DrawBitmap。我靠近,我究竟做错了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆