我是 android 动画的新手,并尝试使用 Property Animations。我正在尝试使用平移 x 属性来制作滑动效果。但我没有得到我想要的结果。
我的布局是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/parent"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/first"
android:layout_width="320dp"
android:layout_height="200dp"
android:background="#00ff00" >
</LinearLayout>
<LinearLayout
android:id="@+id/second"
android:layout_width="320dp"
android:layout_height="200dp"
android:background="#0000ff" >
</LinearLayout>
</LinearLayout>
</LinearLayout>
根据布局。我在水平放置的父线性布局中有两个线性布局。并且只显示第一个布局,第二个布局不在屏幕上。我正在尝试为父 x 属性设置动画,以便使用翻译我将滑动父级,第一个将隐藏,第二个将显示。以下是我的代码:
LinearLayout parent_;
LinearLayout first_;
LinearLayout second_;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
parent_ = (LinearLayout) findViewById(R.id.parent);
first_ = (LinearLayout) parent_.findViewById(R.id.first);
second_ = (LinearLayout) parent_.findViewById(R.id.second);
}
并且只是为了模拟我正在使用选项菜单的动画。
static boolean a = true;
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(a){
ObjectAnimator animate = ObjectAnimator.ofFloat(parent_, "x", -parent_.getWidth());
animate.setDuration(500);
animate.start();
animate.addListener(this);
animate.addUpdateListener(this);
a = false;
} else {
ObjectAnimator animate = ObjectAnimator.ofFloat(parent_, "x", -parent_.getWidth(), 0);
animate.setDuration(500);
animate.start();
a = true;
}
return super.onOptionsItemSelected(item);
}
当我启动应用程序时,我的应用程序如下所示:
在动画之后,应用程序看起来像这样:
注意:白色区域实际上是屏幕截图的一部分。背景为白色。
在动画之后而不是显示蓝色布局。它完全向左滑动并隐藏起来。并且不显示蓝色布局。我试图在动画期间拍摄屏幕截图。它看起来像这样:
我做错了什么???我希望我已经很好地解释了我的问题......