【问题标题】:android positioning a drawBitmap that uses rectandroid定位一个使用rect的drawBitmap
【发布时间】:2012-03-13 07:26:06
【问题描述】:

我在这个网站上搜索了如何来回旋转图像,并在这个网站上的其他帖子的帮助下想出了我自己的,它工作得很好,唯一的问题是现在我让它工作了所以它以我喜欢的方式来回旋转,我现在如何将它定位在屏幕上? 到目前为止的代码如下:

 private Bitmap ray;
    private  int mRot = -33;
    private boolean goRight = true;

    void onRotDraw(Canvas canvas)
    {
        int width = ray.getWidth();
        int height = ray.getHeight();

        if (mRot > 30){
            goRight = false;
        }
        if (mRot < -30){
            goRight = true;
        }
        if (goRight){
            mRot += 1;
        }else
        {
            mRot -= 1;
        }
               canvas.rotate(mRot,width / 2,height / 2);
               this.Draw_Sprite(canvas, 0, mRot ); 

    }

    public void Draw_Sprite(Canvas c, int whatever, int rot) {  
        //rotating image back and forth
        int width = ray.getWidth();
        int height = ray.getHeight();
         Rect src = new Rect(width - width, height - height, width, height);
         Rect dst = new Rect(width - width, height - height,   width,  height); 
         c.drawBitmap(this.ray, src, dst, null);
    }

通常使用 drawBitmap 我用它来定位我的图像,但是这个使用 src 和 dst 而不是矩形的大小,所以现在它工作得很好但是我如何将屏幕上的位置从 0,0 更改为 where我想要它去吗?

这部分或大部分取自这篇文章: Sprite Rotation in Android using Canvas.DrawBitmap. I am close, what am I doing wrong?

除了如何在屏幕上设置 x 和 y 位置之外,这给了我一切所需的一切,对此的任何帮助将不胜感激,我一直在绞尽脑汁试图找到一种方法这设置了 x 和 y 位置没有运气,它可能很简单。 提前致谢。

【问题讨论】:

    标签: android rotation drawbitmap


    【解决方案1】:

    这是您引用的其他帖子中的一段代码:

    Rect src = new Rect(0, 0, width, height);
    Rect dst = new Rect(x, y, x + width, y + height);
    

    dst Rect 中的 x 和 y 决定了位图的位置。

    您的代码:

    Rect src = new Rect(width - width, height - height, width, height);
    Rect dst = new Rect(width - width, height - height,   width,  height);
    

    将始终在 0、0 处绘制它,因为宽度 - 宽度将为 0,高度 - 高度将为 0。

    【讨论】:

    • 这对我来说太愚蠢了,好的,我得到了那个部分,谢谢你,但现在我遇到了另一个问题,当我设置 dst 并将 50 添加到它现在不能正确旋转的所有内容时,即使我在 src 和 dst 中添加相同的数字,它也会旋转出来(如果那是一个词),但如果我拥有它们,就像我拥有它一样,它旋转得很漂亮,这是一个缩放问题吗?我试过 dst = newRect(50,50,50+width,50+height)
    • 没关系,我是个白痴,哈哈,我忘了将 50 也添加到其他部分 canvas.rotate(mRot,50 + (width / 2),50 + (height / 2));现在一切正常,感谢您为我回答这个简单的问题,感谢您为此花费的时间。
    猜你喜欢
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-25
    • 2023-04-02
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    相关资源
    最近更新 更多