我正在使用minSDK = 8和targetSDK = 17
在活动开始时,我的3个屏幕(3个不同的视图)在XML布局中设置为不可见。在onResume中,我调用setScreen(1),它启动一个动画,将View'wait'设置为可见。但是,在第34行,'wait'设置为可见,没有任何反应。如果我从第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>
答案 0 :(得分:2)
在onCreate()之后,您的UI才会被布局。尝试将动画移动到onStart()或更好,一旦布局完成,使用ViewTreeObserver启动它们