Hello,

I am trying to implement a custom WebView and override OnTouchEvent()
so that a tap near the bottom of the screen would result in the view
smoothly scrolling down one ‘page’ (and a tap at the top would result
in a smooth upward scroll).

I’ve been looking at the Scroller Widget to do the animated scrolling
but can’t seem to figure out how to make it work right. I found an
example of how to use a Scroller to animate a TextView (http://bear-
polka.blogspot.com/2009/01/scrolltextview-scrolling-textview-
for.html), but TextViews have a setScroller() method to attach a
Scroller to that view.

How can you attach and use a Scroller in a View that does not expose a
setScroller() method?

Below is the relevant section of my code:


private Scroller mScroller;
private static final long TAP_THRESHHOLD = 300; //ms
private float mDownY = 0;
private int mHeight = 0;
private int mWidth = 0;

public MyWebView(Context context, ChapterNavigator cn) {
   super(context);
   mScroller = new Scroller(context);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
   Log.v(TAG, "onTouchEvent");
   final int action = event.getAction();

   if (action == MotionEvent.ACTION_DOWN) {
      //Abort scrolling animation if not finished yet
      if (!mScroller.isFinished()) {
         Log.v(TAG, "aborting scroll");
         mScroller.abortAnimation();
      }
      mDownY = event.getY();
   }
   else if (action == MotionEvent.ACTION_UP) {
      //Detect if we had a TAP
      if(event.getEventTime() - event.getDownTime() < TAP_THRESHHOLD)
{
         if(event.getX() < mHeight/2 ) {
            mScroller.startScroll(0, mDownScrollY, 0, -mHeight, 1000);
         }
         else {
            mScroller.startScroll(0, mDownScrollY, 0, mHeight, 1000);
         }
      }
   }
   return true;
}

@Override
protected void onSizeChanged(int w, int h, int ow, int oh) {
   super.onSizeChanged(w, h, ow, oh);
   mWidth = w;
   mHeight = h;
}


I’d greatly appreciate any pointer or suggestion.

Thanks

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers-unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to