0

我需要做一个RelativeLayout一个旋转的动画,我用NineOldAndroid库动画:怎么做旋转动画上的RelativeLayout

View v = findViewById(R.id.wrap_btnSelect); 
ObjectAnimator t1 = ObjectAnimator.ofFloat(
     v, "rotation", 0.0f, -45.0f); 
t1.setStartDelay(500); 
t1.setDuration(1500); 
t1.start(); 

动画正常工作,还可以单击posions相应的更新ICS,但在Gingerbread点击位置停留在旧位置,导致视图无法点击。

有什么我可以做的,以解决这个问题在Android 2.3?

原始布局:

Original Layout

试图实现这一点:

Rotated Layout

编辑:此布局构造的4个方格,每个是一个TextView

回答

1

您可以自行扩展View来处理触摸事件,并根据您点击时的旋转值来判断您要点击哪个区域。

像这样:

public class WrapBtnSelect extends RelativeLayout { 
    // Copy onstructors here 

    @Override 
    public boolean onTouchEvent(MotionEvent event) { 
     if (event.getAction() == MotionEvent.ACTION_DOWN) { 
      double x = (event.getX()/getWidth()) * 2 - 1; 
      double y = (event.getY()/getHeight()) * 2 - 1; 
      double angle = Math.atan2(y, x); 
      // Do your actions depending on the angle here 

      return true; 
     } 
     return super.onTouchEvent(event); 
    } 
} 
+0

此布局构造的4个方格,每个是一个'TextView'。这种方法似乎在某种程度上是过度劳累。是否有可能实际旋转按钮? – Nevercom

+1

你可以在你的'RelativeLayout'上调用'View.setRotation()',但它只能在API 11中使用(顺便说一下,你还使用'ObjectAnimator',它只能从API 11中获得)。要从API 8开始工作,我将使用View Animation动画“RelativeLayout”,并使用自定义类来处理点击事件,因为View Animation实际上并不移动视图,所以根据动画不同,您的位置不会更新。 – minipif