I am getting images using the Html.fromHtml() method. So I am using URLImageParser and URLDrawable classes as defined here: Android HTML ImageGetter as AsyncTask.
I have adapted two methods from URLImageParser in order to scale the image to the width of the screen whilst maintaining the aspect ratio. The changed methods are as follows:
@Override
protected void onPostExecute(Drawable result) {
DisplayMetrics dm = new DisplayMetrics();
((WindowManager) c.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = width * container.getHeight() / container.getWidth();
urlDrawable.setBounds(0, 0, 0+width, 0+height);
// change the reference of the current drawable to the result
// from the HTTP call
urlDrawable.drawable = result;
// redraw the image by invalidating the container
URLImageParser.this.container.invalidate();
// For ICS
URLImageParser.this.container.setHeight((URLImageParser.this.container.getHeight()
+ height));
// Pre ICS
URLImageParser.this.container.setEllipsize(null);
}
and
public Drawable fetchDrawable(String urlString) {
try {
InputStream is = fetch(urlString);
Drawable drawable = Drawable.createFromStream(is, "src");
DisplayMetrics dm = new DisplayMetrics();
((WindowManager) c.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = width * container.getHeight() / container.getWidth();
drawable.setBounds(0, 0, 0 + width, 0
+ height);
return drawable;
} catch (Exception e) {
return null;
}
}
My problem is that when the phone is held in portrait orientation the image looks fine. However when I rotate the phone the width of the image goes to the new width of the phone, however the height does not change so the image looks distorted.
Is there a way to reset the height when the phone is rotated?
Pretty sure this is a silly mistake somewhere