TranslateAnimation 如何在 Android 上工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11504005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does TranslateAnimation work on Android?
提问by Yasir Khan
I went through
我经历了
TranslateAnimation (float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
but am still confused about how Translate animation
works.
但我仍然对如何Translate animation
工作感到困惑。
Can someone please explain how it works? I read the docs which say
有人可以解释一下它是如何工作的吗?我阅读了说的文档
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
but it is still not clear to me how it works.
但我仍然不清楚它是如何工作的。
EDIT: I have a Button
and a LinearLayout
without any children. When I am clicking on the Button
I want to dynamically generate a TextView
and animate that TextView
to appear in the LinearLayout
. The number of TextView
s will depend upon the number of clicks on the Button.
编辑:我有一个Button
和一个LinearLayout
没有任何孩子。当我点击Button
我想动态生成一个TextView
并动画TextView
显示在LinearLayout
. 数TextView
旨意取决于在按钮上点击次数。
回答by Hiral Vadodaria
AFAIK,there would be relative connection between this.
AFAIK,这之间会有相对联系。
That is,if you want to translate a hidden textview from right of screen to left of screen,on click of a button,you actually need to translate it from 100% of X-direction(right side of screen) to 0% of X-direction(left side of screen).
也就是说,如果你想将隐藏的文本视图从屏幕右侧转换到屏幕左侧,点击一个按钮,你实际上需要将它从 X 方向(屏幕右侧)的100% 转换到 X 的0% -方向(屏幕左侧)。
At this point,you don't need to change Y-direction at all.so that would be 0% for both the options.So finally,you will have:
在这一点上,你根本不需要改变 Y 方向。所以这两个选项都是 0%。所以最后,你将有:
fromXDelta100%
来自 XDelta100%
toXDelta0%
toXDelta0%
fromYDelta0%
来自YDelta0%
toYDelta0%
toYDelta0%
you can restrict view of the component by setting this percentages between 0 to 100,as per your requirement.
您可以根据您的要求将此百分比设置在 0 到 100 之间来限制组件的查看。
Similarly,if you need to translate your component on Y-direction as well,then you need to change 0% to some other value.
同样,如果您还需要在 Y 方向上平移组件,则需要将 0% 更改为其他值。
Hope,its clear to you now.
希望,你现在清楚了。
EDIT :
编辑 :
for your requirement,you need to override onclick of button-1,and there you can control button-2's visibility as well as translation.
根据您的要求,您需要覆盖按钮 1 的点击,然后您可以控制按钮 2 的可见性以及翻译。
create animation file in anim folder in your res.
在 res 中的 anim 文件夹中创建动画文件。
translate_button.xml:
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>
now,in your activity file,
现在,在您的活动文件中,
...
// 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);
}
}
});
...
回答by eMi
With TranslateAnimation you can create an animation to control an object.
使用 TranslateAnimation,您可以创建动画来控制对象。
With TranslateAnimation you're able to control the position of an object. You pass this 4 parameters, which stand for the X and Y coordinates.
使用 TranslateAnimation,您可以控制对象的位置。您传递这 4 个参数,它们代表 X 和 Y 坐标。
By Example you want to move an object to the right, you would do something like: TranslateAnimation(0.0f, 1.0f, 0.0f, 0.0f)
例如,您想将对象向右移动,您可以执行以下操作: TranslateAnimation(0.0f, 1.0f, 0.0f, 0.0f)
(or use Animation.ABSOLUTE
, Animation.RELATIVE_TO_SELF
)
(或使用Animation.ABSOLUTE
, Animation.RELATIVE_TO_SELF
)
We only use the X coordinate now, because we are doing now an easy "LeftToRight" animation-move.
我们现在只使用 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 to right
= 1 向右
Maybe take a look at http://en.wikipedia.org/wiki/Coordinate_system