在自定义FrameLayout中旋转多个ImageView

时间:2012-09-27 21:52:35

标签: android android-layout android-ui ontouchlistener android-framelayout

我创建了自定义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 试剂盒

1 个答案:

答案 0 :(得分: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);
}