我正在尝试使用两个textview制作动画。两者都处于相对布局中。动画的功能是留下textview将稍微离开,同时右侧textview也将留下一点点。我试过了:
http://nineoldandroids.com/和默认方式。
但对于这两种情况,我在这个过程中都有差距。我已经提出了一个问题,但我没有得到任何肯定的答复:
Android slider animation is not synchronized
Nineoldandroids代码:
xml文件:
<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>
MainActivity.java:
public class MainActivity extends Activity {
double counter = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.toggles);
final View target = findViewById(R.id.target);
final int duration = 5*1000;
final int duration1 = 300;
final View textView1 = findViewById(R.id.TextView01);
final View textView2 = findViewById(R.id.TextView02);
textView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (counter == 0) {
textView2.setVisibility(View.VISIBLE);
ObjectAnimator.ofFloat(textView1, "translationX", 0, -50).setDuration(duration1).start();
ObjectAnimator.ofFloat(textView2, "translationX", 100, 0).setDuration(duration1).start();
counter++;
}
else {
ObjectAnimator.ofFloat(textView1, "translationX", -50,0).setDuration(duration1).start();
ObjectAnimator.ofFloat(textView2, "translationX", 0,100).setDuration(duration1).start();
counter--;
}
}
});
}
}
我该如何解决?
否则我试图将textview放在第二个textview将在屏幕之外的布局中,并使用动画我将移动整个布局。我怎么能像这样制作xml?
答案 0 :(得分:2)
很抱歉误解了你的问题。
无论如何,你的问题与时间无关。两个动画具有相同的持续时间,但问题是动画区域的大小是不同的。从逻辑上讲,在相同的持续时间内移动较长的距离看起来比移动较小的距离要慢。
例如,为了解决您的问题,我在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--;
}
}
答案 1 :(得分:1)
试试这个
<!-- 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>
然后使用这样的ViewPropertyAnimator
(如果你可以使用它)
//Translate your view -50 to the left.
yourLinearLayout.animate().translationX(-50).setDuration(1000);
使用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();
希望这有助于你