簡體   English   中英

Android TranslateAnimation動畫

[英]Android TranslateAnimation animation

我有一個混合視圖,上面混合了一些按鈕,這些按鈕通過RelativeLayout附加在屏幕的右上角。 當我單擊視圖上的“打開-關閉”按鈕並停留在那里直到用戶選擇/單擊其按鈕之一或再次單擊“打開-關閉”按鈕時,我希望此視圖向右移動,然后應該向右和變得隱形。 問題在於它會向左移動動畫,然后移回其原始位置! 我該怎么做才能解決這個問題?

碼:

public class AlarmCommandComponent extends LinearLayout {

    ImageButton carOffButton;
    ImageButton carOnButton;
    ImageButton armButton;
    ImageButton disArmButton;
    ImageButton openCloseButton;
    LayoutAnimationController controller;
    Animation animation;

    public AlarmCommandComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.car_alarm_view, this);

        openCloseButton = (ImageButton) findViewById(R.id.alarmOpenCloseButton);

        AnimationSet set = new AnimationSet(true);
        animation = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, //fromXType 
                0.0f,                       //fromXValue
                Animation.RELATIVE_TO_SELF, //toXType
                -1.0f,                      //toXValue
                Animation.RELATIVE_TO_SELF, //fromYType
                0.0f,                       //fromYValue
                Animation.RELATIVE_TO_SELF, //toYType
                0.0f);                      //toYValue
        animation.setDuration(500);
        set.addAnimation(animation);
        LayoutAnimationController controller = new LayoutAnimationController(set, 0.25f);
        this.setLayoutAnimation(controller);
        this.setPadding(0, 7, 7, 10);
        this.setBackgroundColor(Color.BLACK);

        openCloseButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                openCloseButton_onClick(v);
            }
        });
    }

    public void openCloseButton_onClick(View v) {
        this.startAnimation(animation);
    }

}

任何想法?

默認情況下,動畫會將對象重置為初始狀態。 您需要將fillAfter指定為true

animation.setFillAfter(true);

為了使按鈕在平移后能夠正常工作,您需要將其物理位置也移至新位置。 您可以通過編程方式更改布局位置來實現。

設置動畫偵聽器,並在onAnimationEnd內部執行此操作-

  @Override
  public void onAnimationEnd(Animation animation) {

yourLayout.layout(newLeft, newTop, newRight, newBottom);

  }

要了解有關Honeycomb API之前存在的動畫約束的更多信息,請閱讀以下內容-http://android-developers.blogspot.in/2011/02/animation-in-honeycomb.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM