24

我经历了

TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)

但我仍然对如何Translate animation工作感到困惑。

有人可以解释一下它是如何工作的吗?我读了文档说

fromXDelta  Change in X coordinate to apply at the start of the animation
toXDelta    Change in X coordinate to apply at the end of the animation
fromYDelta  Change in Y coordinate to apply at the start of the animation
toYDelta    Change in Y coordinate to apply at the end of the animation 

但我仍然不清楚它是如何工作的。

编辑:我有 aButton和 aLinearLayout没有任何孩子。当我单击时,Button我想动态生成一个TextView并动画TextView显示在LinearLayout. s 的数量TextView取决于按钮的点击次数。

4

2 回答 2

27

AFAIK,这之间会有相对的联系。

也就是说,如果您想将隐藏的文本视图从屏幕右侧翻译到屏幕左侧,单击按钮,您实际上需要将其从 X 方向(屏幕右侧)的 100% 翻译为 X 的 0% -方向(屏幕左侧)。

在这一点上,你根本不需要改变 Y 方向。所以这两个选项都是 0%。所以最后,你将拥有:

来自XDelta 100%

toXDelta 0%

来自YDelta 0%

toYDelta 0%

根据您的要求,您可以通过将此百分比设置在 0 到 100 之间来限制组件的视图。

同样,如果您还需要在 Y 方向上平移组件,则需要将 0% 更改为其他值。

希望,你现在清楚了。

编辑 :

根据您的要求,您需要覆盖 button-1 的 onclick,然后您可以控制 button-2 的可见性以及翻译。

在您的 res 的 anim 文件夹中创建动画文件。

翻译按钮.xml:

<?xml version="1.0" encoding="utf-8"?>
<!-- translating button from right to left -->
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:shareInterpolator="false">
    <translate
        android:fromXDelta="100%" android:toXDelta="0%"
        android:fromYDelta="0%" android:toYDelta="0%"
        android:duration="900"
    />
</set>

现在,在您的活动文件中,

...

// ll  is linear layout containing button_2
//counter is used to manage visibility of button_2 on click of button_1,i.e.1st click-button_2 would be visible,on 2nd click on button_1,it would be invisible.

//you can change behavior as per your need

button_2.setVisibility(View.GONE);
button_1.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        if(counter<1)
        {
            counter++;                  
            button_2.setVisibility(View.VISIBLE);
            Animation anim=AnimationUtils.loadAnimation(context, R.anim.translate_button);
            button_2.startAnimation(anim);
        }
        else
        {
            counter=0;
            button_2.setVisibility(View.GONE);
        }
    }
});
ll.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

        if(counter==1)
        {
            counter=0;
            button_2.setVisibility(View.GONE);
        }
    }
});

...
于 2012-07-16T12:20:13.170 回答
2

使用 TranslateAnimation,您可以创建动画来控制对象。

使用 TranslateAnimation,您可以控制对象的位置。您传递这 4 个参数,它们代表 X 和 Y 坐标。

通过示例,您要将对象向右移动,您可以执行以下操作: TranslateAnimation(0.0f, 1.0f, 0.0f, 0.0f)

(或使用Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF

我们现在只使用 X 坐标,因为我们现在正在做一个简单的“从左到右”动画移动。

Change in X coordinate to apply at the start of the animation
toXDelta (0.0f)    

Change in X coordinate to apply at the end of the animation (1.0f)

= 1 向右

也许看看http://en.wikipedia.org/wiki/Coordinate_system

于 2012-07-16T12:20:40.197 回答