0

我创建了自定义 FrameLayout。我添加了 1 个主圈和 5 个围绕主圈的另一个圈。我想围绕主圆圈旋转圆圈。

public class Circles extends FrameLayout{
ImageView mMainCircle;
ImageView mCircle0;
ImageView mCircle1;
ImageView mCircle2;
ImageView mCircle3;
ImageView mCircle4;    

public Circles(Context context) {
    super(context, null, 0);
    init();
}

public Circles(Context context, AttributeSet attrs) {
    super(context, attrs, 0);
    init();
}

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

public void init(){
    // set positions, onClick and add
    Cicle.setOnClickListener(mOnClickListener);
    this.addView(mMainCircle);
    this.addView(Circle0);
    this.addView(Circle1);
    this.addView(Circle2);
    this.addView(Circle3);
    this.addView(Circle4);

    }
}

我怎样才能围绕主圈旋转所有圈。我尝试了所有 circleX 的 setOnTouchListener ,但它不起作用。

CircleX.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:

                    break;
                case MotionEvent.ACTION_MOVE:

                    break;
                case MotionEvent.ACTION_UP:

                    break;
                default:
                    break;
                }
                return false;
            }
        });

如果可能的话,我需要一个代码或示例:)

THX 套件

4

1 回答 1

0
    private float angle = 0f;
    private float theta_old = 0f;
    private RotaryKnobListener listener;
    public interface RotaryKnobListener {
    public void onKnobChanged(int arg);
    }

    public void setKnobListener(RotaryKnobListener l) {
    listener = l;
    }

    private float getTheta(float x, float y) {
    float sx = x - (obj.getWidth() / 2.0f);
    float sy = y - (obj.getHeight() / 2.0f);

    float length = (float) Math.sqrt(sx * sx + sy * sy);
    float nx = sx / length;
    float ny = sy / length;
    float theta = (float) Math.atan2(ny, nx);

    final float rad2deg = (float) (180.0 / Math.PI);
    float thetaDeg = theta * rad2deg;

    return (thetaDeg < 0) ? thetaDeg + 360.0f : thetaDeg;
    }

    public void setRotateListener() {
    mRotateImage.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            float x = event.getX(0);
            float y = event.getY(0);
            float theta = getTheta(x, y);

            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_POINTER_DOWN:
                theta_old = theta;
                break;
            case MotionEvent.ACTION_MOVE:
                float delta_theta = theta - theta_old;
                theta_old = theta;
                int direction = (delta_theta > 0) ? 1 : -1;
                angle += 3 * direction;

                Log.d("Tag", "rotate angle : " + obj.getHeight());
                obj.setRotation(angle);
                notifyListener(direction);
                break;
            }
            return true;
        }
    });
}

private void notifyListener(int arg) {
    if (null != listener)
        listener.onKnobChanged(arg);
}
于 2015-05-07T05:58:37.890 回答