Android 具有多个视图的滑动动画同步问题
我正在尝试用两个文本视图制作动画。两者都是相对布局。动画的功能是左文本视图将向左移动一点,同时右文本视图也将向左移动一点。我试过: 和默认方式。 但在这两种情况下,我都得到了一个过程中的差距。我已经提出了一个问题,但没有得到任何肯定的答复: NINEOLDADROIDS代码: xml文件:Android 具有多个视图的滑动动画同步问题,android,android-animation,android-xml,nineoldandroids,Android,Android Animation,Android Xml,Nineoldandroids,我正在尝试用两个文本视图制作动画。两者都是相对布局。动画的功能是左文本视图将向左移动一点,同时右文本视图也将向左移动一点。我试过: 和默认方式。 但在这两种情况下,我都得到了一个过程中的差距。我已经提出了一个问题,但没有得到任何肯定的答复: NINEOLDADROIDS代码: xml文件: <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" andro
<RelativeLayout
android:layout_width="match_parent" android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="@+id/target"
android:layout_marginTop="50dp">
<TextView
android:id="@+id/TextView01"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:background="#f00"
android:gravity="center"
android:text="RED"
android:textColor="#000" />
<Button
android:id="@+id/TextView02"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/TextView01"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/TextView01"
android:background="#0F0"
android:text="TXT"
android:visibility="invisible" />
</RelativeLayout>
我怎样才能修好它
否则,我试图将两个textview都放在一个布局中,其中第二个textview将位于屏幕之外,并使用动画移动整个布局。我怎样才能使xml像这样呢?
试试这个
<!-- YOUR CONTAINER -->
<LinearLayout
android:id="@+id/target"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/target"
android:layout_marginTop="50dp"
android:orientation="horizontal" >
<TextView
android:id="@+id/TextView01"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:background="#f00"
android:gravity="center"
android:text="RED"
android:textColor="#000" />
<Button
android:id="@+id/TextView02"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/TextView01"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/TextView01"
android:background="#0F0"
android:text="TXT"
android:visibility="invisible" />
</LinearLayout>
使用ObjectAnimator
我通过使用此代码获得了相同的结果
Button textView02 = (Button) findViewById(R.id.textView02);
LinearLayout target = (LinearLayout) findViewById(R.id.target);
int width = textView02.getWidth();
ObjectAnimator.ofFloat(target, "translationX", 0, -width).setDuration(1000).start();
希望这对你有所帮助抱歉误解了你的问题 不管怎么说,你的问题与时间无关。两个动画的持续时间相同,但问题是动画区域的大小不同。从逻辑上讲,在相同的持续时间内移动更长的距离看起来比移动更小的距离要慢 例如,为了解决您的问题,我在OnClickListener中使用了以下代码:
public void onClick(View v) {
int width =textView2.getWidth();
if (counter == 0) {
textView2.setVisibility(View.VISIBLE);
ObjectAnimator.ofFloat(textView1, "translationX", 0, -1*width).setDuration(duration1).start();
ObjectAnimator.ofFloat(textView2, "translationX", 1*width, 0).setDuration(duration1).start();
counter++;
}
else {
ObjectAnimator.ofFloat(textView1, "translationX", -1*width, 0).setDuration(duration1).start();
ObjectAnimator.ofFloat(textView2, "translationX", 0, 1*width).setDuration(duration1).start();
counter--;
}
}
从85岁到100岁,你用过你的右手。你应该使用100到115。若要将其放回,请使用从115到100。对不起,它不起作用。您能显示当前结果吗?如果第一个文本视图当前滑动正确,请尝试使用
0,50
和50,0
而不是100,0
和0,100
滑动第二个文本视图。我想你没有明白我的问题。我在动画前后将textview设置在适当的位置。只是在动画制作的时候,我得到了一些小的差距。嘿@Neil,我只是尝试了你所做的一切。但是只有红色的文本视图在移动,我看不到按钮。因为它与红色文本视图重叠。我认为在一个线性布局内,如果您放置一个宽度等于匹配父视图的文本视图,则另一个视图将不可见。在我指定的布局中,没有按钮将位于屏幕外视图的右侧。移除visibility=“invisible”并查看结果。我认为您应该用按钮宽度替换翻译中的50值。可能是按钮的宽度是50dp,但翻译使用的是50px或其他尺寸。对不起,老兄,我刚才也试过了。它不起作用。按钮不出现。只有红色文本VIEW1正在移动。您是否使用按钮的宽度进行翻译?看我的编辑。是的,我也这么做了。我想当我点击布局时,只有文本视图1在移动,而另一部分在屏幕外侧,这可能就是它不移动的原因。谢谢@sherif,工作正常。我假设文本视图2的宽度为50dp,这就是为什么移动将为0到-50。但我错了。再次感谢。
Button textView02 = (Button) findViewById(R.id.textView02);
LinearLayout target = (LinearLayout) findViewById(R.id.target);
int width = textView02.getWidth();
ObjectAnimator.ofFloat(target, "translationX", 0, -width).setDuration(1000).start();
public void onClick(View v) {
int width =textView2.getWidth();
if (counter == 0) {
textView2.setVisibility(View.VISIBLE);
ObjectAnimator.ofFloat(textView1, "translationX", 0, -1*width).setDuration(duration1).start();
ObjectAnimator.ofFloat(textView2, "translationX", 1*width, 0).setDuration(duration1).start();
counter++;
}
else {
ObjectAnimator.ofFloat(textView1, "translationX", -1*width, 0).setDuration(duration1).start();
ObjectAnimator.ofFloat(textView2, "translationX", 0, 1*width).setDuration(duration1).start();
counter--;
}
}