Android 中旋转位图的问题

发布于 2024-12-14 08:25:50 字数 1338 浏览 2 评论 0原文

我在使位图正确旋转时遇到问题。我有一个 SurfaceView,上面有多个位图。这些位图存在于数组列表中,并使用 for 循环,我在 onDraw 方法中为每个位图调用 canvas.drawBitmap。

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.BLACK);

    for (int i = 0; i < jumper.size(); i++) {
        canvas.drawBitmap(jumper.get(i).getGraphic(), jumper.get(i)
                .getCoordinates().getX(), jumper.get(i).getCoordinates()
                .getY(), null);
    }
}

我试图让用户选择一个特定的位图(众多位图之一),然后在用户在屏幕上拖动手指时旋转该位图。这是旋转代码。现在我只是使用默认的 android 图标(72x72px),存在于屏幕中心附近的随机位置。

private void rotateJumper(int direction) {
    Matrix matrix = new Matrix();
    Bitmap source = jumper.get(selectedJumperPos).getGraphic();
    matrix.postRotate(direction, source.getWidth() / 2, 
            source.getHeight() / 2);
    int x = 0;
    int y = 0;
    int width = 72;
    int height = 72;
    Bitmap tempBitmap = Bitmap.createBitmap(source, x, y, width, height,       
            matrix, true);
    jumper.get(selectedJumperPos).setGraphic(tempBitmap);
}

整数方向为 +1 或 -1,具体取决于手指拖动的方向。因此,对于每个 MotionEvent.ACTION_MOVE 事件,图像应旋转 1 度。

问题如下:

  1. 图像不围绕图像中心旋转。 CW 旋转以左下角为中心。 CCW 旋转以右上角为中心。
  2. 由于它不绕中心旋转,因此图像会旋转到其初始边界之外并最终消失。
  3. 图像旋转时会变得模糊。

您能给我的任何帮助将不胜感激。

谢谢!

I'm having a problem getting a bitmap to rotate properly. I have a SurfaceView with multiple bitmaps on it. These bitmaps exist within an arraylist and using a for loop I call canvas.drawBitmap for each one in the onDraw method.

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.BLACK);

    for (int i = 0; i < jumper.size(); i++) {
        canvas.drawBitmap(jumper.get(i).getGraphic(), jumper.get(i)
                .getCoordinates().getX(), jumper.get(i).getCoordinates()
                .getY(), null);
    }
}

I am attempting to have the user select a specific bitmap (one of many) and then have that bitmap rotate while the user drags his finger across the screen. So here is the rotation code. For now I'm just using the default android icon (72x72px) existing at a random position somewhere near the center of the screen.

private void rotateJumper(int direction) {
    Matrix matrix = new Matrix();
    Bitmap source = jumper.get(selectedJumperPos).getGraphic();
    matrix.postRotate(direction, source.getWidth() / 2, 
            source.getHeight() / 2);
    int x = 0;
    int y = 0;
    int width = 72;
    int height = 72;
    Bitmap tempBitmap = Bitmap.createBitmap(source, x, y, width, height,       
            matrix, true);
    jumper.get(selectedJumperPos).setGraphic(tempBitmap);
}

The integer direction is either +1 or -1 depending on the direction of the finger drag. So the image should rotate 1 degree for every MotionEvent.ACTION_MOVE event.

Here are the problems:

  1. The image doesn't rotate about the image center. CW rotations center about the bottom left corner. CCW rotations center about the top right corner.
  2. Since it isn't rotating about the center, the image rotates outside its initial bounds and eventually disappears.
  3. The image gets blurry as it rotates.

Any help you could give me would be greatly appreciated.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说不完的你爱 2024-12-21 08:25:50

使用矩阵将现有位图绘制到画布上,而不是创建新位图:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.BLACK);

    for (int i = 0; i < jumper.size(); i++) {
        canvas.drawBitmap(jumper.get(i).getGraphic(), jumper.get(i).getMatrix(), null);
    }
}

private void rotateJumper(int direction) {
    Matrix matrix = jumper.get(selectedJumperPos).getMatrix();
    if(matrix == null) {
        matrix = new Matrix();
        matrix.setTranslate(jumper.get(...).getCoord...().getX(), jumper.get(..).getCoord...().getY());
        jumper.get(selectedJumperPos).setMatrix(matrix);
    }
    Bitmap source = jumper.get(selectedJumperPos).getGraphic();
    matrix.postRotate(direction, source.getWidth() / 2, 
            source.getHeight() / 2);

}

Use the matrix to draw the existing bitmap to the canvas rather than creating a new bitmap:

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(Color.BLACK);

    for (int i = 0; i < jumper.size(); i++) {
        canvas.drawBitmap(jumper.get(i).getGraphic(), jumper.get(i).getMatrix(), null);
    }
}

private void rotateJumper(int direction) {
    Matrix matrix = jumper.get(selectedJumperPos).getMatrix();
    if(matrix == null) {
        matrix = new Matrix();
        matrix.setTranslate(jumper.get(...).getCoord...().getX(), jumper.get(..).getCoord...().getY());
        jumper.get(selectedJumperPos).setMatrix(matrix);
    }
    Bitmap source = jumper.get(selectedJumperPos).getGraphic();
    matrix.postRotate(direction, source.getWidth() / 2, 
            source.getHeight() / 2);

}
旧故 2024-12-21 08:25:50

请原谅我的回答相当偏离主题,但你的 for 循环引起了我的注意。也许可以将其写成“更易读”的格式;

for (YourJumperItem item : jumper) {
    canvas.drawBitmap(
        item.getGraphic(), item.getCoordinates().getX(),
        item.getCoordinates().getY(), null );
}

其中 YourJumperItem 是您的 Jumper 数组包含的类类型。不幸的是,关于旋转位图不能说太多,我只是推广这种方便的 for 循环编写方式。

Pardon me for rather offtopic answer, but your for -loop caught my attention. It might be possible to write it in 'more readable' format;

for (YourJumperItem item : jumper) {
    canvas.drawBitmap(
        item.getGraphic(), item.getCoordinates().getX(),
        item.getCoordinates().getY(), null );
}

Where YourJumperItem is the class type your jumper -array contains. Unfortunately can't say much about rotating Bitmaps, am just promoting this handy way of writing for -loops.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文