Android圈不能从中心旋转?

时间:2016-04-01 07:35:07

标签: java android mobile geometry

我无法让圆圈围绕其中心旋转。

这是我的xml:

public class MainActivity
    extends Activity
    implements View.OnTouchListener {
    private static final String TAG = "MainActivity";
    ImageView circleView;
    double currentRotationDegrees;
    double cx;
    double cy;
    float mPreviousX;
    float mPreviousY;
    private Matrix rotationMatrix;

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        this.setContentView(R.layout.activity_main);
        this.circleView = (ImageView)this.findViewById(R.id.wheel);
        this.circleView.setOnTouchListener((View.OnTouchListener) this);
        this.circleView.setScaleType(ImageView.ScaleType.MATRIX);
        if (this.rotationMatrix == null) {
        this.rotationMatrix = new Matrix();
        }
        this.rotationMatrix.reset();
        this.currentRotationDegrees = 0.0;
    }

/*
 * Enabled aggressive block sorting
 */
    public boolean onTouch(View view, MotionEvent motionEvent) {
        this.cx = (double)this.circleView.getWidth() / 2.0;
        this.cy = (double)this.circleView.getHeight() / 2.0;
        float f = motionEvent.getX();
        float f2 = motionEvent.getY();

        Log.d((String)"MainActivity", (String)("Got x: " + f + " and y: " + f2));
        switch (motionEvent.getAction()) {

            case 1: {
                Log.d((String)"MainActivity", (String)("Current rotation degrees = " + this.currentRotationDegrees));
                double d = this.currentRotationDegrees % 30.0;
                Log.d((String)"MainActivity", (String)("Rotation modulus = " + d));
                if (d == 0.0) break;
                double d5 = d < 15.0 ? -1.0 * d : 30.0 - d;
                this.rotationMatrix.postRotate((float)d5, (float)this.cx, (float)this.cy);
                this.circleView.setImageMatrix(this.rotationMatrix);
                this.currentRotationDegrees -= d;
            }

            case 2: {
                Log.d((String)"MainActivity", (String)"Calculating rotation..");
                double d = (double)f - this.cx;
                double d2 = Math.atan2((double)f2 - this.cy, d);
                double d3 = (double)this.mPreviousX - this.cx;
                double d4 = Math.toDegrees(d2 - Math.atan2((double)this.mPreviousY - this.cy, d3));
                Log.d((String)"MainActivity", (String)("Rotation degrees: " + d4));
                this.rotationMatrix.postRotate((float) d4, (float) this.cx, (float) this.cy);
                this.circleView.setImageMatrix(this.rotationMatrix);
                this.currentRotationDegrees = d4 + this.currentRotationDegrees;
            }
            default: {
                break;
            }

        }

        this.mPreviousX = f;
        this.mPreviousY = f2;
        return true;
    }
}

以下是所发生情况的屏幕截图:

screenshot

从图像中可以看出,圆圈不是中心,也不会围绕中心旋转。圆圈也被推到屏幕

这是我的java:

MariaDB [(none)]> show global variables like 'event_scheduler';
+-----------------+-------+
| Variable_name   | Value |
+-----------------+-------+
| event_scheduler | OFF   |
+-----------------+-------+
1 row in set (0.00 sec)

2 个答案:

答案 0 :(得分:0)

我认为在你的xml中,LinearLayout在你的圈外,你设置了LinearLayout的layout_margin。如果你想让圆圈围绕它的中心旋转,你应该得到LinearLayout.s宽度和它的边距并除以2.

答案 1 :(得分:0)

替换此代码。

       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="0.0dip">

        <ImageView
            android:id="@+id/image_camelot_wheel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/wheel" />

        <ImageView
            android:id="@+id/image_camelot_arrows"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/centre" />