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

How to set group indicator in extendable listview
How to get rectangular grid co-ordinate with 2 geo points
Converting a large Canvas in Bitmap in Android
How do I build Libgdx.so from source for Android with gdb tracing enabled?
ArrayAdapter requires the resource ID to be a TextView when it is
How to create a profile tag system like in facebook?
Library projects in Eclipse missing after Github clone?
Reading when write is restricted on Mifare Ultralight C
how to vary layout size based on different screen sizes in android? [duplicate]
Facebook SDK 3.18 - replacement for the deprecated facebook constructor
searchManager.getSearchableInfo(getComponentName()) returns null only in test
Features and libraries of Nexus 5 [closed]
Obtaining Build Target in android development
File.mkdirs warning from Android Studio
How to toast a message when an user come into a particular area in googlemaps
Android Jni : crash in global and local ref variables

Categories

HOME
batch-file
phantom-dsl
azure-stream-analytics
nvd3.js
appcelerator
access
google-apps
hpc
zip
missing-data
jtextpane
ibeacon-android
heap
project-intu
automatic-ref-counting
shared-libraries
flatpak
heat
keytool
red5
mapstruct
cfml
enterprise-miner
topology
windows-xp
poedit
backpack-for-laravel
angular-routing
exit
epsilon
siri
android-sharedpreferences
corpus
winexe
sqsh
finagle
constexpr
jmockit
hibernate-validator
rselenium
dsx-desktop
installscript
processwire
playframework-2.1
tex
node-mssql
ods
linq-to-objects
ogc
odoo-website
vmware-fusion
install.packages
bindingnavigator
facebook-social-plugins
monkey-testing
servermanager
libgphoto2
openrasta
mongotemplate
flac
cpu-cache
gameplay-kit
jtree
sandcastle
pocketsphinx-android
rmongo
cvxpy
node-request
watch-os-2
jboss-tools
hclust
dimension
lean
maybe
linklabel
iokit
file-move
jquery-forms-plugin
at-job
cocos3d
raygun
insight.database
extjs2
square-bracket
amslidemenu
mysql-error-1044
c++builder-xe5
away3d
abstract-data-type
pagedown
web-notifications
boost-msm
garbage
httponly
google-maps-mobile
screen-size
filemerge
cloudfiles
cruisecontrol.rb
drupal-fivestar
complex-event-processing
scrubyt
netbeans6.1

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