Android SplashScreen 在 ImageView 中更改图像 - 为什么 MainActivity 会定期重新加载?
Android SplashScreen changing images in ImageView - Why MainActivity reload periodically?
我是 Android 初学者。我在 ImageView evry 2 秒内制作了一个 SplashScreen 更改 img 图片。最后一张图片连续显示两次,以避免在移动到 MainActivity 之前立即消除它。所以它以某种方式工作 - 图片正在改变,最后它将用户带到 MainActivity,在那里我只有一个按钮什么都不做(只是为了让任何东西在那里可见以供测试)。理论上一切都很好,但每 8 秒 MainActivity 可能会重新加载一次。我可以看到,因为 Button 上下跳跃了 1px。
你能看看我的代码并回答为什么吗?
谢谢!
public class SplashScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash_screen);
handler.postDelayed(runnable, 0);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
int[] imageArraySplashScreen = { R.drawable.pierwszy, R.drawable.drugi, R.drawable.trzeci, R.drawable.trzeci};
Handler handler = new Handler();
Runnable runnable = new Runnable(){
int i = 0;
ImageView splashImageView;
public void run() {
splashImageView = findViewById(R.id.idSplashScreenImageView);
splashImageView.setImageResource(imageArraySplashScreen[i]);
i++;
if (i>imageArraySplashScreen.length-1){
i=0;
Intent splashScreenIntent = new Intent(SplashScreen.this, MainActivity.class );
startActivity(splashScreenIntent);
finish();
}
handler.postDelayed(this, 2000);
}
};
}
尝试
handler.removeCallbacks(runnable);
在 onPause()
您需要 post 处理程序,仅当图像尚待显示时。只需更改您的代码,例如,
Handler handler = new Handler();
Runnable runnable = new Runnable() {
int i = 0;
ImageView splashImageView;
public void run() {
splashImageView.setImageResource(imageArraySplashScreen[i]);
i++;
if (i > imageArraySplashScreen.length - 1) {
i = 0;
Intent splashScreenIntent = new Intent(SplashScreen.this, MainActivity.class);
startActivity(splashScreenIntent);
finish();
} else {
handler.postDelayed(this, 2000);
}
}
};
我是 Android 初学者。我在 ImageView evry 2 秒内制作了一个 SplashScreen 更改 img 图片。最后一张图片连续显示两次,以避免在移动到 MainActivity 之前立即消除它。所以它以某种方式工作 - 图片正在改变,最后它将用户带到 MainActivity,在那里我只有一个按钮什么都不做(只是为了让任何东西在那里可见以供测试)。理论上一切都很好,但每 8 秒 MainActivity 可能会重新加载一次。我可以看到,因为 Button 上下跳跃了 1px。
你能看看我的代码并回答为什么吗? 谢谢!
public class SplashScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash_screen);
handler.postDelayed(runnable, 0);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
int[] imageArraySplashScreen = { R.drawable.pierwszy, R.drawable.drugi, R.drawable.trzeci, R.drawable.trzeci};
Handler handler = new Handler();
Runnable runnable = new Runnable(){
int i = 0;
ImageView splashImageView;
public void run() {
splashImageView = findViewById(R.id.idSplashScreenImageView);
splashImageView.setImageResource(imageArraySplashScreen[i]);
i++;
if (i>imageArraySplashScreen.length-1){
i=0;
Intent splashScreenIntent = new Intent(SplashScreen.this, MainActivity.class );
startActivity(splashScreenIntent);
finish();
}
handler.postDelayed(this, 2000);
}
};
}
尝试
handler.removeCallbacks(runnable);
在 onPause()
您需要 post 处理程序,仅当图像尚待显示时。只需更改您的代码,例如,
Handler handler = new Handler();
Runnable runnable = new Runnable() {
int i = 0;
ImageView splashImageView;
public void run() {
splashImageView.setImageResource(imageArraySplashScreen[i]);
i++;
if (i > imageArraySplashScreen.length - 1) {
i = 0;
Intent splashScreenIntent = new Intent(SplashScreen.this, MainActivity.class);
startActivity(splashScreenIntent);
finish();
} else {
handler.postDelayed(this, 2000);
}
}
};