2011-11-25 53 views
2

我喜歡調整位圖的大小,如果它很大,使它變小並將其放置在表面視圖中的特定位置,則需要獲取設備寬度和高度,然後獲取位圖大小並將它們放置在表面視圖,然後採取另一個圖像調整大小,並將其放置在任何我喜歡的位置。 如何首先知道位置座標(放置第二個圖像 - 如果更大/更小的屏幕布局不應該改變,則應該與設備無關)Android Image Resize basic

以及如何縮放大位圖並設置爲背景,以便我可以繪製圖像在它上面。 我的代碼:

final int canvasWidth = getWidth(); 
final int canvasHeight = getHeight(); 

int imageWidth = img.getWidth(); 
int imageHeight = img.getHeight(); 

float scaleFactor = Math.min((float)canvasWidth/imageWidth, 
           (float)canvasHeight/imageHeight); 
Bitmap scaled = Bitmap.createScaledBitmap( img, 
              (int)(scaleFactor * imageWidth), 
              (int)(scaleFactor * imageHeight), 
              true); 
canvas.drawColor(Color.BLACK); 
canvas.drawBitmap(scaled, 10, 10, null); 

這種規模的大圖像,但它不適合整個屏幕「IMG - 位圖就像是一個背景圖像」

有人能幫助我瞭解調整大小的基礎知識(我是新的,因此難以理解調整大小)來調整圖像的大小以適應屏幕,並將任何圖像調整爲較小的圖像並將其放置在我喜歡的任何位置。

回答

1

Store中的位圖作爲操作的來源和使用ImageView的顯示出來:

Bitmap realImage = BitmapFactory.decodeFile(filePathFromActivity.toString()); 

Bitmap newBitmap = scaleDown(realImage, MAX_IMAGE_SIZE, true); 


imageView.setImageBitmap(newBitmap); 


//scale down method 
public static Bitmap scaleDown(Bitmap realImage, float maxImageSize, 
     boolean filter) { 
    float ratio = Math.min(
      (float) maxImageSize/realImage.getWidth(), 
      (float) maxImageSize/realImage.getHeight()); 
    int width = Math.round((float) ratio * realImage.getWidth()); 
    int height = Math.round((float) ratio * realImage.getHeight()); 

    Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width, 
      height, filter); 
    return newBitmap; 
} 

,並設置您的ImageView的寬度和高度,以「fill_parent」。

+0

Thx的幫助,但我得到了ForceQuit消息,因爲我試圖在表面視圖中繪製圖像,我已經有幾張圖像 – optimus