c# - What is the best way to implement both swipe and click over a Relative Layout in android? -
creating relative layout , want add click swipe(touch , move pointer on layout move in real time) , detect left , right swipe. have tried following code far.
public bool ontouch(view v, motionevent e) { if (gesturedetector.ontouchevent(e)) { //this click return true; } else { int initialtouchx = 0, initialtouchy = 0; int newx = 0; var x = v.left; switch (e.action) { case motioneventactions.down: { _viewx = e.getx(); _viewy = e.gety(); initialtouchx = (int)e.rawx; initialtouchy = (int)e.rawy; break; } case motioneventactions.move: { var left = (int)(e.rawx - _viewx); newx = left; var right = (int)(left + v.width); var top = (int)(e.rawy - _viewy); var bottom = (int)(top + v.height); v.layout(left, top, right, bottom); break; } case motioneventactions.up: { int lastx = (int)e.getx(); int lasty = (int)e.gety(); if ((x - newx) > 40) { //detect right swipe } else if ((newx - x > 40)) { //detect left swipe } else { //skip others } break; } } } return true; }
my code gesturedetector.ontouchevent(e)
gesturedetector = new gesturedetector(this, new singletapup()); class singletapup : android.views.gesturedetector.simpleongesturelistener { public override bool onsingletapup(motionevent e) { // toast.maketext(this,, toastlength.long).show(); return true; } }
my code working fine on devices emulator. not working , onclick layout moves automatically(the layout out centers @ touch pointer). think wrong , causing issue. can suggest me best/standard way it. appreciated.
im implementing swype in way:
public class mainactivity : activity, gesturedetector.iongesturelistener { private gesturedetector _gesturedetector; public bool ondown(motionevent e) { return false; } public bool onfling(motionevent e1, motionevent e2, float velocityx, float velocityy) { bool result = false; int swipe_threshold = 80; int swipe_velocity_threshold = 80; try { float diffy = e2.gety() - e1.gety(); float diffx = e2.getx() - e1.getx(); if (math.abs(diffx) > math.abs(diffy)) { if (math.abs(diffx) > swipe_threshold && math.abs(velocityx) > swipe_velocity_threshold) { if (diffx > 0) { //code swipe right here } else { //code swipe left here } } } } catch (exception exception) { //exception.printstacktrace(); } return result; } public void onlongpress(motionevent e) {} public bool onscroll(motionevent e1, motionevent e2, float distancex, float distancey) { return false; } public void onshowpress(motionevent e) {} public bool onsingletapup(motionevent e) { return false; } public override bool ontouchevent(motionevent e) { _gesturedetector.ontouchevent(e); return false; } protected override void oncreate (bundle bundle) { base.oncreate (bundle); setcontentview (resource.layout.main); _gesturedetector = new gesturedetector(this); } }
for viewpager
can overlap pages pagetransformation
:
using system; using android.views; using android.support.v4.view; namespace somename { public class sinkandslidetransformer : java.lang.object, viewpager.ipagetransformer { public void transformpage(view view, float position) { if (position < -1 || position > 1) { view.alpha = 0; // view offscreen. } else { view.alpha = 1; if (position < 0) { // 'sink' view if it's left. // scale view. view.scalex = 1 - math.abs (position); view.scaley = 1 - math.abs (position); // set translationx keep view in middle. view.translationx = view.width * math.abs (position); } } } } }
with adapter
:
using system; using system.collections.generic; using android.app; using android.content; using android.runtime; using android.views; using android.widget; using android.os; using android.support.v4.view; using square.picasso; namespace staffpro { public class slideshowpageradapter : pageradapter { list<uri> _items = new list<uri>(); private readonly activity _context; public slideshowpageradapter (activity context, list<uri> items) : base() { _items = items; _context = context; } public override int count { { return _items.count; } } public override bool isviewfromobject(view view, java.lang.object _object) { return view == ((relativelayout) _object); } public override java.lang.object instantiateitem(viewgroup container, int position) { var view = layoutinflater.from (container.context).inflate (resource.layout.slideshowviewpager, container, false); imageview imageview = view.findviewbyid<imageview> (resource.id.slideshowimageview); picasso.with(_context).load(_items [position].tostring()).into(imageview); container.addview(view); return view; } public override void destroyitem(viewgroup container, int position, java.lang.object _object) { container.removeview((view)_object); } } }
and call viewpages , adapter this:
var viewpageradapter = new slideshowpageradapter(activity, some_uri_images); slideshowviewpager.adapter = viewpageradapter; slideshowviewpager.setpagetransformer (true, new sinkandslidetransformer ());
more transformations can find here.
also keep in mind viewpager
has method called setpagemargin()
. method can receive negative value make fragments/views overlap each other.
can use view.rotation
inside transformation code implement photo randomly dropped , overlapped effect want.
if need retain many images loaded , displayed check viewpager.offscreenpagelimit
override default cache limit of storing pages next , previous item, careful high memory usage however.
Comments
Post a Comment