2

我想使用 webview 构建一个由 9 个图块组成的应用程序。它们中的每一个都是我加载到 webview 中的 .html 文件,并且导航是通过滑动进行的。

这是刷卡的代码:

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)
          onSwipeRight();
        else
          onSwipeLeft();
      }
    } else {
      if (Math.abs(diffY) > SWIPE_THRESHOLD && Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
        if (diffY > 0)
          onSwipeBottom();
        else
          onSwipeTop();
      }
    }
  } catch (Exception exception) {
    exception.printStackTrace();
  }
  return result;
}

其中每种onSwipe<Direction>()方法的结构如下:

private void onSwipeTop() {
  if (row < MAX_ROW) {
    row++;
    loadUrl(Direction.UP);
    System.out.println("Swipe Top!");
  }
}

然后动画由在另一个问题中找到的这段代码处理:

public void onPageFinished(WebView view, String url) {
    webView.setDrawingCacheEnabled(true);
    webView.buildDrawingCache();
    imageView.setImageBitmap(Bitmap.createBitmap(webView.getDrawingCache()));
    webView.setDrawingCacheEnabled(false);

    imageView.setVisibility(View.VISIBLE);
    webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

    ObjectAnimator webViewAnimator = null;
    ObjectAnimator imageViewAnimator = null;

    switch (currentDirection) {
      case LEFT:
        System.out.println(">> Left!");
        webViewAnimator =
            ObjectAnimator.ofFloat(webView, "translationX", webView.getWidth(), 0);
        imageViewAnimator =
            ObjectAnimator.ofFloat(imageView, "translationX", 0, -webView.getWidth());
        break;
      case RIGHT:
        System.out.println(">> Right!");
        webViewAnimator =
            ObjectAnimator.ofFloat(webView, "translationX", -webView.getWidth(), 0);
        imageViewAnimator =
            ObjectAnimator.ofFloat(imageView, "translationX", 0, webView.getWidth());
        break;
      case UP:
        System.out.println(">> Up!");
        webViewAnimator =
            ObjectAnimator.ofFloat(webView, "translationY", webView.getHeight(), 0);
        imageViewAnimator =
            ObjectAnimator.ofFloat(imageView, "translationY", -webView.getHeight());
        break;
      case BOTTOM:
        System.out.println(">> Bottom!");
        webViewAnimator =
            ObjectAnimator.ofFloat(webView, "translationY", -webView.getHeight(), 0);
        imageViewAnimator =
            ObjectAnimator.ofFloat(imageView, "translationY", webView.getHeight());
        break;
      default:
        break;
    }

    imageViewAnimator.setDuration(SLIDE_DURATION);
    webViewAnimator.setDuration(SLIDE_DURATION);

    AnimatorSet set = new AnimatorSet();
    set.addListener(new AnimatorListenerAdapter() {
      public void onAnimationEnd(Animator animation) {
        webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        imageView.setVisibility(View.GONE);
      }
    });

    set.playTogether(imageViewAnimator, webViewAnimator);
    set.start();

我的问题:只要我改变滑动方向,ImageView 就不再出现,而是被白屏代替,而我想获得平滑的过渡效果。有谁能够帮我?或者也许建议另一种解决方案?任何帮助将非常感激!

4

1 回答 1

0

我对 webviews 有类似的问题,这个问题解决方案解决了我的问题。不久,我禁用了我的活动的硬件加速。希望这可以帮助。

于 2013-04-25T12:14:26.173 回答