我是Android初学者。我已经在SplashScreen内2秒钟内更改了ImageView内的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);
}
};
}
答案 0 :(得分:-1)
尝试
handler.removeCallbacks(runnable);
在onPause()
答案 1 :(得分:-1)
仅当需要显示图像时才需要发布处理程序。只需更改代码即可,
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);
}
}
};