【发布时间】:2014-01-31 05:12:59
【问题描述】:
我需要在我的图像视图中显示位图。我从服务器获取位图,我想将图像正确地放入我的 imageview 中。我怎样才能做到这一点??我试过了,但显示不正确。
代码:
public class ImageTask extends AsyncTask<String, Integer, Bitmap> {
@Override
protected Bitmap doInBackground(String... urls) {
Bitmap event_Image = null;
String urldisplay = urls[0];
try {
InputStream in = new java.net.URL(urldisplay.toString().trim())
.openStream();
event_Image = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", "Error while getting image " + e.toString());
// e.printStackTrace();
}
return event_Image;
}
public Bitmap getResizedBitmap(Bitmap image, int newHeight, int newWidth) {
int width = image.getWidth();
int height = image.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// create a matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(image, 0, 0, width, height,
matrix, false);
return resizedBitmap;
}
new ImageTask() {
@Override
protected void onPostExecute(Bitmap result) {
if (result != null) {
// Rescaling event details image view
Bitmap resized = this.getResizedBitmap(result,
eventDetails_Image.getWidth(),
eventDetails_Image.getHeight());
eventDetails_Image.setImageBitmap(resized);
}
}
}.execute(mEventDetailsUtil1.getEvent_image_url());
【问题讨论】:
-
只需在图像视图中使用 scaleType = "fitXY"。这就是你想要的吗?
-
我已经试过了。
标签: android bitmap android-imageview