保持menuitem动画不重叠?

时间:2013-07-24 01:46:52

标签: android android-webview android-animation

因此,每当menuitem加载时以及活动开始时,我都会尝试为刷新webview设置动画。我也试图在没有必要时隐藏前锋menuitem。以下代码完美地工作,除非webview正在加载并且您回击或当您尝试一次执行多个任务时(单击返回大量历史记录)。每当发生这种情况时,刷新menuitem往往会复制,动画和重叠。转发menuitem也会发生这种情况。它往往与动画刷新图标重叠。有没有办法防止这种情况发生?以下代码完全是我的代码,除了已被修剪的任何不相关的代码。有人可以帮助我克服这个错误吗?

public class TideWeb extends Activity {
    private static WebView webView;
    String name = null;
    MenuItem refresh, forward;
    ActionBar ab;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent launchingIntent = getIntent();
        name = launchingIntent.getType().toString();
        setContentView(R.layout.webview);
        webView = (WebView) findViewById(R.id.WebView);
        webView.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView webview, String url) {
                    if (refresh != null && refresh.getActionView() == null) {
                        StartAnimation();
                    }
                    ab.setSubtitle("Loading...");
                    webView.loadUrl(url);

                return true;

            }
        });
        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                setSupportProgress(progress * 100);
                if (progress == 100) {
                    view.clearCache(true);
                    StopAnimation();
                    ab.setSubtitle(name);
                }
            }
        });
        String url = launchingIntent.getData().toString();

        webView.loadUrl(url);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.tideweb, menu);
        forward = menu.findItem(R.id.forward_menu);
        if (webView.isFocused() && webView.canGoForward()) {
            forward.setVisible(true);
        } else {
            forward.setVisible(false);
        }
        ab.setSubtitle("Loading...");
        refresh = menu.findItem(R.id.refresh_menu);
        if (refresh != null && refresh.getActionView() == null) {
            StartAnimation();
        }
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.forward_menu:
            webView.goForward();
            invalidateOptionsMenu();
            break;
        case R.id.refresh_menu:
            webView.reload();
            invalidateOptionsMenu();
            break;
        case android.R.id.home:
            Intent intent = new Intent(this, Main.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
            return true;
        }
        return true;
    }

    public void onBackPressed() {
        if (webView.isFocused() && webView.canGoBack()) {
            webView.goBack();
            invalidateOptionsMenu();
        } else {
            super.onBackPressed();
            finish();
        }
    }

    private void StartAnimation() {
    if (refresh != null && refresh.getActionView() == null) {
            final LayoutInflater inflater = (LayoutInflater) getApplication()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final RelativeLayout ivRefresh = (RelativeLayout) inflater.inflate(
                R.layout.refresh_view, null);

            final Animation rotation = AnimationUtils.loadAnimation(
                getApplication(), R.anim.rotate);
            ivRefresh.startAnimation(rotation);
            refresh.setActionView(ivRefresh);
        }
    }

    private void StopAnimation() {
        if (refresh != null && refresh.getActionView() != null) {
            refresh.getActionView().clearAnimation();
            refresh.setActionView(null);
        }
    }
}

加载,然后返回: loading, then back

加载,然后后退和后退: loading, then back and back

1 个答案:

答案 0 :(得分:0)

我并没有真正找到解决方法,但是我没有使用图像和动画,而是将动作视图设置为进度条。重叠不再发生了,所以我想现在已经解决了。