0

我創建了自定義FrameLayout。我在主體周圍增加了1個主圈和5個圈。我想圍繞主圈旋轉圈子。在自定義FrameLayout中旋轉Multi ImageView

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 套件

+0

我完全不明白你想要什麼。您想要在主圖像周圍添加圖像,o製作旋轉動畫,其中用戶可以旋轉圖像環? – neworld

+0

沒有動畫。觸摸其中一個並圍繞主圖像旋轉。 – user1509068

回答

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