0

我正在使用 minSDK = 8 和 targetSDK = 17

在活动开始时,我的 3 个屏幕(3 个不同的视图)在 XML 布局中设置为不可见。在 onResume 中,我调用了 setScreen(1),它启动了一个将 View 'wait' 设置为可见的动画。然而,在 'wait' 设置为可见的第 34 行,没有任何反应。如果我从第 43 行中删除注释 // 'main' 设置为不可见,'wait' 变为可见。

为什么注释掉第 43 行时不出现“等待”?为什么需要将已经不可见的“主”视图设置为不可见才能出现“等待”?

public class TestActivity extends Activity {
static final String TAG = "test";
public Context context;
Activity myActivity;
UserFunctions userFunctions;
Button but_begin;
int status;
View main;
View wait;
View noconnect;
Animation fadeIntoWait;
Animation fadeIntoConnect;
Animation fadeIntoMain;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.test);
    context = getApplicationContext();
    myActivity = this;

    main = findViewById(R.id.screen_main);
    wait = findViewById(R.id.screen_wait);
    noconnect = findViewById(R.id.screen_connect);

    fadeIntoWait = AnimationUtils.loadAnimation(this, R.anim.fade_in);
    fadeIntoConnect = AnimationUtils.loadAnimation(this, R.anim.fade_in);
    fadeIntoMain = AnimationUtils.loadAnimation(this, R.anim.fade_in);

    fadeIntoWait.setAnimationListener(new AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
            wait.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            //main.setVisibility(View.INVISIBLE);
            //noconnect.setVisibility(View.INVISIBLE);

        }
    });
    fadeIntoConnect.setAnimationListener(new AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

            noconnect.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            wait.setVisibility(View.INVISIBLE);
        }
    });
    fadeIntoMain.setAnimationListener(new AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

            main.setVisibility(View.VISIBLE);

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
            wait.setVisibility(View.INVISIBLE);
        }
    });

}

@Override
public void onStart() {

    super.onStart();

}

@Override
public void onResume() {
    super.onResume();

    setScreen(1);

}

public void setScreen(int screen) {

    switch (screen) {
    case 1: // into wait
        wait.startAnimation(fadeIntoWait);

        break;
    case 2: // to noconnect
        noconnect.startAnimation(fadeIntoConnect);

        break;

    case 3: // to main
        main.startAnimation(fadeIntoMain);

        break;
    }
}

public void gotoActivity(Class<?> s) {
    Intent g = new Intent(getApplicationContext(), s);

    // Close all views before launching Dashboard
    g.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(g);
    overridePendingTransition(R.anim.fade_in, R.anim.fade_out);
}

public void retryConnect(View view) {

}

public void begin(View view) {
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add(0, 1, 0, "Exit App");
    menu.add(0, 2, 0, "Close Menu");
    menu.add(0, 3, 0, "Dashboard");

    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    case 1: // log out
        closeOptionsMenu();
        return true;
    case 2: // Exit App
        finish();
        return true;
    case 3: // Exit App
        gotoActivity(DashboardActivity.class);
        return true;

    }
    return super.onOptionsItemSelected(item);

}

public void menu(View v) {

    openOptionsMenu();

}

}

这是 XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >

<LinearLayout
    android:id="@+id/header"
    android:layout_width="match_parent"
    android:layout_height="44dp"
    android:layout_alignParentTop="true"
    android:background="@drawable/line" >
</LinearLayout>

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@id/header" >

    <RelativeLayout
        android:id="@+id/screen_wait"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000"
        android:visibility="gone" >

        <ImageView
            android:id="@+id/img_wait"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:contentDescription="@string/wait"
            android:src="@drawable/wait"
             />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/screen_connect"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000"
        android:visibility="gone" >

        <ImageView
            android:id="@+id/img_connect"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="center"
            android:contentDescription="@string/connect"
            android:onClick="retryConnect"
            android:src="@drawable/connect"
            />
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/screen_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000"
        android:visibility="gone" >

        <ScrollView
            android:id="@+id/middle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/footer"
            android:background="#2F2430" >

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="#2F2430" >

                <TextView
                    android:id="@+id/textView3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:text="@string/idea"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="#21dbd4" />

                <Button
                    android:id="@+id/but_begin"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/TextView01"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="67dp"
                    android:onClick="begin"
                    android:text="Begin" />
            </RelativeLayout>
        </ScrollView>

        <RelativeLayout
            android:id="@+id/footer"
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:layout_alignParentBottom="true"
            android:background="@drawable/line"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:text="Pennies:" />

            <TextView
                android:id="@+id/textView4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginLeft="22dp"
                android:layout_toRightOf="@+id/textView2" />

            <Button
                android:id="@+id/btnMenu"
                style="?android:attr/buttonStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:onClick="menu"
                android:text="Menu" />
            </RelativeLayout>
        </RelativeLayout>
    </FrameLayout>

</RelativeLayout>
4

1 回答 1

2

你的 UI 直到 onCreate() 之后才会布局。尝试将动画移动到 onStart() 或更好,一旦布局完成,使用 ViewTreeObserver 启动它们

于 2013-07-10T13:26:01.133 回答