对话框动画被webview弄乱了:android bug? [英] Dialog animation messed up by webview: android bug?

查看:80
本文介绍了对话框动画被webview弄乱了:android bug?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我制作了一个包含Enter和Exit慢速动画的对话框.但是该对话框包含一个Webview myMsg(该文件加载了本地文件,因此没有延迟)并弄乱了动画.

I made a dialog with both Enter and Exit slow animations. But the dialog contains a webview myMsg (that loads a local file, so no delay) and messes up the animation.

使用下面的代码(无webview),该对话框可以完美运行,在Enter和Exit处设置动画.但是,如果我取消评论 在//builder.setView(myMsg)行中,Exit动画仍然可以正常运行,但是Enter动画没有执行(或执行得太快).有趣的是,如果我最小化应用程序并再次最大化它,则输入"对话框动画效果很好.

With the code below (no webview), the dialog works perfectly, animating both at Enter and at Exit. However, if I uncomment the line //builder.setView(myMsg), the Exit animation still works perfectly, but the Enter animation is not performed (or performed too fast). Funny thing, if I minimize the app and maximize it again, the Enter dialog animation is performed fine.

就像Webview加载使Enter动画混乱一样.尝试在加载Web视图后显示对话框,结果相同.

It's like the webview load messes the Enter animation. I tried to show the dialog after the webview is loaded, with the same results.

那不是疯了吗?关于正在发生的事情以及如何解决的任何线索?任何帮助将不胜感激.

Isn't that crazy?? Any clue of what is happening, and how to solve it? Any help would be appreciated.

这是代码的相关部分:

    WebView myMsg = new WebView(context);
    myMsg.loadUrl("file:///android_asset/page.html");

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setIcon(R.drawable.ic_launcher);
    //builder.setView(myMsg);
    builder.setTitle("Some Title");
    final AlertDialog dialog = builder.create();

    WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
    lp.windowAnimations = R.style.AnimateDialog;

    dialog.show();
    dialog.getWindow().setAttributes(lp);

和样式

<style name="AnimateDialog">
    <item name="android:windowEnterAnimation">@anim/in_left</item>
    <item name="android:windowExitAnimation">@anim/out_left</item>
</style>

编辑

我尝试了setWebViewClient,但是没有运气(没有dialog.getWindow().setLayout...行,Enter动画根本不起作用)

I tried with a setWebViewClient, but without luck (without the line dialog.getWindow().setLayout... the Enter animation simply does not work):

    WebView myMsg = new WebView(context);
    myMsg.getSettings().setJavaScriptEnabled(true);
    myMsg.loadUrl("file:///android_asset/" + page);
    myMsg.setBackgroundColor(0);
    myMsg.setSoundEffectsEnabled(true);

    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    builder.setIcon(R.drawable.ic_launcher);
    builder.setTitle("SomeTitle");
    builder.setView(myMsg);
    final AlertDialog dialog = builder.create();

    myMsg.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            dialog.show();
            dialog.getWindow().setWindowAnimations(R.style.AnimateDialog);
            dialog.getWindow().setLayout(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT);
        }
    }

EDIT2

我还尝试了使用xml布局的Dialog而不是AlertDialog,也遇到了相同的问题(也尝试了WebView.loadData而不是WebView.loadUrl,同样的问题):

I also tried with Dialog instead of AlertDialog, with an xml layout, getting identical problems (tried also WebView.loadData instead of WebView.loadUrl, same problems):

    final Dialog d = new Dialog(context);
    d.setContentView(R.layout.viewhtml);
    d.setTitle("SomeTitle");
    // Without this, Enter animation does not work
    d.getWindow().setLayout(WindowManager.LayoutParams.FILL_PARENT,
            WindowManager.LayoutParams.FILL_PARENT);
    WebView wv = (WebView) d.findViewById(R.id.webview);
    wv.loadUrl("file:///android_asset/" + page);
    wv.setBackgroundColor(0);
    d.getWindow().setWindowAnimations(R.style.AnimateDialog);
    wv.setWebViewClient(new WebViewClient() {
        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            d.show();
        }
    });

这是Dialog xml布局文件:

and this is Dialog xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<WebView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webview"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:soundEffectsEnabled="true" >
</WebView>

</LinearLayout>

EDIT3

我刚刚意识到,将TextViewTextView.setText(Html.fromHTML...)而不是WebView一起使用时,会发生相同的问题.此外,如果我在 dialog.show()之后添加dialog.getWindow().setLayout(600,800) ,则动画将按预期执行.看来问题在于动画是在事先不知道对话框大小的情况下执行的吗?

I just realized that the same problem happens when using a TextView with TextView.setText(Html.fromHTML...) instead of a WebView. Moreover, if I add dialog.getWindow().setLayout(600,800) after dialog.show(), the animation is performed as expected. So it seems that the problem is that the animation is not performed without knowing beforehand the dialog size?

推荐答案

对话框动画似乎被WebView弄乱的原因是WebView内容加载的异步特性.因此,将loadUrlloadData或什至loadDataWithBaseURL放在dialog.show()之前,不能保证即使内容来自本地文件或字符串,内容也已完全加载.仍然会有一些延迟(通常间隔一秒钟或不到一秒钟).

The reason why your dialog animation seemed to be messed up by the WebView is the asynchronous nature of the loading of WebView's content. So putting the loadUrl, loadData or even loadDataWithBaseURL before dialog.show() is not a guarantee that the content is already fully loaded even if the content comes from local file or string. There is still a slight delay (normally split of a second or less than a second).

如果要真正显示对话框 ,请在WebViewClientonPageFinished内部显示该对话框.我已经对此进行了测试,并且它在ICS上可以正常工作,但是与用户激活对话框和WebView内容的可用性相比,当然有一点延迟.如果稍有延迟,那么此解决方案可能适合您.

If you want to truly display the dialog only after the WebView content is fully loaded, you will have to show the dialog inside onPageFinished of WebViewClient. I have tested this and it worked fine with ICS, but of course with a slight delay from the time the user activated the dialog and the availability of WebView content. If the slight delay is fine then this solution could be for you.

您可以通过以下方式修改代码(已更新为包含setWebViewClient):

You could modify your code this way (Updated to include setWebViewClient):

WebView myMsg = new WebView(context);

AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setIcon(R.drawable.ic_launcher);
builder.setView(myMsg);
builder.setTitle("Some Title");
final AlertDialog dialog = builder.create();

// hook a listener for page load complete
WebViewClient webviewclient = new WebViewClient() {
    @Override  
    public void onPageFinished(WebView view, String url) {
        super.onPageFinished(view, url);
        dialog.show(); // show the dialog here
    }
};
myMsg.setWebViewClient(webviewclient);

myMsg.loadUrl("file:///android_asset/page.html");
WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();
lp.windowAnimations = R.style.AnimateDialog;

//dialog.show(); // do not show until content is truly available
dialog.getWindow().setAttributes(lp);

我已经在装有Android 4.3的Nexus 7上对其进行了测试,但是内容的显示仍然受到系统动画时间的延迟,就像我之前的建议中所讨论的那样,因此您将不得不解决与之前相同的问题Android 4.3.

I have tested it with my Nexus 7 with Android 4.3, but the showing of the content is still delayed by the system animation time just like I had discussed in my previous suggestion so you will have to solve the same problem I had on Android 4.3.

这篇关于对话框动画被webview弄乱了:android bug?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆