2016-03-27 22 views
0

我想從圖像視圖中檢索位圖,然後對其進行縮放。但我得到下面的錯誤:Android - 更新位圖當視圖已經測量

「java.lang.IllegalArgumentException異常:寬度和高度必須> 0」

正如我搜索這是因爲ImageView的完成之前將被測量的網絡,並轉換爲位圖,我嘗試更新位圖。任何人都可以幫助我如何解決這個問題。我的代碼如下:

profileImage.buildDrawingCache(); 
Bitmap bm = profileImage.getDrawingCache(); 

int widthPixel = bm.getWidth(); 
int heightPixel = bm.getHeight(); 
int basePixel; 
float widthRatio; 
float heightRatio; 

if (widthPixel < heightPixel) { 
    basePixel = widthPixel; 
} 
else { 
    basePixel = heightPixel; 
} 

if (basePixel > 180) { 
    widthRatio = 180/basePixel; 
    heightRatio = 180/basePixel; 
} 
else { 
    widthRatio = 1; 
    heightRatio = 1; 
} 

Bitmap bmResized = Bitmap.createScaledBitmap(bm,(int)(bm.getWidth()*widthRatio), (int)(bm.getHeight()*heightRatio), true); 
// bm.recycle(); 

ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bmResized.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
byteArray1 = stream.toByteArray(); 

編輯代碼(現在越來越 「java.lang.IllegalArgumentException異常:值不能爲空」 錯誤):

        profileImage.buildDrawingCache(); 
            bm = profileImage.getDrawingCache(); 



            profileImage.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() { 
             public boolean onPreDraw() { 
              profileImage.getViewTreeObserver().removeOnPreDrawListener(this); 

              widthPixel = profileImage.getMeasuredWidth(); 
              heightPixel = profileImage.getMeasuredHeight(); 

              if (widthPixel < heightPixel) { 

               basePixel = widthPixel; 

              } 

              else { 

               basePixel = heightPixel; 

              } 


              if (basePixel > 180) { 

               widthRatio = 180/basePixel; 
               heightRatio = 180/basePixel; 


              } 

              else { 

               widthRatio = 1; 
               heightRatio = 1; 

              } 


              Bitmap bmResized = Bitmap.createScaledBitmap(bm,(int)(bm.getWidth()*widthRatio), (int)(bm.getHeight()*heightRatio), true); 
              // bm.recycle(); 

              ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
              bmResized.compress(Bitmap.CompressFormat.JPEG, 100, stream); 
              byteArray1 = stream.toByteArray(); 

              image1 = new ParseFile("profilePhoto.jpg", byteArray1, "image/jpg"); 

              return true; 
             } 
            }); 

回答

1

使用的OnPreDrawListenerOnPreDrawListener允許您在View的度量操作之後但佈局階段之前檢索View維度。這意味着這是您可以獲取視圖尺寸的最早時間。

view.getViewTreeObserver().adOnPreDrawListener(new OnPreDrawListener() { 
    public boolean onPreDraw() { 
     view.getViewTreeObserver().removeOnPreDrawListener(this); 


     int width = view.getMeasuredWidth(); 
     int height = view.getMeasuredHeight(); 

     // TODO scale your bitmap here 

     return true; 
    } 
}); 
+0

您好萊納,我編輯的代碼與上面的「OnPreDrawListener」,但現在我得到了「java.lang.IllegalArgumentException異常:值不能爲空」錯誤。你可以檢查我的代碼嗎?謝謝。 – saner

+0

每當您的應用程序崩潰時出現異常,請查找提及的行號。這會指向您的代碼中出現錯誤的那一行,以便您可以調試該問題。既然你提到被拋出的錯誤是'IllegalArgumentException',並且消息讀取'value不能爲空',我猜你試圖調整'null'位圖的大小。 – Reinier

+0

@saner您的方法存在一些問題。您試圖將視圖的繪圖緩存渲染到「位圖」中。然而,視圖尚未繪製,因此視圖的繪圖緩存爲空,使view.getDrawingCache()返回null。要麼強制視圖繪製(關於SO的大量例子),要麼採取另一種方法。另一種方法是調用'view.getDrawable()',然後將'Drawable'轉換爲位圖,然後調整大小。 – Reinier