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

在android中围绕一个点旋转位图而不显示动画

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

    public void Rotate_Sprite(int transform, int deg)
        {
            int spriteCenterX = x+(width/2);
                    int spriteCenterY = y+(height/2);
            mMatrix.setRotate(deg, spriteCenterX, spriteCenterY);
            }
    
    public void Draw_Sprite(Canvas c) { 
    //c.drawBitmap(images[curr_frame], x, y, null); //this worked great esp in move sprite
        c.drawBitmap(images[curr_frame], mMatrix, null);
    }
    
    public void Create_Sprite(blah blah) {
    
        ...
        ...
        mMatrix = new Matrix();
        mMatrix.reset();
    }
    
    public int Move_Sprite() {
        //with the matrix stuff, I assume I need a translate.  But it does't work right 
        //for me at all.
        int lastx=this.x;
        int lasty=this.y;
        this.x+=this.vx;
        this.y+=this.vy;
        mMatrix.postTranslate(lastX-x,lastY-y); //doesn't work at all
    }
    

    我确实找到了这个 J2me like reference here. 虽然它似乎有我所有的精灵,我呼吁在轨道上围绕一个点旋转。

    3 回复  |  直到 7 年前
        1
  •  1
  •   Ben Mc    14 年前

    试试这个:

    mMatrix.setTranslate(objectX,objectY);
    mMatrix.postRotate(Degrees, objectXcenter, objectYcenter);
    

        2
  •  1
  •   Lunin    14 年前

    我没有在android上工作过,自从我上次使用矩阵已经有一段时间了,但是听起来你的旋转工作正常,你只是忘记了平移,所以旋转的点是0,0。如果这个问题是真的,你要做的是平移精灵,使它的世界位置是0,0;旋转精灵;然后把它翻译回以前的地方。这一切都应该发生在画那个框架之前,所以翻译本身永远不会被看到。

        3
  •  1
  •   Unconn    14 年前

    对于任何发现这一点并试图做同样事情的人:

    我像这样旋转图像:

    //create all your canvases and bitmaps and get sizes first
    Bitmap minBitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.minute);
    minCanvas..setBitmap(minBitmap);
    int height = min.getHeight();
    int width = min.getWidth();
    //Bitmap minBitmap = Bitmap(width, height, Bitmap.Config.ARGB_8888); //not using in example
    
    
    //The basically applies the commands to the source bitmap and creates a new bitmpa.  Check the order of width and height, mine was a square.
    minMatrix.setRotate(minDegrees, width/2, height/2);
    Bitmap newMin = Bitmap.createBitmap(minBitmap, 0, 0, (int) width, (int) height, minMatrix, true);
    
    //apply this to a canvas.  the reason for this is that rotating an image using Matrix changes the size of the image and this will trim it and center it based on new demensions.
    minCanvas2.drawBitmap(newMin, width/2 - newMin.getWidth()/2, height/2 - newMin.getHeight()/2, null);
    
    //Then you can use it the way you want, but I create a bitmap from the canvas
    minCanvas2.setBitmap(minBitmap);
    

    HTH公司