2012-02-03 98 views
3

我们的应用程序大量使用webviews。在ICS上进行测试时,我们注意到我们需要设置应用程序属性hardwareAccelerated="true"。但是,这样做会在我们的应用程序中导致一些其他错误,其中大部分我们已经修复。我们仍然有问题让我们的“滑入式”动画工作。如果没有任何代码更改,动画只会执行“显示”类型的动画而不是滑动。我们尝试了几个不同的东西:在ICS上硬件加速的webview幻灯片动画闪烁

  1. 添加一个小的alpha转换(.99-> 1)。这会将“揭示”更改为“幻灯片”,但有时会在屏幕上产生一些奇怪的痕迹。
  2. 在动画过程中使用硬件层。这不一致。
  3. 在动画过程中使用软件层。这可以起作用,但在滑入动画首次完成后导致缓慢重绘。

方法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" /> 

回答

7

好吧,如果您尝试支持ICS并继续使用,您可以使用新的动画API,因为它们更容易使用,我相信会非常流畅。

这里是看看这个API两个链接:

thisthis

如果你想使用这个API的旧版本,尝试NineOldAndroids

编辑:尝试将WebView的图层设置为软件一:

web.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 

它停止了闪烁,但您在视图内部失去了硬件加速。我不确定,但我想对于像动画这样的东西,视图仍然会被视为硬件加速,因为它的“容器”仍然存在。但我也可能完全错误。

+1

ObjectAnimator有同样的问题。我认为这是一个android动画+ webviews +硬件加速的bug。我想知道是否有人找到解决方法。 – slushi 2012-02-03 17:52:14

+0

我明白了。我在这里做了一个小测试,可以确认即使使用新的API也会发生。强制图层成为软件确实解决了问题,请参阅我的编辑,是否可以帮助您。 – 2012-02-03 18:22:50

+0

是的,这将解决闪烁问题,但在web视图中的滚动性能变得不可接受 - 这是我们将硬件加速转换为的主要原因之一......看起来像在动画结束时调用以恢复图层类型到NONE导致闪烁。 – slushi 2012-02-03 19:07:44

3

它使用

web.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 

闪烁的问题得到了解决

+0

web.setLayerType(View.LAYER_TYPE_SOFTWARE,null); webview.setBackgroundColor(0); Harsha 2014-09-24 11:52:27

+0

其工作像我一样没有flikkering – Harsha 2014-09-24 11:53:45

0

从你的描述好像这是同一个问题我有,但我不能完全肯定这工作对我罚款。起初我用你的解决方案,但现在我有一个更好的工作。我简单地设置

android:alwaysDrawnWithCache="true" 

在网页上查看。这将摆脱所有闪烁和错误的绘图。

我希望它有帮助!

+0

没有为我工作,也根据android文档,这个标志没有任何效果,如果你的应用程序是硬件加速。我的猜测是你离开你的应用程序在S/W模式... – slushi 2012-06-09 23:05:05