2011-06-17 54 views
0

我最初顯示一個空的視圖。當我點擊「添加圖像」按鈕時,它會在網格中顯示一組圖像,然後點擊圖像。我通過使用拖動圖層動態添加ImageView來將圖像放置在視圖中。通過這種方式,我添加了更多圖像。現在我想以編程方式調整每個圖像的大小。可能嗎?如果可能,請提供代碼。是否有可能通過點擊按鈕來調整圖像編程?

回答

5

您可以使用下面的代碼來調整圖像大小。如果你知道圖片的路徑和所需的寬度和高度

public static Bitmap resizeBitMapImage1(String filePath, int targetWidth, 
      int targetHeight) { 
     Bitmap bitMapImage = null; 
     // First, get the dimensions of the image 
     Options options = new Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(filePath, options); 
     double sampleSize = 0; 
     // Only scale if we need to 
     // (16384 buffer for img processing) 
     Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math 
       .abs(options.outWidth - targetWidth); 

     if (options.outHeight * options.outWidth * 2 >= 1638) { 
      // Load, scaling to smallest power of 2 that'll get it <= desired 
      // dimensions 
      sampleSize = scaleByHeight ? options.outHeight/targetHeight 
        : options.outWidth/targetWidth; 
      sampleSize = (int) Math.pow(2d, 
        Math.floor(Math.log(sampleSize)/Math.log(2d))); 
     } 

     // Do the actual decoding 
     options.inJustDecodeBounds = false; 
     options.inTempStorage = new byte[128]; 
     while (true) { 
      try { 
       options.inSampleSize = (int) sampleSize; 
       bitMapImage = BitmapFactory.decodeFile(filePath, options); 

       break; 
      } catch (Exception ex) { 
       try { 
        sampleSize = sampleSize * 2; 
       } catch (Exception ex1) { 

       } 
      } 
     } 

     return bitMapImage; 
    } 

感謝 迪帕克

相關問題