android - Why does image goes to its original size? -
when galleryview first displays image shows scaled max possible size fit screen(as want) . after tap creen image goes original size. how can keep image scaled?
class imageadapterr extends baseadapter { /** parent context */ private context mycontext; /** simple constructor saving 'parent' context. */ public imageadapterr(context c) { this.mycontext = c; } matrix savedmatrix = new matrix(); /** returns amount of images have defined. */ public int getcount() { return uilapplication.photo_buffer_big.size(); } /* use array-positions unique ids */ public object getitem(int position) { return position; } public long getitemid(int position) { return position; } /** * returns new imageview displayed, depending on position * passed. */ public view getview(final int position, view convertview, viewgroup parent) { imview = new imageview(this.mycontext); current = imview; imgpos = position; imageloader imageloader = new imageloader(app.getapplicationcontext()); imageloader.displayimage(uilapplication.photo_buffer_big.get(imgpos), imview); /* image should scaled width/height set. */ imview.setscaletype(imageview.scaletype.fit_center); /* set width/height of imageview. */ imview.setlayoutparams(new gallery.layoutparams( layoutparams.fill_parent, layoutparams.fill_parent)); imview.setontouchlistener(new ontouchlistener() { private static final string tag = "touch"; // these matrices used move , zoom image pointf start = new pointf(); public pointf mid = new pointf(); // can in 1 of these 3 states public static final int none = 0; public static final int drag = 1; public static final int zoom = 2; public int mode = none; float olddist; public boolean ontouch(view v, motionevent event) { imageview view = (imageview) v; view.setscaletype(imageview.scaletype.matrix); switch (event.getaction() & motionevent.action_mask) { case motionevent.action_down: savedmatrix.set(matrix); start.set(event.getx(), event.gety()); log.d(tag, "mode=drag"); mode = drag; break; case motionevent.action_pointer_down: olddist = spacing(event); log.d(tag, "olddist=" + olddist); if (olddist > 10f) { 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); } else if (mode == zoom) { float newdist = spacing(event); log.d(tag, "newdist=" + newdist); if (newdist > 10f) { matrix.set(savedmatrix); float scale = newdist / olddist; matrix.postscale(scale, scale, mid.x, mid.y); } } break; case motionevent.action_up: case motionevent.action_pointer_up: mode = none; log.d(tag, "mode=none"); break; } // perform transformation log.d("point", (event.getx() - start.x) + " " + (event.gety() - start.y)); // log.d("point",start.x +" "+start.y); float[] values = new float[9]; matrix.getvalues(values); float = values[matrix.mtrans_x]; float b = values[matrix.mtrans_y]; log.d("touch matrix", values[matrix.mpersp_0] + " " + values[matrix.mpersp_1] + " " + values[matrix.mpersp_2]); log.d("touch matrix scale", values[matrix.mscale_x] + " " + values[matrix.mscale_y]); log.d("touch matrix scew", values[matrix.mskew_x] + " " + values[matrix.mskew_y]); log.d("touch matrix trans", values[matrix.mtrans_x] + " " + values[matrix.mtrans_y]); if (reset == 1) { matrix.reset(); savedmatrix.reset(); } view.setimagematrix(matrix); onfront = view; reset = 0; // images.set(position, view); return true; // indicate event handled } private float spacing(motionevent event) { float x = event.getx(0) - event.getx(1); float y = event.gety(0) - event.gety(1); return (float) math.sqrt(x * x + y * y); } 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); } }); return imview; } /** * returns size (0.0f 1.0f) of views depending on * 'offset' center. */ public float getscale(boolean focused, int offset) { /* formula: 1 / (2 ^ offset) */ return math.max(0, 1.0f / (float) math.pow(2, math.abs(offset))); } }
edit: if add
view.setscaletype(imageview.scaletype.fit_center); /* set width/height of imageview. */ view.setlayoutparams(new gallery.layoutparams( layoutparams.fill_parent, layoutparams.fill_parent));
to ontouch image keeps scaling, not dragable more.
Comments
Post a Comment