【问题标题】:Android TranslateAnimation animationAndroid TranslateAnimation 动画
【发布时间】:2012-07-05 18:21:27
【问题描述】:

我有一个由一些按钮混合在一起的复合视图,这些按钮使用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);
    }

}

有什么想法吗?

【问题讨论】:

    标签: android android-animation


    【解决方案1】:

    默认情况下,动画会将对象重置为初始状态。您需要将fillAfter指定为true

    animation.setFillAfter(true);
    

    【讨论】:

    • 谢谢!它有效,但在这种情况下,我的“打开-关闭”按钮似乎没有响应点击?我说的对吗?
    • Honeycomb 之前的动画框架只移动布局的视觉部分。物理位置保持不变,您必须自己移动它。 Honeycomb 引入了 Animator 类,更具体地说是 PropertyAnimator,它将动画和物理移动对象。
    • 我正在使用 Android 2.1,所以我应该怎么做才能移动带有动画的实际对象而不仅仅是它们的快照?
    • 要么弄乱View 的边距,要么添加一个AnimationListener,在动画结束时在布局内物理移动对象(即不要使用setFillAfter)。
    • 亲爱的 Jens,我必须使用 setFillAfter(true)(再次阅读问答),所以现在是为布局设置新边距的唯一方法吗?
    【解决方案2】:

    为了使按钮在翻译后正常工作,您还需要将其物理移动到新位置。您可以通过以编程方式更改布局位置来做到这一点。

    设置一个动画侦听器并在 onAnimationEnd 内部执行此操作 -

      @Override
      public void onAnimationEnd(Animation animation) {
    
    yourLayout.layout(newLeft, newTop, newRight, newBottom);
    
      }
    

    要了解有关蜂窝 API 之前存在的动画约束的更多信息,请阅读此文章 - http://android-developers.blogspot.in/2011/02/animation-in-honeycomb.html

    【讨论】:

    • 我用滑动抽屉和淡入淡出完成了这项工作!我认为在程序中使用布局在大中型应用程序中不是一个好习惯。
    • 但是滑动抽屉有一个限制,它可以从下到上和从右到左切换。如果您需要抽屉从左到右或从上到下打开,它不起作用。无论如何,祝你好运!
    猜你喜欢
    • 1970-01-01
    • 2012-07-01
    • 2016-03-04
    • 2012-01-03
    • 2011-11-19
    • 2012-10-10
    • 2013-10-20
    • 2012-01-17
    • 1970-01-01
    相关资源
    最近更新 更多