1
如何使動畫僅在父視圖上運行,並保持子視圖不變(即,無需爲它們設置動畫)?Android - 僅在父視圖上播放動畫
這裏是我的嘗試:
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視圖不旋轉的TextView我在它的中心增加。
你爲什麼不使用的FrameLayout把它包起來,並TurnButton和TextView的兄弟姐妹? – bladefury