您在創建縮放位圖時需要考慮密度。由於您的ImageView
有設置新的位圖前正確的尺寸,你的情況,你可以只縮放新Bitmap
時使用ImageView
的尺寸...例如:
我假設你正在使用Uri
小號從文件系統中獲取存儲的圖像。如果您使用File
s,只需使用Uri.fromFile(file)
即可獲得Uri
。我通常使用的AsyncTask
做到這一點,因爲你應該做的位圖處理掉主線程......這裏的一些須藤代碼(不是在AsyncTask
爲了簡單,但容易重構):
//Somewhere in your Activity
public void scaleAndSetBitmap(ImageView imageView, Uri uri){
InputStream stream = null;
Bitmap bitmap = null;
try {
stream = getContentResolver().openInputStream(uri);
bitmap = BitmapFactory.decodeStream(stream, null, options);
if (image_view != null && bitmap != null) {
bitmap = Bitmap.createScaledBitmap(bitmap, image_view.getWidth(), image_view.getHeight(), true);
image_view.setImageBitmap(bitmap);
}
} catch(Exception e){
e.printStackTrace();
return;
} finally {
try{
stream.close();
} catch(IOException i){
return;
}
}
}