Prevent GradientDrawable to redraw while animating
I have set a GradientDrawable as background. While doing some 3d transaction (especially at the Y axis) the GradientDrawable redraws it self and the famerate is not good.
- I cannot cache the view that the drawing is set as background because i wa开发者_JAVA百科nt other views on top of that to animate.
- I cannot put another view just for the background and cache that view cause then that view only beeing fullscreen will cause the same problems.
- I cannot use windowsDecorator for the background.
So what i want is a way to stop the GradientDrawable to try to redraw itself during the animation. I think jumpToCurrentState()
does what i want but i want it to work on API level >= 8 and jumpToCurrentState()
is only for API level >=11. Also i wish i knew what jumpToCurrentState does exactly to port it to the API lvl 8 but can't find the function anywhere in the drawable(source not released?)
So is there any way to cache the drawable it self and not the view containing that Drawable?.
The last resort will be to not draw the background during animations but i really really want to avoid that.
I ended up creating a bitmap caching system for the GradientDrawable it self. (Didn't want to cache the view that has this drawable as a background).
This gives a great speed boost for animations.
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);
}
}
精彩评论