2011-11-15 3 views
2

​​メソッドを使用してURLからイメージをロードし、Bitmapオブジェクトに格納しています。それから私はそのサイズを縮小する。縮小された画像サイズを取得した後、私は自分のレイアウトのImageViewに表示しています。今私は、画像のサイズが縮小されたかどうかを知りたいです。それをどうやって確認できますか?ビットマップサイズをAndroidで縮小した後に知る方法は?

public class ReduceImageActivity extends Activity { 

    String image_URL = "http://pennapps.com/biblioteka/images/C.jpg"; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     ImageView bmImage = (ImageView) findViewById(R.id.imageView1); 
     BitmapFactory.Options bmOptions; 
     bmOptions = new BitmapFactory.Options(); 
     bmOptions.inSampleSize = 1; 
     Bitmap bm = LoadImage(image_URL, bmOptions); 
     getResizedBitmap(bm, 200, 200); 

     System.out.println("after resizing it"+bm); 


     bmImage.setImageBitmap(getResizedBitmap(bm, 200, 200)); 
    } 

    private Bitmap LoadImage(String URL, BitmapFactory.Options options) { 
     Bitmap bitmap = null; 
     InputStream in = null; 
     try { 
      in = OpenHttpConnection(URL); 
      bitmap = BitmapFactory.decodeStream(in, null, options); 
      in.close(); 
     } catch (IOException e1) { 
     } 
     return bitmap; 
    } 

    private InputStream OpenHttpConnection(String strURL) throws IOException { 
     InputStream inputStream = null; 
     URL url = new URL(strURL); 
     URLConnection conn = url.openConnection(); 

     try { 
      HttpURLConnection httpConn = (HttpURLConnection) conn; 
      httpConn.setRequestMethod("GET"); 
      httpConn.connect(); 

      if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) { 
       inputStream = httpConn.getInputStream(); 
      } 
     } catch (Exception ex) { 
     } 
     return inputStream; 
    } 

    public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { 

     int width = bm.getWidth(); 

     int height = bm.getHeight(); 

     float scaleWidth = ((float) newWidth)/width; 

     float scaleHeight = ((float) newHeight)/height; 

     // create a matrix for the manipulation 

     Matrix matrix = new Matrix(); 

     // resize the bit map 

     matrix.postScale(scaleWidth, scaleHeight); 

     // recreate the new Bitmap 

     Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, 
       matrix, false); 

     System.out.println("in getresizebitmap"+resizedBitmap.toString()); 



     return resizedBitmap; 

    } 

} 

答えて

2

使用getWidthgetHeight @barzosが提案されているよう。そして、元のbmのビットマップを変更するのではなく、新しいものを作り出すので、以下のようにそれを使用していないgetResizedBitmap(bm, 200, 200)に呼び出すことに注意してください:

Bitmap bm = LoadImage(image_URL, bmOptions); 
Bitmap resized = getResizedBitmap(bm, 200, 200); 

System.out.println("after resizing [w: " + resized.getWidth() + ", h: " + resized.getHeight() + "]"); 

bmImage.setImageBitmap(resized); 

また、これは、CPUとメモリリソースを節約getResizedBitmap 1つの不要な呼び出しを排除します。

+0

CPUメモリを使用しないようにうまく動作しますが、上記のようにCPUメモリを使用すると感謝します。 –

2

イメージサイズを縮小した後で、を使用して新しい(縮小した)イメージのサイズを確認できます。 getWidth() getHeight() 使用 - >resizedBitmap.getWidth(), resizedBitmap.getHeight()

関連する問題