0

我在让 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 语句中使用什么表达式来检查图像是否已加载?

4

1 回答 1

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);
    } 

更好的解决方案:

使用AyncTask下载图像:

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();   
于 2012-07-12T11:04:50.090 回答