奇怪的事情發生 - 至少我不明白。Android - 縮放圖像後,我編程
我有一個圖像(w:120px,h:63px),它出現在一個ImageButton上。我的圖像的唯一變體放置在drawable-hdpi中(並且沒有其他可繪製目錄)。 一切都可以與這個圖像和每個分辨率(密度),Android很好地照顧它。
但是,麻煩的是當我從一張專輯拍攝一張照片(例如一些非常大的照片)時,將其縮放到一個按鈕的尺寸(前面提到,w:120px h:63px),並將該小圖像設置爲ImageButton背景。
如果我在中等密度(160)的仿真器上測試,那麼可以,但是當在高密度按鈕設備上進行測試時,由於縮放後的圖像顯得更小,所以調整大小。
有沒有人有一個想法發生了什麼,爲什麼縮放後圖像的大小再次改變?
任何線索將不勝感激。
這裏是我使用的調整圖像的代碼:
public static void resizeAndSaveImage(String pathToResize, int newWidth, int newHeight, FileOutputStream output) {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(pathToResize), null, options);
if (bitmap != null) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
float scaleWidth = ((float) newWidth)/width;
float scaleHeight = ((float) newHeight)/height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
resizedBitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
output.close();
}
} catch (Exception e) {
// ...
}
尺寸參數在如下決議:newWidth = 120,newHeight = 63
將api設置爲1.5,這將停止android調整大小,但意味着,你必須這樣做。 – 2011-02-26 11:50:24