我们的应用程序大量使用webviews。在ICS上进行测试时,我们注意到我们需要设置应用程序属性hardwareAccelerated="true"
。但是,这样做会在我们的应用程序中导致一些其他错误,其中大部分我们已经修复。我们仍然有问题让我们的“滑入式”动画工作。如果没有任何代码更改,动画只会执行“显示”类型的动画而不是滑动。我们尝试了几个不同的东西:在ICS上硬件加速的webview幻灯片动画闪烁
- 添加一个小的alpha转换(.99-> 1)。这会将“揭示”更改为“幻灯片”,但有时会在屏幕上产生一些奇怪的痕迹。
- 在动画过程中使用硬件层。这不一致。
- 在动画过程中使用软件层。这可以起作用,但在滑入动画首次完成后导致缓慢重绘。
方法3是最有前途的,但我们还没有想出如何避免重绘。我们已经创建了使用示例项目有一个活动小测试案例:
活动类:
int accelValue = View.LAYER_TYPE_NONE; //hack
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
addClickListenerAnimation(R.id.buttonhl, R.anim.to_left, View.VISIBLE, View.INVISIBLE);
addClickListenerAnimation(R.id.buttonhr, R.anim.to_right, View.VISIBLE, View.INVISIBLE);
addClickListenerAnimation(R.id.buttonsl, R.anim.from_left, View.INVISIBLE, View.VISIBLE);
addClickListenerAnimation(R.id.buttonsr, R.anim.from_right, View.INVISIBLE, View.VISIBLE);
final Button b = (Button) findViewById(R.id.accel);
b.setOnClickListener(
new View.OnClickListener()
{
@Override
public void onClick(View v)
{
accelValue = ++accelValue % 3;
b.setText(accelValue == 0 ? "NONE" : accelValue == 1 ? "S/W" : "H/W");
}
});
}
private void addClickListenerAnimation(int buttonId, final int animId, final int startVis, final int endVis)
{
Button b = (Button) findViewById(buttonId);
b.setOnClickListener(
new View.OnClickListener()
{
@Override
public void onClick(View v)
{
final WebView wv = (WebView) findViewById(R.id.webview);
final View layout = findViewById(R.id.frame);
Animation a = AnimationUtils.loadAnimation(getApplicationContext(), animId);
a.setDuration(500);
a.setAnimationListener(new AnimationListener()
{
@Override
public void onAnimationStart(Animation animation)
{
Log.i("sb", "layer type was " + wv.getLayerType());
Log.i("sb", "llayout layer type was " + layout.getLayerType());
wv.setLayerType(accelValue, null);
Log.i("sb", "setting layer type " + accelValue);
}
@Override
public void onAnimationRepeat(Animation animation)
{
}
@Override
public void onAnimationEnd(Animation animation)
{
wv.setLayerType(View.LAYER_TYPE_NONE, null);
Log.i("sb", "restoring layout layer type " + layout.getLayerType());
}
});
layout.setAnimation(a);
layout.setVisibility(endVis);
}
});
}
@Override
protected void onStart()
{
super.onStart();
((WebView)findViewById(R.id.webview)).loadUrl("http://www.wikipedia.org");
}
from_left.xml动画(其他动画个XML是相似的):
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="true">
<translate android:fromXDelta="-100%"
android:toXDelta="0%"
android:fromYDelta="0%"
android:toYDelta="0%"
android:duration="600"
android:zAdjustment="bottom"
/>
</set>
主.XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/llayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/frame"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="invisible"
>
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</FrameLayout>
<View
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="#FF000000" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="80dp"
android:orientation="horizontal" >
<Button
android:id="@+id/buttonsl"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="show L" />
<Button
android:id="@+id/buttonhl"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="hide L" />
<Button
android:id="@+id/buttonsr"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="show R" />
<Button
android:id="@+id/buttonhr"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="hide R" />
<Button
android:id="@+id/accel"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="NONE" />
</LinearLayout>
</LinearLayout>
清单:
<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.INTERNET" />
ObjectAnimator有同样的问题。我认为这是一个android动画+ webviews +硬件加速的bug。我想知道是否有人找到解决方法。 – slushi 2012-02-03 17:52:14
我明白了。我在这里做了一个小测试,可以确认即使使用新的API也会发生。强制图层成为软件确实解决了问题,请参阅我的编辑,是否可以帮助您。 – 2012-02-03 18:22:50
是的,这将解决闪烁问题,但在web视图中的滚动性能变得不可接受 - 这是我们将硬件加速转换为的主要原因之一......看起来像在动画结束时调用以恢复图层类型到NONE导致闪烁。 – slushi 2012-02-03 19:07:44