简体   繁体   中英

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. 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.

By default, setRotation(float radians) will rotate the image in radians around the center point of the view you are rotating. So if you call group.get(i).setRotation(Math.PI/2) , your bitmap will rotate 90 degrees from the point it is at.

You can adjust the point at which it rotates by calling the setPivot() methods.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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