2012-07-12 187 views
0

我一直有一些麻烦,以获得IF/ELSE声明工作。如果/其他语句麻烦(android)

我有以下代码:

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

回答

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(); 
+0

感谢您的第一awnser工作就像一个魅力,整个下载的东西已经处于的AsyncTask使第一个awnser会做得很好!谢谢! – 2012-07-12 11:16:29

+0

如果它解决了你的问题,那么你可以接受作为正确的答案,并upvote它。 – user370305 2012-07-12 11:17:29

+0

还没有足够的代表:D – 2012-07-12 11:19:05