我一直在努力让IF / ELSE声明有效。
我有以下代码:
File fileOnSD=Environment.getExternalStorageDirectory();
String storagePath = fileOnSD.getAbsolutePath();
Bitmap BckGrnd = BitmapFactory.decodeFile(storagePath + "/oranjelanbg.png");
ImageView BackGround = (ImageView)findViewById(R.id.imageView1);
BackGround.setImageBitmap(BckGrnd);
if (){
}else{
TextView text1 = (TextView) findViewById(R.id.textView1);
TextView text2 = (TextView) findViewById(R.id.textView2);
text1.setVisibility(View.VISIBLE);
text2.setVisibility(View.VISIBLE);
}
我正努力实现以下目标。 我的应用程序将图像下载到手机并将其用作背景。 但是当你第一次运行应用程序时,图片还没有下载,所以必须有一些文字。 默认情况下,文本是不可见的,我希望在图像仍在下载但尚未放置时使其可见。
我应该在IF语句中使用什么表达式来检查图像是否已加载?
答案 0 :(得分:3)
if (BckGrnd != null){
BackGround.setImageBitmap(BckGrnd);
}else{
TextView text1 = (TextView) findViewById(R.id.textView1);
TextView text2 = (TextView) findViewById(R.id.textView2);
text1.setVisibility(View.VISIBLE);
text2.setVisibility(View.VISIBLE);
}
更好的解决方案:
使用 AsyncTask 进行下载图片:
AsyncTask<Void, Void, Void> loadingTask = new AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
TextView text1 = (TextView) findViewById(R.id.textView1);
TextView text2 = (TextView) findViewById(R.id.textView2);
text1.setVisibility(View.VISIBLE);
text2.setVisibility(View.VISIBLE);
}
@Override
protected Void doInBackground(Void... params) {
// Download Image Here
}
@Override
protected void onPostExecute(Void result) {
BackGround.setImageBitmap(BckGrnd);
text1.setVisibility(View.GONE);
text2.setVisibility(View.GONE);
}
};
loadingTask.execute();