TranslateAnimation如何在Android上运行?

Yas*_*han 22 android android-animation

我经历了

TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
Run Code Online (Sandbox Code Playgroud)

但我仍然对如何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 
Run Code Online (Sandbox Code Playgroud)

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

编辑:我有Button一个LinearLayout没有任何孩子.当我点击时,Button我想动态生成一个TextView和动画TextView显示在LinearLayout.数TextView旨意取决于在按钮上点击次数.

Hir*_*ral 25

AFAIK,这之间会有相对联系.

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

此时,您根本不需要更改Y方向.对于两个选项,这将是0%.最后,您将具有:

来自XDelta 100%

toXDelta 0%

来自YDelta 0%

toYDelta 0%

您可以根据需要将此百分比设置在0到100之间来限制组件的视图.

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

希望,现在对你很清楚.

编辑:

根据您的要求,您需要覆盖按钮1的onclick,在那里您可以控制按钮2的可见性以及翻译.

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

translate_button.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>
Run Code Online (Sandbox Code Playgroud)

现在,在您的活动文件中

...

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

...
Run Code Online (Sandbox Code Playgroud)