使用动画android在WebView中实现Swiping

时间:2013-01-22 06:30:42

标签: android android-layout android-widget

我是android的新手,我需要使用viewFlipper在android webview.without上加载不同的动画网页。

我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

将OnGestureListener实施到我们的活动中,然后使用

private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;

    public GestureDetector detector;

   detector = new GestureDetector(this);

   webview.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {

            return detector.onTouchEvent(event);
        }
    });

    @Override
public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    return detector.onTouchEvent(event);
}

public boolean onDown(MotionEvent e) { 
    // TODO Auto-generated method stub
    return true; 
} 

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    boolean result = false;
    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) {
                    System.out.println("swipe right");


                } else {
                    System.out.println("swipe left");

                }
            }
        } 
    } catch (Exception exception) {
        exception.printStackTrace();
    }
    return result;
}


public void onLongPress(MotionEvent e) {
    // TODO Auto-generated method stub

}
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {
    // TODO Auto-generated method stub
    return true;
}
public void onShowPress(MotionEvent e) {
    // TODO Auto-generated method stub

}
public boolean onSingleTapUp(MotionEvent e) {
    // TODO Auto-generated method stub
    return true;
}

答案 1 :(得分:2)

Animation slideLeftAnim = AnimationUtils.loadAnimation(getBaseContext (), R.anim.righttoleft_in);
mWebView.startAnimation(slideLeftAnim);
动画文件夹中的

righttoleft_in.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator" >

<translate
    android:duration="200"
    android:fromXDelta="100%p"
    android:toXDelta="0" />

</set>
动画文件夹中的

righttoleft_out.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator" >

<translate
    android:duration="200"
    android:fromXDelta="0"
    android:toXDelta="-100%p" />

</set>