Android Images are stretched when the phone is rotated -
i getting images using html.fromhtml() method. using urlimageparser , urldrawable classes defined here: android html imagegetter asynctask.
i have adapted 2 methods urlimageparser in order scale image width of screen whilst maintaining aspect ratio. changed methods 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); // alter reference of current drawable result // http phone call urldrawable.drawable = result; // redraw image invalidating container urlimageparser.this.container.invalidate(); // ics urlimageparser.this.container.setheight((urlimageparser.this.container.getheight() + height)); // pre ics urlimageparser.this.container.setellipsize(null); }
and
public drawable fetchdrawable(string urlstring) { seek { inputstream = 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); homecoming drawable; } grab (exception e) { homecoming null; } }
my problem when phone held in portrait orientation image looks fine. when rotate phone width of image goes new width of phone, height not alter image looks distorted.
is there way reset height when phone rotated?
pretty sure silly error somewhere
to observe screen rotation, can seek this:
@override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); // checks orientation of screen if (newconfig.orientation == configuration.orientation_landscape) { toast.maketext(this, "landscape", toast.length_short).show(); } else if (newconfig.orientation == configuration.orientation_portrait){ toast.maketext(this, "portrait", toast.length_short).show(); } }
see more.
here another great example.
android
No comments:
Post a Comment