Android TranslateAnimation 动画

发布于 2024-12-28 08:09:15 字数 1971 浏览 5 评论 0原文

我有一个由一些按钮混合而成的复合视图,它通过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);
    }

}

有什么想法吗?

I have a compound view mixed by some buttons on it that is attached on top-right corner of screen with RelativeLayout. I want this view to animate to right when I click on "open-close" button on it and stays there until user selects/clicks one of its buttons or clicks again on "open-close" button and then it should animate to right and become invisible. The problem is that it animates to left and then it moves back to its original place! What should I do to solve this problem?

Code:

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);
    }

}

Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

秋日私语 2025-01-04 08:09:15

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

animation.setFillAfter(true);

By default, the animation resets the object to the initial state. You need to specify fillAfter to true:

animation.setFillAfter(true);
故事未完 2025-01-04 08:09:15

为了使按钮在翻译后起作用,您还需要将其物理移动到新位置。您可以通过以编程方式更改布局位置来完成此操作。

设置动画监听器和动画监听器在 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

In order to make the button work after translation you need to shift it physically as well to the new position. You can do this by changing the layout position programatically.

Set an animation listener & Inside onAnimationEnd do this -

  @Override
  public void onAnimationEnd(Animation animation) {

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

  }

To know more about the animation constraints that exists prior to honeycomb API read this - http://android-developers.blogspot.in/2011/02/animation-in-honeycomb.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文