代码之家  ›  专栏  ›  技术社区  ›  Codejoy

Android中的Sprite旋转画布.DrawBitmap. 我很接近,我做错什么了?

  •  7
  • Codejoy  · 技术社区  · 14 年前

        /*************************************************
     * 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;
    }
    

    现在是用法:

    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将关闭。。。。关于如何解决这个问题的建议?

    2 回复  |  直到 14 年前
        1
  •  8
  •   methodin    13 年前

    尝试使用画布.保存()旋转前和画布.还原()操作完成后。

        2
  •  15
  •   JP_    13 年前

    只是为了下一个可能读到这篇文章的人。只需几行代码即可完成此操作:

    canvas.save();
    canvas.rotate(rotation_angle, x + (widthofimage / 2), y + (heightofimage / 2));
    canvas.drawBitmap(bitmap, x, y, null);
    canvas.restore();