i wondering how can reference image curently displayed gallery view? example wish add button rotate image, dont understand how select it. thought possible getitemviewtype
dont understand returns.
activity
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.image_show); app = this; final gallery gallery = (gallery) findviewbyid(r.id.gallery); // edges invisible gallery.sethorizontalfadingedgeenabled(false); ia = new imageadapterr(this); gallery.setadapter(ia); // ia.getview(position, convertview, parent); final int length = uilapplication.photo_buffer_big.size(); button back_btn = (button) findviewbyid(r.id.analitics_back_btn); back_btn.setonclicklistener(new onclicklistener() { public void onclick(view v) { finish(); } }); final textview img_counter_tv = (textview) findviewbyid(r.id.img_counter); img_counter_tv.settext(p + 1 + "/" + length); button nextbutton = (button) findviewbyid(r.id.next_btn); nextbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (p < length - 1) { p++; } else { p = 0; } gallery.setselection(p, true); img_counter_tv.settext(p + 1 + "/" + length); } }); button backbutton = (button) findviewbyid(r.id.back_btn); backbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { if (p == 0) { p = length - 1; } else { p--; } gallery.setselection(p, true); img_counter_tv.settext(p + 1 + "/" + length); } }); } public void rotates(view v) { // int q = (ia.getitemviewtype(p)); // bitmap b = ((bitmapdrawable) imview.getdrawable()).getbitmap(); bitmap b = ((bitmapdrawable) images.get(p).getdrawable()).getbitmap(); matrix matrix = new matrix(); matrix.postrotate(geg); matrix.postscale(0.5f, 0.5f); bitmap bmaprotate = bitmap.createbitmap(b, 0, 0, b.getwidth(), b.getheight(), matrix, true); imview.setimagebitmap(bmaprotate); geg = (geg+90)%360; }
adapter
class imageadapterr extends baseadapter { /** parent context */ private context mycontext; /** simple constructor saving 'parent' context. */ public imageadapterr(context c) { this.mycontext = c; } /** 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 imageview getimage(int position, view convertview, viewgroup parent) { // // } public view getview(int position, view convertview, viewgroup parent) { imageview imview = new imageview(this.mycontext); imgpos = position; asyncload imload = new asyncload(); imload.execute(); try { bm = imload.get(); } catch (interruptedexception e) { // todo auto-generated catch block e.printstacktrace(); } catch (executionexception e) { // todo auto-generated catch block e.printstacktrace(); } if (bm != null) { imview.setimagebitmap(bm); } else if (bm == null) { imview.setimageresource(r.drawable.logo); } /* 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.match_parent)); imview.setontouchlistener(new ontouchlistener() { private static final string tag = "touch"; // these matrices used move , zoom image matrix matrix = new matrix(); matrix savedmatrix = new matrix(); 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 view.setimagematrix(matrix); 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 floatmath.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); } }); images.add(imview); log.d("images","id "+ imview.getid()); log.d("images","size "+ images.size()); 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))); } class asyncload extends asynctask<void, void, bitmap> { progressdialog pd; @override protected void onpreexecute() { pd = new progressdialog(app); pd.setowneractivity(app); pd.settitle("Идет загрузка..."); pd.setcancelable(true); pd.show(); } @override protected bitmap doinbackground(void... arg0) { // todo auto-generated method stub try { /* * open new url , inputstream load data it. */ url aurl = new url(uilapplication.photo_buffer_big.get(imgpos)); urlconnection conn = aurl.openconnection(); conn.connect(); inputstream = conn.getinputstream(); /* buffered performance plus. */ bufferedinputstream bis = new bufferedinputstream(is); /* decode url-data bitmap. */ bm = bitmapfactory.decodestream(bis); bis.close(); is.close(); /* apply bitmap imageview returned. */ // imview.setimagebitmap(bm); } catch (ioexception e) { // imview.setimageresource(r.drawable.logo); bm = null; log.e("debugtag", "remote image exception", e); } return bm; } @override protected void onpostexecute(bitmap arg0) { pd.dismiss(); } } }
this code use ...try .... on button click ------------------------------------------------------------------------------- intent = new intent( intent.action_pick, android.provider.mediastore.images.media.external_content_uri); startactivityforresult(i, result_load_image); ----------------------------------------------------------------------------- @override protected void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if (requestcode == result_load_image && resultcode == result_ok && null != data) { uri selectedimage = data.getdata(); string[] filepathcolumn = { mediastore.images.media.data }; cursor cursor = getcontentresolver().query(selectedimage, filepathcolumn, null, null, null); cursor.movetofirst(); int columnindex = cursor.getcolumnindex(filepathcolumn[0]); string picturepath = cursor.getstring(columnindex); cursor.close(); // string picturepath contains path of selected image }
Comments
Post a Comment