Android:旋转按钮?

时间:2015-07-27 16:29:06

标签: java android button rotation

我试图创建一个可由用户旋转的按钮,到目前为止,这是我的代码:

    slider.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {

                case MotionEvent.ACTION_DOWN:
                    startAngle = (float) getAngle(event.getX(), event.getY());
                    break;

                case MotionEvent.ACTION_MOVE:
                    double currentAngle = getAngle(event.getX(), event.getY());
                    sliderRotate((float)currentAngle, slider);
                    Log.v("Current angle:", String.valueOf(currentAngle));
                    startAngle = (float)currentAngle;
                    break;

                case MotionEvent.ACTION_UP:
                    break;
            }

            return true;
        }
    });

    return rootView;
}

private double getAngle(double xTouch, double yTouch) {
    double x = xTouch - (Width / 2d);
    double y = Height - yTouch - (Height / 2d);

    switch (getQuadrant(x, y)) {
        case 1:
            return Math.asin(y / Math.hypot(x, y)) * 180 / Math.PI;
        case 2:
            return 180 - Math.asin(y / Math.hypot(x, y)) * 180 / Math.PI;
        case 3:
            return 180 + (-1 * Math.asin(y / Math.hypot(x, y)) * 180 / Math.PI);
        case 4:
            return 360 + Math.asin(y / Math.hypot(x, y)) * 180 / Math.PI;
        default:
            return 0;
    }
}

private static int getQuadrant(double x, double y) {
    if (x >= 0) {
        return y >= 0 ? 1 : 4;
    } else {
        return y >= 0 ? 2 : 3;
    }
}

private void sliderRotate(float degrees, ImageView slider) {
    slider.setRotation(degrees);
    Log.v("Rotation:", String.valueOf(degrees));
}

我在这里找到了它:http://code.tutsplus.com/tutorials/android-sdk-creating-a-rotating-dialer--mobile-8868

0 个答案:

没有答案