我是安卓初学者。我每 2 秒就在 ImageView 中制作了一个 SplashScreen 更改 img 图片。最后一个图像连续显示两次,以避免在移动到 MainActivity 之前立即消失。所以它以某种方式工作 - 图片正在改变,最后它把用户带到 MainActivity 那里我只有一个什么都不做的按钮(只是为了测试在那里可见)。理论上一切都很好,但每 8 秒 MainActivity 可能会再次重新加载。我可以看到因为 Button 上下跳动 1px。你能看看我的代码并回答为什么吗?谢谢你!public class SplashScreen extends Activity {@Overrideprotected 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);}@Overrideprotected 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); }};}
2 回答
墨色风雨
TA贡献1853条经验 获得超6个赞
仅当图像要显示时才需要发布处理程序。只需更改您的代码,例如,
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);
}
}
};
添加回答
举报
0/150
提交
取消