简体   繁体   English

围绕指定点旋转图像不起作用! (机器人)

[英]Rotating an image around a specified point doesn't work! (Android)

I'm rotating an ImageView with postRotate(float degrees, float px, float py), setting px and py to a few differnt values including (0,0) and (imgView.getHeight(),imgView.getWidth()) but it refuses to rotate around any other point than the center. 我正在使用postRotate(浮点数,浮点数px,浮点py)旋转ImageView,将px和py设置为几个不同的值,包括(0,0)和(imgView.getHeight(),imgView.getWidth())但它拒绝围绕中心以外的任何其他点旋转。 I'm wondering whether it's got anything to do with the fact that my gravity is center in the LinearLayout? 我想知道它是否与我的引力在LinearLayout中心的事实有关?

My layout: 我的布局:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center">

    <ImageView
        android:id="@+id/imageTraj"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:scaleType="matrix"
        android:src="@drawable/impactangle" />

How I'm rotating the image: 我是如何旋转图像的:

matrix.postRotate(degrees,imageView.getHeight(),imageView.getWidth());
imageView.setImageBitmap(Bitmap.createBitmap(imageScaled, 0, 0,
                imageScaled.getWidth(), imageScaled.getHeight(), matrix, true));

PS I've noticed there are a few similar questions, but none of them have suitable answers. PS我注意到有一些类似的问题,但没有一个有合适的答案。

I am using a custom ImageView where I set the angle rotation. 我正在使用自定义ImageView设置角度旋转。

public class CompassImage extends ImageView {
    private float angleRotation;

    public CompassImage(Context context) {
        super(context);
    }

    public CompassImage(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CompassImage(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setAngleRotation(float angleRotation) {
        this.angleRotation = angleRotation;
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        Rect clipBounds = canvas.getClipBounds();
        canvas.save();
        canvas.rotate(angleRotation, clipBounds.exactCenterX(), clipBounds.exactCenterY());
        super.onDraw(canvas);
        canvas.restore();
    }
}

If you play around with clipBounds you may find that helpful. 如果你玩clipBounds,你会发现它很有帮助。

A slight "hack", you could probably adjust the image size using something like Paint.net or Gimp, depending on your OS. 轻微的“黑客”,您可以使用Paint.net或Gimp之类的东西调整图像大小,具体取决于您的操作系统。 That would make the image appear to spin on another point. 这将使图像看起来在另一点旋转。 This solution would be pointless if you are planning on using a lot of images though. 如果您计划使用大量图像,此解决方案将毫无意义。 This is a spinning cube tutorial using opengl. 是一个使用opengl的旋转立方体教程。

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

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