围绕指定点旋转图像不起作用!(安卓) [英] Rotating an image around a specified point doesn't work! (Android)

查看:34
本文介绍了围绕指定点旋转图像不起作用!(安卓)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 postRotate(float degree, float px, float py) 旋转 ImageView,将 px 和 py 设置为几个不同的值,包括 (0,0) 和 (imgView.getHeight(),imgView.getWidth()) 但它拒绝围绕中心以外的任何其他点旋转.我想知道这是否与我的重心位于 LinearLayout 中这一事实有关?

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. I'm wondering whether it's got anything to do with the fact that my gravity is center in the LinearLayout?

我的布局:

<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" />

我如何旋转图像:

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

PS 我注意到有一些类似的问题,但都没有合适的答案.

PS I've noticed there are a few similar questions, but none of them have suitable answers.

推荐答案

我正在使用自定义 ImageView 来设置角度旋转.

I am using a custom ImageView where I set the angle rotation.

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();
    }
}

如果您使用 clipBounds,您可能会发现这很有帮助.

If you play around with clipBounds you may find that helpful.

这篇关于围绕指定点旋转图像不起作用!(安卓)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆