重新定位视图时的动画

时间:2016-05-06 01:41:58

标签: android android-layout android-animation android-view

正如您在附加代码中看到的那样,linearLayoutLogo包含ProgressBarTextViewlinearLayoutReading包含ImageViewTextView。当linearLayoutReading可见时,由于android:layout_centerInParent="true",两个布局会围绕中心点重新定位。

我如何设置linearLayoutLogo动画,以便在linearLayoutReading变为可见之前向上移动到新位置?请参阅附件video for an example

activity_main.xml中

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_centerInParent="true"
    android:gravity="center">

    <LinearLayout
        android:id="@+id/linearLayoutLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center">

        <ImageView
            android:id="@+id/imageViewRLogo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_accessibility_black_24dp"
            android:padding="8dp"/>

        <TextView
            android:id="@+id/textViewAppTitle2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="34sp"
            android:textColor="#CCFFFFFF"
            android:text="AppName"/>

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayoutReading"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="24dp">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            style="@style/Base.TextAppearance.AppCompat.Large"
            android:text="Reading from device"
            android:textColor="#BF000000" />
        <ProgressBar
            android:id="@+id/progressBarSplash"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            style="@style/Base.Widget.AppCompat.ProgressBar.Horizontal"
            android:paddingBottom="8dp"/>

    </LinearLayout>

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {
public ProgressBar progressBarSplash;
public LinearLayout linearLayoutReading;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    linearLayoutReading = (LinearLayout)findViewById(R.id.linearLayoutReading);
    linearLayoutReading.setVisibility(View.GONE);

    new CountDownTimer(5000, 1000) {

        public void onTick(long millisUntilFinished) {
        }

        public void onFinish() {
            linearLayoutReading.setVisibility(View.VISIBLE);
            progressBarSplash = (ProgressBar)findViewById(R.id.progressBarSplash);
            progressBarSplash.setEnabled(true);
            progressBarSplash.setIndeterminate(true);
        }
    }.start();
}
}

1 个答案:

答案 0 :(得分:2)

在Android Studio上测试过。只需在最高android:animateLayoutChanges="true"LinearLayout元素之一)中加入centerInParent