performance - Android imageView is lagging when loading image from bitmap -
i have imageview has touch event move, rotate, , zoom imageview. code works fine, when load image gallery instead of drawable folder, touch event lagging. when try move, rotate or zoom in image, there delay , slow.
imgphoto.setontouchlistener(new view.ontouchlistener() { relativelayout.layoutparams parms; int startwidth; int startheight; float dx = 0, dy = 0, x = 0, y = 0; float angle = 0; @override public boolean ontouch(view v, motionevent event) { final imageview view = (imageview) v; ((bitmapdrawable) view.getdrawable()).setantialias(true); switch (event.getaction() & motionevent.action_mask) { case motionevent.action_down: parms = (relativelayout.layoutparams) view.getlayoutparams(); startwidth = parms.width; startheight = parms.height; dx = event.getrawx() - parms.leftmargin; dy = event.getrawy() - parms.topmargin; mode = drag; break; case motionevent.action_pointer_down: olddist = spacing(event); if (olddist > 10f) { mode = zoom; } d = rotation(event); break; case motionevent.action_up: break; case motionevent.action_pointer_up: mode = none; break; case motionevent.action_move: if (mode == drag) { x = event.getrawx(); y = event.getrawy(); parms.leftmargin = (int) (x - dx); parms.topmargin = (int) (y - dy); parms.rightmargin = 0; parms.bottommargin = 0; parms.rightmargin = parms.leftmargin + (5 * parms.width); parms.bottommargin = parms.topmargin + (10 * parms.height); view.setlayoutparams(parms); } else if (mode == zoom) { if (event.getpointercount() == 2) { newrot = rotation(event); float r = newrot - d; angle = r; x = event.getrawx(); y = event.getrawy(); float newdist = spacing(event); if (newdist > 10f) { float scale = newdist / olddist * view.getscalex(); if (scale > 0.6) { scalediff = scale; view.setscalex(scale); view.setscaley(scale); } } view.animate().rotationby(angle).setduration(0).setinterpolator(new linearinterpolator()).start(); x = event.getrawx(); y = event.getrawy(); parms.leftmargin = (int) ((x - dx) + scalediff); parms.topmargin = (int) ((y - dy) + scalediff); parms.rightmargin = 0; parms.bottommargin = 0; parms.rightmargin = parms.leftmargin + (5 * parms.width); parms.bottommargin = parms.topmargin + (10 * parms.height); view.setlayoutparams(parms); } } break; } return true; } });
the works fine when load image drawable folder in following
android:src="@drawable/icon"
however, if try load image gallery, touch event suppose move, rotate , zoom image lagging.
here's code use load image gallery , set image bitmap imageview.
intent intent = new intent(); intent.settype("image/*"); intent.setaction(intent.action_get_content); startactivityforresult(intent.createchooser(intent, "select picture"), pick_image);
below code set imageview image phone.
@override public void onactivityresult(int requestcode, int resultcode, intent data) { super.onactivityresult(requestcode, resultcode, data); if(requestcode == pick_image && resultcode == activity.result_ok) { uri selectedimage = data.getdata(); try{ inputstream imagestream = getactivity().getcontentresolver().openinputstream(selectedimage); bitmap yourselectedimage = bitmapfactory.decodestream(imagestream); imgphoto.setimagebitmap(yourselectedimage); } catch (exception e) { e.printstacktrace(); } } }
Comments
Post a Comment