Android网络视图滚动行为怪异
在我的应用程序中,我有一个允许用户通过滑动手势水平滚动的网络视图 我使用objectanimator为滚动设置动画,但即使使用简单的滚动,问题仍然会发生 问题:当我刚到某个位置时使用:scrollTo500,0;它通常会将一定数量的像素跳到任意一侧,比如100px的抖动 如果我在第一次刷卡完成后继续刷卡,错误不会发生,它会按照预期的方式运行,但在我停止刷卡的那一刻,最后一次刷卡会以随机抖动结束 代码非常简单,基本上是:myView.scrollToX,Y。我完全不明白为什么会发生这种情况。有没有人有过类似的经历 更新了touchevent中的代码,一些未被评论的内容是乏味的尝试和错误Android网络视图滚动行为怪异,android,Android,在我的应用程序中,我有一个允许用户通过滑动手势水平滚动的网络视图 我使用objectanimator为滚动设置动画,但即使使用简单的滚动,问题仍然会发生 问题:当我刚到某个位置时使用:scrollTo500,0;它通常会将一定数量的像素跳到任意一侧,比如100px的抖动 如果我在第一次刷卡完成后继续刷卡,错误不会发生,它会按照预期的方式运行,但在我停止刷卡的那一刻,最后一次刷卡会以随机抖动结束 代码非常简单,基本上是:myView.scrollToX,Y。我完全不明白为什么会发生这种情况。有没有
@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);
}
这一切都非常令人费解。原来代码中的某个地方出现了一些问题。 这是一个非常不同的、简单得多的示例,它可以完美地工作
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;
}
请给我一些密码。。。
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;
}