【问题标题】:How does TranslateAnimation work on Android?TranslateAnimation 如何在 Android 上运行?
【发布时间】:2018-02-08 06:30:04
【问题描述】:

我经历过

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 

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

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

【问题讨论】:

  • 你的意思是说,你首先有一个小按钮,你想让它足够大(拉伸)到一个确定的位置,点击它?
  • @Hiral 不,当我们单击第一个按钮时会有两个按钮,第二个按钮将可见并动画到一个点。
  • 请检查我的更新答案!

标签: android android-animation


【解决方案1】:

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

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

此时,您根本不需要更改 Y 方向。所以这两个选项都是 0%。所以最后,您将拥有:

来自XDelta 100%

toXDelta 0%

来自YDelta 0%

toYDelta 0%

您可以根据您的要求将此百分比设置在 0 到 100 之间,从而限制对组件的查看。

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

希望,你现在清楚了。

编辑:

根据您的要求,您需要覆盖 button-1 的 onclick,然后您可以控制 button-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>

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

...

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

...

【讨论】:

  • 感谢亲爱的 +1 的回答,我会调查的。
  • @YasirKhan:您需要根据需要更改它。这只是一个演示,可以给您一些想法。请自行尝试。或发布您尝试过的内容。
  • 我有一个 Button 和一个没有任何子项的 LinearLayout,因为当我单击 Button 时,我想生成一个动态 TextView 并将 TextView 动画化为 LinearLayout,TextView 的数量将取决于数量点击按钮。
  • 然后,在单击按钮时,您需要创建 textview 并将其添加到线性布局中。然后您必须为该线性布局设置动画。或者另一个选项是,您必须创建一个线性单击按钮时包含 textview 的布局。并为相同的线性布局设置动画。稍后做一些事情,您将拥有各种线性布局并对 textview 产生连续的效果。
  • 是的,我的问题是 TranslateAnimation 如何在 android 中工作?因为我知道初始坐标和最终坐标。
【解决方案2】:

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

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

例如,您要将对象移动到 右侧,您可以执行以下操作: TranslateAnimation(0.0f, 1.0f, 0.0f, 0.0f)

(或使用Animation.ABSOLUTEAnimation.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

【讨论】:

  • 感谢亲爱的 +1 的回答,我会调查的。
猜你喜欢
  • 2012-10-10
  • 2012-07-15
  • 2012-01-17
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 2012-01-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多