动画后Android WebView不显示

时间:2013-06-28 13:19:36

标签: android android-webview android-animation android-gesture

嗨,我正在制作一本Android应用程序。我正在使用带有WebView的LinearLayout(全屏活动)和带有GestureDetector的OnTouchListener。当我滑动WebView时,它应该加载一个带动画的不同html页面。问题是在动画之后webview是空白的(如果我触摸它,页面为dsiplays)。我究竟做错了什么?这是一段代码片段:

public class MainActivity extends Activity implements OnClickListener, OnTouchListener, OnGestureListener {

    private WebView webview;
    private GestureDetector gesturedetector = null;

    Animation animationNext;
    Animation animationPrevious;

    ArrayList<String> pages;
    ListIterator<String> iterator;

    private int SWIPE_MIN_VELOCITY = 100;
    private int SWIPE_MIN_DISTANCE = 100;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Remove title make full-screen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);

        // Setup Variables
        webview = (WebView) findViewById(R.id.vwBrowser);

        pages = new ArrayList<String>(
                Arrays.asList("index.html", "grus.html", "pigeon.html"));
        iterator = pages.listIterator();

        animationNext = AnimationUtils.loadAnimation(this, R.anim.next_animation);
        animationPrevious = AnimationUtils.loadAnimation(this, R.anim.previous_animation);

        // Setup Listeners
        gesturedetector = new GestureDetector(this, this);
        webview.setOnTouchListener(this);

        // Setup WebView
        webview.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);

                return true;
            }

        });
        webview.loadUrl(String.format("file:///android_asset/%s", iterator.next()));
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        //Get Position
        float ev1X = e1.getX();
        float ev2X = e2.getX();

        //Get distance of X (e1) to X (e2)
        final float xdistance = Math.abs(ev1X - ev2X);
        //Get veclocity of cusor
        final float xvelocity = Math.abs(velocityX);

        SharedPreferences getPrefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        boolean useAnimation = getPrefs.getBoolean("animation", true);

        if( (xvelocity > SWIPE_MIN_VELOCITY) && (xdistance > SWIPE_MIN_DISTANCE) )
        {
            if(ev1X > ev2X)//Switch Left
            {
                if (!iterator.hasNext())
                    iterator = pages.listIterator();
                if (useAnimation) {
                    webview.startAnimation(animationNext);
                }
                webview.loadUrl(String.format("file:///android_asset/%s", iterator.next()));
            }
            else//Switch Right
            {
                if (!iterator.hasPrevious())
                    iterator = pages.listIterator(pages.size());
                if (useAnimation) {
                    webview.startAnimation(animationPrevious);
                }
                webview.loadUrl(String.format("file:///android_asset/%s", iterator.previous()));
            }
        }

        return false;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return gesturedetector.onTouchEvent(event);
    }

}

这是动画xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" >

    <alpha
        android:duration="300"
        android:fromAlpha="1.0"
        android:toAlpha="0.5" />

    <translate
        android:duration="500"
        android:fromXDelta="0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toXDelta="-100%" />

    <scale
        android:duration="500"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.7"
        android:toYScale="0.3" />

</set>

0 个答案:

没有答案