简体   繁体   English

Android围绕一个点旋转多个位图

[英]Android rotating multiple bitmaps around a point

I am rotating a set of bitmaps on canvas around a certain point. 我正在画布上围绕某个点旋转一组位图。 I am using following function to rotate: 我正在使用以下功能进行旋转:

private void rotateGroupdAtPoint(ArrayList<MyBitmap> group, int degrees) {
        // TODO Auto-generated method stub

        float minx=100000,miny=100000,maxx=-100000,maxy=-100000;
        for(int i = 0; i < group.size(); i++) {
            MyBitmap m = group.get(i);
            if(minx > m.getX()) {
                minx = m.getX();
            }
            if(miny > m.getY()) {
                miny = m.getY();
            }
            if(maxx < m.getX() + m.getBmp().getWidth()) {
                maxx = m.getX() + m.getBmp().getWidth();
            }
            if(maxy < m.getY() + m.getBmp().getHeight()) {
                maxy = m.getY() + m.getBmp().getHeight();
            }
        }

        float x = (minx+maxx)/2;
        float y = (miny+maxy)/2;
        Log.d("minmax","min:"+minx+","+maxx+"    max:"+miny+","+maxy);


        for(int i = 0; i < group.size(); i++) {
            Log.d("position before","x:" + group.get(i).getX() + ", y:" + group.get(i).getY());
            float newx = (float)(((group.get(i).getX() - x) * Math.cos(Math.toRadians(degrees))) + ((group.get(i).getY() - y) * Math.sin(Math.toRadians(degrees)))) + x;
            float newy = (float)(((group.get(i).getX() - x) * Math.sin(Math.toRadians(degrees))) - ((group.get(i).getY() - y) * Math.cos(Math.toRadians(degrees)))) + y;
            group.get(i).setX(newx);
            group.get(i).setY(newy);
            group.get(i).setBmp(rotateBitmap(group.get(i).getBmp(), -90));
            Log.d("position","x:" + group.get(i).getX() + ", y:" + group.get(i).getY());

        }
    }


private Bitmap rotateBitmap(Bitmap bmp, int degrees) {
        if (degrees != 0 && bmp != null) {
            Matrix matrix = new Matrix();

            matrix.setRotate(degrees, (float) bmp.getWidth() / 2, (float) bmp.getHeight() / 2);
            try {
                Bitmap rotatedBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
                if (bmp != rotatedBmp) {
                    bmp.recycle();
                    bmp = rotatedBmp;
                }
            } catch (OutOfMemoryError ex) {
                return null;
            }
        }
        return bmp;
    }

Firstly I am finding the mid point that is common to all bitmaps then rotating around that point. 首先,我找到所有位图共有的中点,然后围绕该点旋转。 Currently I have no issue with orientation, I will add code to do that as well. 目前,我对方向没有任何问题,我将添加代码来实现。 But my problem is that bitmaps are not rotating properly at the desired angle around the center point. 但是我的问题是位图无法围绕中心点以所需角度正确旋转。 Can anyone guide me where I am wrong? 谁能指导我哪里做错了?

在此处输入图片说明

You're not rotating the bitmaps at all. 您根本不会旋转位图。 You use the setX() and setY() properties to move the bitmaps to their new locations. 您可以使用setX()setY()属性将位图移动到其新位置。 On top of that, you need to decide what degree they should rotate and use the setRotation() method in conjunction with the setPivotX() and setPivotY() methods. 最重要的是,您需要确定它们应该旋转多少度,并将setRotation()方法与setPivotX()setPivotY()方法结合使用。

By default, setRotation(float radians) will rotate the image in radians around the center point of the view you are rotating. 默认情况下, setRotation(float radians)将以radians围绕旋转视图的中心点旋转图像。 So if you call group.get(i).setRotation(Math.PI/2) , your bitmap will rotate 90 degrees from the point it is at. 因此,如果调用group.get(i).setRotation(Math.PI/2) ,则位图将从其位置旋转90度。

You can adjust the point at which it rotates by calling the setPivot() methods. 您可以通过调用setPivot()方法来调整其旋转点。

View view = group.get(i);
view.setPivotX(0);
view.setPivotY(0);
view.setRotation(Math.PI/2);

This will rotate the view around the top-left corner to a perpendicular angle. 这会将view围绕左上角旋转到垂直角度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM