android - Applying Zoom on Imageview and Sliding Imges -
public class showimgsactivity extends activity implements ontouchlistener{ int imagesarray[]={r.drawable.a1,r.drawable.a2,r.drawable.a3}; private imageview img; private list<string> sd; float x1,x2; float y1, y2; private string[] files; private assetmanager assetmanager; private int possition; static int forwarint=1; private static final string tag = "touch"; @suppresswarnings("unused") private static final float min_zoom = 1f,max_zoom = 1f; // these matrices used scale points of image matrix matrix = new matrix(); matrix savedmatrix = new matrix(); // 3 states (events) user trying perform static final int none = 0; static final int drag = 1; static final int zoom = 2; int mode = none; // these pointf objects used record point(s) user touching pointf start = new pointf(); pointf mid = new pointf(); float olddist = 1f; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.showimgsscreen); init(); } private void init() { img=(imageview) findviewbyid(r.id.img_prev); assetmanager =showimgsactivity.this.getassets(); possition=getintent().getintextra("main_position",0); try { files = assetmanager.list("deaf/0"+possition); getimage(getapplicationcontext(), "deaf/0"+possition, 0); } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } forwarint=0; } private void getimage(context conetx,string foldername,int pos) throws ioexception { if(pos<=files.length-1 && pos>=0) { bitmap bmp; bmp = bitmapfactory.decodestream(assetmanager.open(foldername+"/" + files[pos])); img.setimagebitmap(bmp); img.setontouchlistener(this); }else if (pos>files.length-1) { forwarint=files.length-1; toast.maketext(getapplicationcontext(), "move backward", toast.length_short).show(); } else if (pos<0) { forwarint=0; toast.maketext(getapplicationcontext(), "move forward", toast.length_short).show(); } else{ log.e("_____________", "less greater"); } } @override public boolean ontouchevent(motionevent touchevent) { // touch=true; switch (touchevent.getaction()) { // when user first touches screen x , y coordinate case motionevent.action_down: { x1 = touchevent.getx(); y1 = touchevent.gety(); break; } case motionevent.action_up: { x2 = touchevent.getx(); y2 = touchevent.gety(); //if left right sweep event on screen if (x1 < x2) { forwarint++; toast.maketext(this, "left right swap performed", toast.length_short).show(); try { getimage(getapplicationcontext(), "deaf/0"+possition, forwarint); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } // if right left sweep event on screen if (x1 > x2) { forwarint--; toast.maketext(this, "right left swap performed", toast.length_short).show(); try { getimage(getapplicationcontext(), "deaf/0"+possition, forwarint); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } // if down sweep event on screen if (y1 < y2) { // toast.maketext(this, "up down swap performed", toast.length_long).show(); } //if down sweep event on screen if (y1 > y2) { // toast.maketext(this, "down swap performed", toast.length_long).show(); } break; } } return false; } @override public boolean ontouch(view v, motionevent event) { imageview view = (imageview) v; view.setscaletype(imageview.scaletype.matrix); float scale; // dumpevent(event); // handle touch events here... switch (event.getaction() & motionevent.action_mask) { case motionevent.action_down: // first finger down savedmatrix.set(matrix); start.set(event.getx(), event.gety()); log.d(tag, "mode=drag"); // write logcat mode = drag; break; case motionevent.action_up: // first finger lifted case motionevent.action_pointer_up: // second finger lifted mode = none; log.d(tag, "mode=none"); break; case motionevent.action_pointer_down: // first , second finger down olddist = spacing(event); log.d(tag, "olddist=" + olddist); if (olddist > 5f) { savedmatrix.set(matrix); midpoint(mid, event); mode = zoom; log.d(tag, "mode=zoom"); } break; case motionevent.action_move: if (mode == drag) { matrix.set(savedmatrix); matrix.posttranslate(event.getx() - start.x, event.gety() - start.y); // create transformation in matrix of points } else if (mode == zoom) { // pinch zooming float newdist = spacing(event); log.d(tag, "newdist=" + newdist); if (newdist > 5f) { matrix.set(savedmatrix); scale = newdist / olddist; // setting scaling of // matrix...if scale > 1 means // zoom in...if scale < 1 means // zoom out matrix.postscale(scale, scale, mid.x, mid.y); } } break; } view.setimagematrix(matrix); // display transformation on screen return true; // indicate event handled } /* * -------------------------------------------------------------------------- * method: spacing parameters: motionevent returns: float description: * checks spacing between 2 fingers on touch * ---------------------------------------------------- */ private float spacing(motionevent event) { float x = event.getx(0) - event.getx(1); float y = event.gety(0) - event.gety(1); return floatmath.sqrt(x * x + y * y); } /* * -------------------------------------------------------------------------- * method: midpoint parameters: pointf object, motionevent returns: void * description: calculates midpoint between 2 fingers * ------------------------------------------------------------ */ private void midpoint(pointf point, motionevent event) { float x = event.getx(0) + event.getx(1); float y = event.gety(0) + event.gety(1); point.set(x / 2, y / 2); } /** show event in logcat view, debugging */ private void dumpevent(motionevent event) { string names[] = { "down", "up", "move", "cancel", "outside","pointer_down", "pointer_up", "7?", "8?", "9?" }; stringbuilder sb = new stringbuilder(); int action = event.getaction(); int actioncode = action & motionevent.action_mask; sb.append("event action_").append(names[actioncode]); if (actioncode == motionevent.action_pointer_down || actioncode == motionevent.action_pointer_up) { sb.append("(pid ").append(action >> motionevent.action_pointer_id_shift); sb.append(")"); } sb.append("["); (int = 0; < event.getpointercount(); i++) { sb.append("#").append(i); sb.append("(pid ").append(event.getpointerid(i)); sb.append(")=").append((int) event.getx(i)); sb.append(",").append((int) event.gety(i)); if (i + 1 < event.getpointercount()) sb.append(";"); } sb.append("]"); log.d("touch events ---------", sb.tostring()); } }
i have images available in assets folder . have applied sliding on images want apply zooming on each image. when apply zooming functionality sliding functionality can't work.
Comments
Post a Comment