2011-10-10 30 views
0

我已將GradientDrawable設置爲背景。在做一些3d交易(特別是在Y軸)時,GradientDrawable重繪它自己,並且這個虛名並不好。防止GradientDrawable在動畫製作時重繪

  • 我無法緩存的繪圖設置爲背景,因爲 我想最重要的是其他視圖以動畫的看法。
  • 我不能把另一個視圖只是爲了背景和緩存該視圖 原因然後,該視圖只有全屏將導致相同的 問題。
  • 我無法使用windowsDecorator作爲背景。

所以我想要的是一種方法來阻止GradientDrawable嘗試在動畫過程中重繪自己。我認爲jumpToCurrentState()做了我想要的,但我希望它在API級別> = 8上工作,並且jumpToCurrentState()僅適用於API級別> = 11。另外,我希望我知道jumpToCurrentState會將它移植到API lvl 8中,但無法找到drawable中的任何位置的功能(源不公開)而不是包含那個Drawable的視圖?

最後的手段將不會在動畫中繪製背景,但我真的很想避免這種情況。

+0

你試圖延長GradientDrawable和延緩與Drawable.Callback.scheduleDrawable平局(...),從未使用過它自己,但它可能會在這裏工作 – bgs

+0

嗯,我會嘗試,now.scheduleDrawable塊可繪製到渲染到一個新的狀態,直到它觸發? – weakwire

+1

可能會忽略繪製並注入自己的邏輯,設置時= 0 ...如果我理解正確... – bgs

回答

0

我最終爲GradientDrawable創建了一個位圖緩存系統。 (不想緩存以此drawable作爲背景的視圖)。

這給動畫提供了很大的速度提升。

public class PGradientDrawable extends GradientDrawable { 

    private int firstColor; 
    private int secondColor; 
    private Paint cachePaint = new Paint(); 
    private Canvas cacheCanvas = new Canvas(); 
    private Bitmap bitmap; 

    public PGradientDrawable(Orientation angle, int[] intarray) { 
     super(angle, intarray); 
     firstColor = intarray[0]; 
     secondColor = intarray[1]; 
    } 

    public boolean isBackground = false; 
    boolean firstTime = true; 

    public void buildCache(int width, int height) { 
     if (bitmap != null) 
      bitmap.recycle(); 
     try { 
      bitmap = Bitmap 
        .createBitmap(width, height, Bitmap.Config.ARGB_8888); 
      bitmap.setDensity(context.getResources().getDisplayMetrics().densityDpi); 
      cacheCanvas.setBitmap(bitmap); 
      // TODO shader must be what the GradientDrawable is,type 
      // orientation.. 
      cachePaint.setShader(new LinearGradient(0, 0, width, height, 
        firstColor, secondColor, Shader.TileMode.CLAMP)); 
      cacheCanvas.drawPaint(cachePaint); 

     } catch (OutOfMemoryError e) { 
      // clear the bitmap force draw() to repaint 
      bitmap = null; 
     } 

    } 

    @Override 
    public void draw(Canvas canvas) { 
     if (bitmap != null) { 
      // Draw the bitmap 
      canvas.drawBitmap(bitmap, 0, 0, null); 
      return; 
     } 
     super.draw(canvas); 
    } 

}