2016-03-27 7 views
0

イメージビューからビットマップを取得して、それを拡大しようとしています。私はImageViewのが終了する前にそれを測定するため、インターネットを検索し、ビットマップに変換したようAndroid - ビューが測定されたときにビットマップを更新する

:「> 0でなければならない幅と高さjava.lang.IllegalArgumentExceptionが」

:しかし、私は以下のエラーを取得しています、私はビットマップを更新しようとします。誰も私がどのようにこれを修正することができますabouy助けることができます。私のコードは以下の通りです:

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が。:値がnullではないかもしれない" 取得エラー):

        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

OnPreDrawListenerを使用します。 OnPreDrawListenerでは、ビューのメジャー操作の後、レイアウトフェーズの前にビューディメンションを取得できます。これは、これがビューの次元を取得できる最も早い時期であることを意味します。

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がを:値はnullではないかもしれません。」エラー。上のコードをチェックしてもよろしいですか?ありがとうございました。 – saner

+0

アプリケーションがExceptionでクラッシュした場合は、その行番号を参照してください。これにより、エラーが発生しているコード内の行が指摘され、問題をデバッグすることができます。スローされるエラーは 'IllegalArgumentException'であり、' valueはnullではない可能性があります 'というメッセージが表示されるので、 'null'ビットマップのサイズを変更しようとしていると思います。 – Reinier

+0

@sanerあなたのアプローチにはいくつかの問題があります。ビューの描画キャッシュを 'ビットマップ 'にレンダリングしようとしています。しかし、Viewはまだ描画されていないので、Viewの描画キャッシュは空であり、 'view.getDrawingCache()'は 'null'を返すようになっています。ビューを強制的に描画するか(SOの例がたくさんあります)、別の方法をとってください。一つの方法は、 'view.getDrawable()'を呼び出してから、 'Drawable'をビットマップに変換してから、サイズを変更することです。 – Reinier

関連する問題