android


Android webview Scrolling behaving weird


In my app I have a webview that the user is allowed to scroll horizontally in, through swipe gestures.
I use an objectanimator to animate the scrolling, but even with a simple scrollTo() the problem still occurs.
Problem: When I just to a certain position using: scrollTo(500, 0); it very often jumps a certain amount of pixels to either side, like a 100px jitter.
If I keep swiping RIGHT after the first swipe is done, the error doesnt occur and it acts as expected, but the moment I stop swiping the last swipe ends with a random jitter.
The code is super simple and basically: myView.scrollTo(X,Y). I am completely stumped as to why this happens. have anyone had similar experiences?
update Put the code from the touchevent, some of the outcommented stuff is tedious trial and error.
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouchEvent(final MotionEvent event) {
if(swipeable && !animationRunning){
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
swipeStartPosition = event.getX();
break;
case MotionEvent.ACTION_MOVE:
// Nothing here, move along.
break;
case MotionEvent.ACTION_UP:
float swipeDistance = Math.abs(event.getX() - swipeStartPosition);
// Check if event is click or swipe.
// A swipe is hardcoded to a distance of 50px.
if (swipeDistance > 50) {
// Swipe.
if (event.getX() > swipeStartPosition) {
// Right swipe (previous).
// Don't swipe if you're on the first page.
if (targetPage == 0) {
Toast.makeText(getContext(), "No sections that way, try the other way.", Toast.LENGTH_SHORT).show();
} else {
prevPage = targetPage;
targetPage--;
//inform main activity about pagechange to update UI
mMainActivity.setTargetPage(prevPage, targetPage);
/*
mWebView.postDelayed( new Runnable () {
#Override
public void run() {
mWebView.scrollTo(2560, 0);
}
}, 3000);
*/
//scrollTo(2560, 0);//targetPage * pageWidth, 0);
ObjectAnimator objectAnimator = ObjectAnimator.ofInt(this.mWebView, "scrollX", computeHorizontalScrollOffset(), (targetPage * pageWidth)).setDuration(300);
objectAnimator.addListener(new AnimatorListener() {
#Override
public void onAnimationStart(Animator arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animator arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animator arg0) {
// TODO Auto-generated method stub
animationRunning = false;
}
#Override
public void onAnimationCancel(Animator arg0) {
// TODO Auto-generated method stub
}
});
objectAnimator.start();
}
} else {
// Left swipe (next).
if (computeHorizontalScrollRange() <= (computeHorizontalScrollOffset() + computeHorizontalScrollExtent())) {
Toast.makeText(getContext(), "No sections that way, try the other way.", Toast.LENGTH_SHORT).show();
} else {
prevPage = targetPage;
targetPage++;
mMainActivity.setTargetPage(prevPage, targetPage);
/*
mWebView.postDelayed( new Runnable () {
#Override
public void run() {
mWebView.scrollTo(5120, 0);
}
}, 3000);
*/
//scrollTo(5120, 0); //targetPage * pageWidth, 0);
ObjectAnimator objectAnimator = ObjectAnimator.ofInt(this.mWebView, "scrollX", computeHorizontalScrollOffset(), (targetPage * pageWidth)).setDuration(300);
objectAnimator.addListener(new AnimatorListener() {
#Override
public void onAnimationStart(Animator arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationRepeat(Animator arg0) {
// TODO Auto-generated method stub
}
#Override
public void onAnimationEnd(Animator arg0) {
// TODO Auto-generated method stub
animationRunning = false;
}
#Override
public void onAnimationCancel(Animator arg0) {
// TODO Auto-generated method stub
}
});
objectAnimator.start();
}
}
} else {
// Click.
// Do nothing here.
}
// int pageWidth = getWidth();
//
// int targetPage = (int)Math.round( (pos + event.getX()) / (float)pageWidth );
//
//
// scrollTo(targetPage * pageWidth, 0);
break;
}
}
return super.onTouchEvent(event);
}
Also, the displacement that is happening seems to be bound with the initial touchevent itself rather than the scrolling. If I do the following, and put a 3 sec delay, I see that the displacement happens again a short sec after the touch event, but then when the scrollTo() fires after 3 sec it actually jumps to the correct position:
mWebView.postDelayed( new Runnable () {
#Override
public void run() {
mWebView.scrollTo(2560, 0);
}
}, 3000);
It's all very puzzling..
It turns out somewhere in the original code, something messed up.
This is a very different, much simpler example, that works perfectly.
float x1,x2;
float y1,y2;
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
x1 = event.getX();
y1 = event.getY();
break;
case MotionEvent.ACTION_UP:
x2 = event.getX();
y2 = event.getY();
if(x1 < x2){
//left to right
prevPage = targetPage;
targetPage--;
Toast.makeText(getContext(), "left to right", Toast.LENGTH_SHORT).show();
ObjectAnimator objectAnimator = ObjectAnimator.ofInt(this.mWebView, "scrollX", computeHorizontalScrollOffset(), (targetPage * pageWidth)).setDuration(300);
objectAnimator.start();
}
if(x1 > x2){
//right to left
prevPage = targetPage;
targetPage++;
Toast.makeText(getContext(), "right to left", Toast.LENGTH_SHORT).show();
ObjectAnimator objectAnimator = ObjectAnimator.ofInt(this.mWebView, "scrollX", computeHorizontalScrollOffset(), (targetPage * pageWidth)).setDuration(300);
objectAnimator.start();
}
/*
ObjectAnimator objectAnimator = ObjectAnimator.ofInt(mWebView, "scrollX", computeHorizontalScrollOffset(), (targetPage * displaymetrics.widthPixels)).setDuration(300);
objectAnimator.start();
*/
break;
}
return true;
}

Related Links

Tasker Plugin Variable Replacement: Developer
How to manage “automatic text color” in webView
Why Two Methods for FirebaseDatabase Client Offline?
In Android Studio, set android:text from strings.xml, but back to text later
Bring to front a view over foreground image of a frameLayout
Google Cloud Message Android Key
Android TextView linespacing
Clarification about ios and android push and ionic
error : couldn't read row 0 , col -1 from cursorWindow make sure the cursor is initialized correctly before accessing data from it?
Regd - Geting Bluetooth rssi value of Paired devices
Google Places Autocomplete not working after closing the application
enable click event on parallax ListView with sticky header in android
Get Lifelog API result
How to Know Current Install Location
How to set my circleImageView on the border of my layout in android?
How to make News Bar with Spannable Marquee TextView in Android?

Categories

HOME
django
phantom-dsl
kentico
fuelux
string
pyqt
toolbox
oracle-apex-5
solaris
access
xbee
rds
nested-if
zip
extjs4.2
qpython
p2p
jquery-ui-sortable
drop-down-menu
plist
intercom
zerobrane
magnetic-cards
qsqlquery
http-post
google-data-studio
symbols
squashfs
jquery-ias
highstock
dynamically-generated
itunes
beautifier
entity-relationship-model
browser-sync
realm-mobile-platform
dotspatial
mockjax
record
testbed
simplecv
sammy.js
nested-lists
engine.io
docfx
alphabetical
jpad
filesystemwatcher
odroid
cin
typesafe-config
spring-mvc-test
visa
congestion-control
apollostack
clang++
textsum
automapper-5
gitlist
datamaps
convex-optimization
appdomain
google-photos
swingworker
choice
node-request
cubism.js
stax
reporting-services-2012
delta
udpclient
embedding
aurora
phpwebsocket
grass
blitline
rasterize
boomerang
terracotta
dock
iced-coffeescript
dulwich
pyobjc
telepat
diff3
public-html
cocos3d
trdion2011
ss7
personalization
codeplex
grails-domain-class
drupal-nodes
inappsettingskit
beaker-testing
android-icons
mantle
curb
tree-balancing
frameworkelementfactory
mkv
adobe-media-server
usertype
jquery-address
relative
denied
update-statement
lr
tournament
photolibrary
timertask
drupal-fivestar
lxr
filesystemobject
unmanagedresources

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App