1

在 textview 上应用 TransitionDrawable 时出现 stackoverflow 错误:

private static void applyAnimationTransitionBackground(String descriptorValue, final Resources resources, View view){
Drawable currentBackground;
TransitionDrawable td;

if (view!=null){

    //Get current background or transparent color
    currentBackground = view.getBackground();
    if (currentBackground==null){
        currentBackground = new ColorDrawable(android.R.color.transparent);
    }

    //Create transition depend on trend
    if (descriptorValue.equals("1")){
        td= new TransitionDrawable (new Drawable []{currentBackground, new ColorDrawable(resources.getColor(R.color.positive_animation))}); 
    }else if (descriptorValue.equals("2")){
        td= new TransitionDrawable (new Drawable []{currentBackground, new ColorDrawable(resources.getColor(R.color.neutral_animation))});
    }else{
        td= new TransitionDrawable (new Drawable []{currentBackground, new ColorDrawable(resources.getColor(R.color.negative_animation))});
    }

    //Apply animation
    if (td!=null){
        view.setBackgroundDrawable(td);
        td.startTransition(500);
        //td.reverseTransition(500);            
    }else{
        Logger.error("applyAnimationTransitionBackground: TransitionDrawable is NULL");
    }   

}

}

02-12 17:00:12.899: I/System.out(11217): ---> M SAN nuevo valor 1,52%
02-12 17:00:12.907: I/System.out(11217): ---> M SAN nuevo valor SANTANDER
02-12 17:00:12.907: W/Resources(11217): Converting to string: TypedValue{t=0x11/d=0x55 a=-1}
02-12 17:00:12.907: W/Resources(11217): Converting to string: TypedValue{t=0x10/d=0xfffffffe a=-1}
02-12 17:00:12.915: I/System.out(11217): ---> M SAN nuevo valor 1,52%
02-12 17:00:12.915: I/System.out(11217): ---> M SAN nuevo valor SANTANDER
02-12 17:00:12.915: W/Resources(11217): Converting to string: TypedValue{t=0x11/d=0x55 a=-1}
02-12 17:00:12.915: W/Resources(11217): Converting to string: TypedValue{t=0x10/d=0xfffffffe a=-1}
02-12 17:00:12.915: I/dalvikvm(11217): threadid=1: stack overflow on call to Landroid/view/GLES20Canvas;.setupModifiers:IL
02-12 17:00:12.915: I/dalvikvm(11217):   method requires 40+20+20=80 bytes, fp is 0x4016b324 (36 left)
02-12 17:00:12.915: I/dalvikvm(11217):   expanding stack end (0x4016b300 to 0x4016b000)
02-12 17:00:12.915: I/dalvikvm(11217): Shrank stack (to 0x4016b300, curFrame is 0x4016e420)
02-12 17:00:12.915: D/AndroidRuntime(11217): Shutting down VM
02-12 17:00:12.915: W/dalvikvm(11217): threadid=1: thread exiting with uncaught exception (group=0x40bd91f8)
02-12 17:00:12.961: E/AndroidRuntime(11217): FATAL EXCEPTION: main
02-12 17:00:12.961: E/AndroidRuntime(11217): java.lang.StackOverflowError
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.view.GLES20Canvas.drawRect(GLES20Canvas.java:942)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.view.GLES20RecordingCanvas.drawRect(GLES20RecordingCanvas.java:226)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.view.GLES20Canvas.drawRect(GLES20Canvas.java:952)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.view.GLES20RecordingCanvas.drawRect(GLES20RecordingCanvas.java:232)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.ColorDrawable.draw(ColorDrawable.java:70)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
02-12 17:00:12.961: E/AndroidRuntime(11217):    at android.graphics.drawable.TransitionDrawable.draw(TransitionDrawable.java:195)
4

1 回答 1

0

我几乎可以肯定你会调用applyAnimationTransitionBackground()一个循环,例如onDrawFrame()a Renderer(我这么说只是因为我在那里看到了 GL ES 的用法,但它可能是某种动画或其他东西)。

我这么认为是因为您设置了view. 首先背景view是两种颜色之间的可绘制过渡 - 我们称之为T(C1, C2)。对于每个连续调用,背景view设置为可在以下之间绘制的过渡:

  1. 目前的背景view
  2. 一些颜色可绘制

有效:

  1. 第二次调用后,view背景设置为过渡T(T(C1, C2), C3)
  2. 第三次调用后,view背景设置为 transition T(T(T(C1, C2), C3), C4)
  3. 等等...

实际上,即使在 100 次调用之后,您仍然可以将背景可绘制对象设置为 100 个过渡可绘制对象的大量组合。您可以想象在对您的方法进行 1000 或 10000 次调用之后会发生什么。由于过渡可绘制对象只是将绘图委托给其组件可绘制对象,因此最终会导致堆栈溢出。

检查是否确实如此的最简单方法是将行Logger.info("call");放在applyAnimationTransitionBackground(). 如果您在日志 (LogCat) 中看到大量call消息,那么就是这种情况。

要解决此问题,您只需放弃这种组合可绘制对象的模式。例如,您可以保存用于过渡的最后颜色值及其开始时间。然后,当您需要在当前颜色和新颜色之间创建过渡时,可以使用保存的开始时间和颜色来插入背景的当前颜色。然后,您可以简单地创建一个新的可绘制过渡 - 在当前颜色和新颜色之间。

于 2013-02-12T17:36:18.810 回答