【发布时间】:2013-07-30 01:08:09
【问题描述】:
我用 Enter 和 Exit 慢速动画制作了一个对话框。但是该对话框包含一个 webview myMsg(加载本地文件,因此没有延迟)并弄乱了动画。
使用下面的代码(无 web 视图),对话框可以完美运行,在 Enter 和 Exit 处都具有动画效果。但是,如果我取消注释
行//builder.setView(myMsg),Exit 动画仍然可以完美运行,但是 Enter 动画没有执行(或执行得太快)。有趣的是,如果我将应用程序最小化并再次将其最大化,则 Enter 对话框动画执行得很好。
这就像 webview 加载弄乱了 Enter 动画。我尝试在加载 webview 后显示对话框,结果相同。
这不是疯了吗?关于发生了什么以及如何解决它的任何线索?任何帮助将不胜感激。
这是代码的相关部分:
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 动画根本不起作用):
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
我还尝试使用 Dialog 而不是 AlertDialog,使用 xml 布局,遇到相同的问题(也尝试使用 WebView.loadData 而不是 WebView.loadUrl,同样的问题):
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 布局文件:
<?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
我刚刚意识到将TextView 与TextView.setText(Html.fromHTML...) 一起使用而不是WebView 时会发生同样的问题。此外,如果我在dialog.show() 之后添加dialog.getWindow().setLayout(600,800) ,则动画按预期执行。所以看来问题是在事先不知道对话框大小的情况下不执行动画?
【问题讨论】:
标签: android android-webview android-animation android-alertdialog