2013-08-16 55 views
0

我已經使用了開發人員指南中的Loading Bitmaps Efficiently,並遵循了該教程中的指示。有效地加載位圖 - 在開始時顯示純色

正如你可能知道,這是我使用的代碼:

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth,int reqHeight){ 
    //Raw height and width of the Image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if (height>reqHeight || width>reqWidth) { 
     final int heightratio = Math.round((float)height/(float)reqHeight); 
     final int widthRatio = Math.round((float)width/(float)reqWidth); 

     inSampleSize = heightratio < widthRatio ? heightratio : widthRatio; 
    } 
    return inSampleSize; 
} 
public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,int reqWidth,int reqHeight){ 
    //first decode with inJustdecodeBounds = true to check dimensions. 

    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeResource(res, resId, options); 
    //Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    //Decode bitmap with inSampleSize 
    options.inJustDecodeBounds = false; 

    return BitmapFactory.decodeResource(res, resId, options); 
} 

,我在一個開關情況下調用此方法:

handler.postDelayed(new Runnable() { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      learn_full_iv.setVisibility(View.VISIBLE); 
      learn_full_iv.startAnimation(anim); 

      switch (id) { 
      case R.id.a: 
       //learn_full_iv.setImageResource(R.drawable.aeroplane); 
       learn_full_iv.setImageBitmap(decodeSampledBitmapFromResource(
         getResources(), R.drawable.aeroplane, learn_full_iv.getWidth(), learn_full_iv.getWidth())); 
       Toast.makeText(getApplicationContext(), "Width : " + learn_full_iv.getWidth() + "Height : " + learn_full_iv.getHeight(), Toast.LENGTH_SHORT).show(); 
       playSound(1, 2); 
       break; 
      case R.id.b: 
       //learn_full_iv.setImageResource(R.drawable.ball); 
       learn_full_iv.setImageBitmap(decodeSampledBitmapFromResource(
         getResources(), R.drawable.ball, learn_full_iv.getWidth(), learn_full_iv.getWidth())); 
       Toast.makeText(getApplicationContext(), "Width : " + learn_full_iv.getWidth() + "Height : " + learn_full_iv.getHeight(), Toast.LENGTH_SHORT).show(); 
       playSound(3, 4); 
       break; 
      case R.id.c: 
       //learn_full_iv.setImageResource(R.drawable.camel); 
       learn_full_iv.setImageBitmap(decodeSampledBitmapFromResource(
         getResources(), R.drawable.camel, learn_full_iv.getWidth(), learn_full_iv.getWidth())); 
       Toast.makeText(getApplicationContext(), "Width : " + learn_full_iv.getWidth() + "Height : " + learn_full_iv.getHeight(), Toast.LENGTH_SHORT).show(); 
       playSound(5, 6); 
       break; 
      case R.id.d: 
       //learn_full_iv.setImageResource(R.drawable.drum); 
       learn_full_iv.setImageBitmap(decodeSampledBitmapFromResource(
         getResources(), R.drawable.drum, learn_full_iv.getWidth(), learn_full_iv.getWidth())); 
       Toast.makeText(getApplicationContext(), "Width : " + learn_full_iv.getWidth() + "Height : " + learn_full_iv.getHeight(), Toast.LENGTH_SHORT).show(); 
       playSound(7, 8); 
       break; 
        default: 
        break;  
} 

而現在的問題是,每次上加載onCreate(),首先點擊的圖像將只顯示圖像的純色,而不是整個圖像,如下所示:

Image View Without Width & Height

而且從下一個點擊,無論是在同一圖像或下一張圖片上,代碼工作正常,如:直到活動重新啓動ImageView with desired width & Height!

和其他一切工作的罰款。如果我們重新啓動或恢復此活動,同樣的事情正在發生。

那麼,出了什麼問題?

回答

2

我只是解決了它&想通了我自己,其實它是一個小的外觀上的圖像的機器人裝載:

@Override 
    public void run() { 
     // TODO Auto-generated method stub 
     learn_full_iv.setVisibility(View.VISIBLE); 
     learn_full_iv.startAnimation(anim); 

     switch (id) { 
     case R.id.a: 
      //learn_full_iv.setImageResource(R.drawable.aeroplane); 
      learn_full_iv.setImageBitmap(decodeSampledBitmapFromResource(
        getResources(), R.drawable.aeroplane, learn_full_iv.getWidth(), learn_full_iv.getWidth())); 
      Toast.makeText(getApplicationContext(), "Width : " + learn_full_iv.getWidth() + "Height : " + learn_full_iv.getHeight(), Toast.LENGTH_SHORT).show(); 
      playSound(1, 2); 
      break; 
    ........... 

在上面的代碼的問題是,我只是使ImageView的「learn_full_iv」只是在運行方法的開始,這將是具有其寬度&高度默認爲「0」的形象還是不能設置在ImageView的

所以如果我們呼籲:

learn_full_iv.setImageBitmap(decodeSampledBitmapFromResource(
       getResources(), R.drawable.aeroplane, learn_full_iv.getWidth(), learn_full_iv.getWidth())); 

寬度&高度將是零,在這種情況下,只需一個因此,我使用方法來獲得寬度&屏幕的高度,因爲整數值&將它們傳遞給上述BITMAP DECODE方法中的相應寬度&高度部分。

多數民衆贊成它,因爲現在我們有寬度&高度作爲值,位圖將顯示現在。

簡單!

+0

我也陷在那個坑裏。謝謝你幫助我走出困境:) –

0

請澄清:

  1. 你爲什麼調用運行的開關鱈魚?

  2. 你在哪裏調用處理程序來運行?

確保不調用的onResume/OnStart方法處理程序啓動方法也確保OnCreate中不再調用定向變化的情況下

相關問題