2011-11-30 80 views

回答

2

在代碼中(而不是通過佈局XML),您可以通過在縮放位圖後裁剪圖像以創建足夠大的剪裁來創建右寬高比(設備高x寬)的位圖。您需要確保圖像可以放大/縮小(最好是放下)而不會損失清晰度。您還需要確保在以不同寬高比裁剪圖像時重要信息不會丟失。

一旦你得到了位圖,然後將它作爲ImageView的內容放置在顯示器上。

我發現最好將底層圖像與圖層視圖分開放置,以使文本保持清晰。

我創建了ImageView類的一個子類來封裝調整大小和裁剪。唯一值的方法是重寫onMeasure()方法:

/** 
* Override the onMeasure method to resize the Bitmap as needed 
*/ 
@Override 
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
    super.onMeasure(widthMeasureSpec, heightMeasureSpec); 


    Drawable currentDrawable = this.getDrawable(); 
    BitmapDrawable theBitmapDrawable; 
    if (BitmapDrawable.class.isInstance(currentDrawable)){ 
     // We have a bitmap to work with 
     theBitmapDrawable = (BitmapDrawable) currentDrawable; 
     Bitmap currentBitmap = theBitmapDrawable.getBitmap(); 
     Bitmap resizedBitmap = null; 

     if (currentBitmap != null) { 
      int currentHeight = currentBitmap.getHeight(); 
      int currentWidth = currentBitmap.getWidth(); 
      int parentWidth = MeasureSpec.getSize(widthMeasureSpec); 
      int parentHeight = MeasureSpec.getSize(heightMeasureSpec); 
      if ((currentHeight != parentHeight) || (currentWidth != parentWidth)) { 
       // The bitmap needs to be resized, and/or cropped to fit 
       if ((currentHeight < parentHeight) || (currentWidth < parentWidth)) { 
        // Need to make the bitmap larger 
        float heightFactor = (float) parentHeight/(float) currentHeight; 
        float widthFactor = (float) parentWidth/(float) currentWidth; 
        float scaleFactor; 
        // Choose the largest factor 
        if (Float.compare(heightFactor, widthFactor) < 0) { 
         scaleFactor = widthFactor; 
        } else { 
         scaleFactor = heightFactor; 
        } 
        int dstWidth = (int) (currentWidth * scaleFactor); 
        int dstHeight = (int) (currentHeight * scaleFactor); 
        if (dstWidth < parentWidth) dstWidth = parentWidth;  // Deal with off by one rounding errors 
        if (dstHeight < parentHeight) dstHeight = parentHeight; // Deal with off by one rounding errors 
        resizedBitmap = Bitmap.createScaledBitmap(currentBitmap, dstWidth, dstHeight, true); 
        currentBitmap.recycle(); 
       } else if ((currentHeight > parentHeight) && (currentWidth > parentWidth)){ 
        // Need to make the splash screen bitmap smaller 
        float heightFactor = (float) parentHeight/(float) currentHeight; 
        float widthFactor = (float) parentWidth/(float) currentWidth; 
        float scaleFactor; 
        // Choose the largest factor 
        if (Float.compare(heightFactor, widthFactor) < 0) { 
         scaleFactor = widthFactor; 
        } else { 
         scaleFactor = heightFactor; 
        } 
        int dstWidth = (int) (currentWidth * scaleFactor); 
        int dstHeight = (int) (currentHeight * scaleFactor); 
        if (dstWidth < parentWidth) dstWidth = parentWidth;  // Deal with off by one rounding errors 
        if (dstHeight < parentHeight) dstHeight = parentHeight; // Deal with off by one rounding errors 
        resizedBitmap = Bitmap.createScaledBitmap(currentBitmap, dstWidth, dstHeight, true); 
        currentBitmap.recycle(); 
       } else { 
        // No need to resize the image - we'll just need to crop it 
        resizedBitmap = currentBitmap; 
       } 

       // Now crop the image so that it fits the aspect ratio of the screen 
       currentHeight = resizedBitmap.getHeight(); 
       currentWidth = resizedBitmap.getWidth(); 
       Bitmap newBitmap; 
       if ((currentHeight != parentHeight) || (currentWidth != parentWidth)) { 
        // Crop the image to fit exactly 
        int startX = (currentWidth - parentWidth)/2; 
        if (startX < 0) startX = 0; // Hmm! 
        int startY = (currentHeight - parentHeight)/2; 
        if (startY < 0) startY = 0; // Hmm! again 
        newBitmap = Bitmap.createBitmap(resizedBitmap, startX, startY, parentWidth, parentHeight); 
        resizedBitmap.recycle(); 
       } else { 
        // The resized image is the exact right size 
        newBitmap = resizedBitmap; 
       } 

       this.setImageBitmap(newBitmap); 
      } 
     } 
    } 

} 
+0

謝謝,但我不能Override onMeasure方法。不知道爲什麼.. – user420574

+0

@ user420574 - 您需要擴展ImageView類,如'public class WidgetSelfSizingImageView extends ImageView {' – Colin