2014-03-27 2 views
1

하위 뷰를 그대로 유지하면서 애니메이션을 부모 뷰에서 만 실행하려면 어떻게해야합니까?안드로이드 - 상위 뷰에서만 애니메이션 재생

public class TurnButton extends RelativeLayout implements OnClickListener, AnimationListener{ 

    private static final String TAG = TurnButton.class.getSimpleName(); 

    private final int textViewID = 1; 

    public TurnButton(final Context context, final AttributeSet attrs, final int defStyle) { 

     super(context, attrs, defStyle); 
     setEverything(); 
    } 

    public TurnButton(final Context context, final AttributeSet attrs) { 

     super(context, attrs); 
     setEverything(); 
    } 

    public TurnButton(final Context context) { 

     super(context); 
     setEverything(); 
    } 

    private void setEverything(){ 

     setOnClickListener(this); 

     final TextView textViewNumber = new TextView(ThisApplication.getContext()); 
     textViewNumber.setId(textViewID); 
     textViewNumber.setText("30"); 
     textViewNumber.setTextColor(ThisApplication.getContext().getResources().getColor(R.color.white)); 

     final RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 
     relativeParams.addRule(RelativeLayout.CENTER_IN_PARENT); 

     addView(textViewNumber, relativeParams); 
    } 

    @Override 
    public void onClick(final View view) { 

     final RotateAnimation rotate = new RotateAnimation(view.getRotation(), view.getRotation() - 360.0f, RotateAnimation.RELATIVE_TO_SELF, 0.5f, 
                  RotateAnimation.RELATIVE_TO_SELF, 0.5f); 
     rotate.setDuration(300); 
     rotate.setFillEnabled(true); 
     rotate.setFillAfter(true); 
     view.startAnimation(rotate); 

     view.findViewById(textViewID).setRotation(0); 
    } 
} 

내가 원하는 내가 그것의 중앙에 추가 텍스트 뷰를 회전하지 않고 TurnButton보기를 회전 :

여기 내 시도이다.

+1

왜 당신이 그것을 마무리하고 TurnButton 및 텍스트 뷰 형제를 만들기 위해 FrameLayout이를 사용하지 않는? – bladefury

답변

1

그것은 아이 뷰 (텍스트 뷰)에 역 회전을 적용함으로써 가능하다 :

@Override 
public void onClick(final View view) { 

    final RotateAnimation rotate = new RotateAnimation(view.getRotation(), 
      view.getRotation() - 360.0f, RotateAnimation.RELATIVE_TO_SELF, 0.5f, 
      RotateAnimation.RELATIVE_TO_SELF, 0.5f); 
    rotate.setDuration(300); 
    rotate.setFillEnabled(true); 
    rotate.setFillAfter(true); 
    view.startAnimation(rotate); 

    final TextView textView = (TextView)view.findViewById(textViewID); 
    final RotateAnimation reverseRotate = new RotateAnimation(view.getRotation(), 
      360.0f - view.getRotation(), RotateAnimation.RELATIVE_TO_SELF, 0.5f, 
      RotateAnimation.RELATIVE_TO_SELF, 0.5f); 
    reverseRotate.setDuration(300); 
    reverseRotate.setFillEnabled(true); 
    reverseRotate.setFillAfter(true); 
    textView.startAnimation(reverseRotate); 
} 
관련 문제