android - Can anyone help me reset the zoom of the image in ImageView? -
i made app downloads image url (using universal image loader
library) , display image in touchimageview
. used touch image view implement pinch zoom functionality. followed this tutorial learn how use touchimageview.
the problem whenever zoom in image downloaded universal image loader , loads image touchimageview new image stretched , distorted. problem solved when previous zoomed image restored original size zooming out, , loading new image.
so problem can solved zooming out of loaded image , loading new image. don't know how reset zoom , library not provide function that.
i tried using latest version of touchimageview
(which includes reset image function) newer version not compatible uil
seems tutorial using part of code original library (tough working fine).
does know how reset zoom in touchimageview
?
**strong text**code :
package com.hpubts50.hpubustracker; import android.content.context; import android.graphics.matrix; import android.graphics.pointf; import android.graphics.drawable.drawable; import android.util.attributeset; import android.util.log; import android.view.motionevent; import android.view.scalegesturedetector; import android.view.view; import android.widget.imageview; public class touchimageview extends imageview { matrix matrix ; // can in 1 of these 3 states static final int none = 0; static final int drag = 1; static final int zoom = 2; int mode = none; // remember things zooming pointf last = new pointf(); pointf start = new pointf(); float minscale = 1f; float maxscale = 8f; float[] m; int viewwidth, viewheight; static final int click = 3; float savescale = 1f; protected float origwidth, origheight; int oldmeasuredwidth, oldmeasuredheight; scalegesturedetector mscaledetector; context context; public touchimageview(context context) { super(context); sharedconstructing(context); } public touchimageview(context context, attributeset attrs) { super(context, attrs); sharedconstructing(context); } private void sharedconstructing(context context) { super.setclickable(true); this.context = context; mscaledetector = new scalegesturedetector(context, new scalelistener()); matrix = new matrix(); m = new float[9]; setimagematrix(matrix); setscaletype(scaletype.matrix); setontouchlistener(new ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { mscaledetector.ontouchevent(event); pointf curr = new pointf(event.getx(), event.gety()); switch (event.getaction()) { case motionevent.action_down: last.set(curr); start.set(last); mode = drag; break; case motionevent.action_move: if (mode == drag) { float deltax = curr.x - last.x; float deltay = curr.y - last.y; float fixtransx = getfixdragtrans(deltax, viewwidth, origwidth * savescale); float fixtransy = getfixdragtrans(deltay, viewheight, origheight * savescale); matrix.posttranslate(fixtransx, fixtransy); fixtrans(); last.set(curr.x, curr.y); } break; case motionevent.action_up: mode = none; int xdiff = (int) math.abs(curr.x - start.x); int ydiff = (int) math.abs(curr.y - start.y); if (xdiff < click && ydiff < click) performclick(); break; case motionevent.action_pointer_up: mode = none; break; } setimagematrix(matrix); invalidate(); return true; // indicate event handled } }); } public void setmaxzoom(float x) { maxscale = x; } private class scalelistener extends scalegesturedetector.simpleonscalegesturelistener { @override public boolean onscalebegin(scalegesturedetector detector) { mode = zoom; return true; } @override public boolean onscale(scalegesturedetector detector) { float mscalefactor = detector.getscalefactor(); float origscale = savescale; savescale *= mscalefactor; if (savescale > maxscale) { savescale = maxscale; mscalefactor = maxscale / origscale; } else if (savescale < minscale) { savescale = minscale; mscalefactor = minscale / origscale; } if (origwidth * savescale <= viewwidth || origheight * savescale <= viewheight) matrix.postscale(mscalefactor, mscalefactor, viewwidth / 2, viewheight / 2); else matrix.postscale(mscalefactor, mscalefactor, detector.getfocusx(), detector.getfocusy()); fixtrans(); return true; } } void fixtrans() { matrix.getvalues(m); float transx = m[matrix.mtrans_x]; float transy = m[matrix.mtrans_y]; float fixtransx = getfixtrans(transx, viewwidth, origwidth * savescale); float fixtransy = getfixtrans(transy, viewheight, origheight * savescale); if (fixtransx != 0 || fixtransy != 0) matrix.posttranslate(fixtransx, fixtransy); } float getfixtrans(float trans, float viewsize, float contentsize) { float mintrans, maxtrans; if (contentsize <= viewsize) { mintrans = 0; maxtrans = viewsize - contentsize; } else { mintrans = viewsize - contentsize; maxtrans = 0; } if (trans < mintrans) return -trans + mintrans; if (trans > maxtrans) return -trans + maxtrans; return 0; } float getfixdragtrans(float delta, float viewsize, float contentsize) { if (contentsize <= viewsize) { return 0; } return delta; } @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { super.onmeasure(widthmeasurespec, heightmeasurespec); viewwidth = measurespec.getsize(widthmeasurespec); viewheight = measurespec.getsize(heightmeasurespec); // // rescales image on rotation // if (oldmeasuredheight == viewwidth && oldmeasuredheight == viewheight || viewwidth == 0 || viewheight == 0) return; oldmeasuredheight = viewheight; oldmeasuredwidth = viewwidth; if (savescale == 1) { // fit screen. float scale; drawable drawable = getdrawable(); if (drawable == null || drawable.getintrinsicwidth() == 0 || drawable.getintrinsicheight() == 0) return; int bmwidth = drawable.getintrinsicwidth(); int bmheight = drawable.getintrinsicheight(); log.d("bmsize", "bmwidth: " + bmwidth + " bmheight : " + bmheight); float scalex = (float) viewwidth / (float) bmwidth; float scaley = (float) viewheight / (float) bmheight; scale = math.min(scalex, scaley); matrix.setscale(scale, scale); // center image float redundantyspace = (float) viewheight - (scale * (float) bmheight); float redundantxspace = (float) viewwidth - (scale * (float) bmwidth); redundantyspace /= (float) 2; redundantxspace /= (float) 2; matrix.posttranslate(redundantxspace, redundantyspace); origwidth = viewwidth - 2 * redundantxspace; origheight = viewheight - 2 * redundantyspace; setimagematrix(matrix); } fixtrans(); } }
photoactivity.java
package com.hpubts50.hpubustracker; import android.graphics.bitmap; import android.graphics.matrix; import android.graphics.typeface; import android.os.bundle; import android.support.v7.app.actionbaractivity; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.imageview; import android.widget.progressbar; import android.widget.textview; import android.widget.toast; import com.nostra13.universalimageloader.core.displayimageoptions; import com.nostra13.universalimageloader.core.imageloader; import com.nostra13.universalimageloader.core.imageloaderconfiguration; import com.nostra13.universalimageloader.core.assist.disccacheutil; import com.nostra13.universalimageloader.core.assist.failreason; import com.nostra13.universalimageloader.core.assist.imageloadinglistener; import com.nostra13.universalimageloader.core.assist.imageloadingprogresslistener; public class photoactivity extends actionbaractivity implements onclicklistener { private button btn_insideview, btn_outsideview, btn_resetzoom; private imageview img_busicon; private touchimageview img_pictureviewer; private textview tv_busname, tv_busno, tv_camerastatus; private string busid = null; private string busname = null; private imageloaderconfiguration config; private imageloader imageloader; private displayimageoptions options; private string url; private static string server_path = "http://hpubts.hpuiitshimla.org/images/"; private progressbar pb_loading; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_photo); // initialize views initializeviews(); // busid bundle bundle gotphotobundle = getintent().getextras(); busid = gotphotobundle.getstring("busid"); busname = gotphotobundle.getstring("busname"); // setting bus information if (busname.tolowercase().equals("airavat")) { img_busicon.setimageresource(r.drawable.airavat); } else if (busname.tolowercase().equals("alaknanda")) { img_busicon.setimageresource(r.drawable.alaknanda); } else if (busname.tolowercase().equals("chaitanya")) { img_busicon.setimageresource(r.drawable.chaitanya); } else if (busname.tolowercase().equals("garud")) { img_busicon.setimageresource(r.drawable.garud); } else if (busname.tolowercase().equals("nandi")) { img_busicon.setimageresource(r.drawable.nandi); } else if (busname.tolowercase().equals("neela")) { img_busicon.setimageresource(r.drawable.neela); } else if (busname.tolowercase().equals("pushpak")) { img_busicon.setimageresource(r.drawable.pushpak); } tv_busname.settext(busname); tv_busno.settext(busid); tv_camerastatus.settext("back cam"); // photo loading initializing config = new imageloaderconfiguration.builder(getapplicationcontext()).build(); imageloader.getinstance().init(config); imageloader = imageloader.getinstance(); options = new displayimageoptions.builder().resetviewbeforeloading(true).cacheondisc(true).showimageonfail(r.drawable.ic_action_warning).build(); // start photo download pb_loading.setprogress(0); pb_loading.setvisibility(view.visible); startphotodownload(img_pictureviewer, server_path + busid + "_back.jpg"); } private void initializeviews() { btn_insideview = (button) findviewbyid(r.id.btn_insideview); btn_outsideview = (button) findviewbyid(r.id.btn_outsideview); btn_resetzoom = (button) findviewbyid(r.id.btn_resetzoom); tv_busname = (textview) findviewbyid(r.id.txt_busname); tv_busno = (textview) findviewbyid(r.id.txt_busno); tv_camerastatus = (textview) findviewbyid(r.id.txt_camera_status); img_pictureviewer = (touchimageview) findviewbyid(r.id.img_photoviewer); img_busicon = (imageview) findviewbyid(r.id.img_busicon); pb_loading = (progressbar) findviewbyid(r.id.pb_loading); btn_insideview.setonclicklistener(this); btn_outsideview.setonclicklistener(this); btn_resetzoom.setonclicklistener(this); // set font type settypeface(btn_insideview); settypeface(btn_outsideview); settypeface(btn_resetzoom); settypeface(tv_busname); settypeface(tv_busno); settypeface(tv_camerastatus); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.photo, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } @override public void onclick(view v) { switch (v.getid()) { case r.id.btn_insideview: url = server_path + busid + "_front.jpg"; tv_camerastatus.settext("front cam"); break; case r.id.btn_outsideview: url = server_path + busid + "_back.jpg"; tv_camerastatus.settext("back cam"); break; case r.id.btn_resetzoom: break; } // img_pictureviewer pb_loading.setprogress(0); pb_loading.setvisibility(view.visible); startphotodownload(img_pictureviewer, url); } private void startphotodownload(imageview img_viewer, string completeurl) { imageloader.displayimage(completeurl, img_viewer, options, new imageloadinglistener() { @override public void onloadingstarted(string imageuri, view view) { disccacheutil.removefromcache(imageuri, imageloader.getinstance().getdisccache()); } @override public void onloadingfailed(string imageuri, view view, failreason failreason) { pb_loading.setvisibility(view.invisible); toast.maketext(getapplicationcontext(), "image loading failed", toast.length_short).show(); } @override public void onloadingcomplete(string imageuri, view view, bitmap loadedimage) { pb_loading.setvisibility(view.invisible); } @override public void onloadingcancelled(string imageuri, view view) { } }, new imageloadingprogresslistener() { @override public void onprogressupdate(string imageuri, view view, int current, int total) { pb_loading.setmax(total); pb_loading.setprogress(current); } }); } // method set font type compact private void settypeface(textview textview) { typeface custom_font = typeface.createfromasset(getassets(), "fonts/compact.ttf"); textview.settypeface(custom_font); } private void settypeface(button button) { typeface custom_font = typeface.createfromasset(getassets(), "fonts/compact.ttf"); button.settypeface(custom_font); } }
add method called resetzoom()
, this:
public void resetzoom() { matrix = new matrix(); setimagematrix(matrix); }
Comments
Post a Comment