I use TransitionDrawable two transitions between two GradientDrawables, and I set this as the button background. This works great when switching between two ColorDrawables, but when I try to do this with GradientDrawables, I get very strange results.
On my 2.3.5 device everything works peachy. On my device 4.2.2, the buttons just take the first gradient and don't redraw at all.
If I add a call to view.refreshDrawableState () or view.setBackgroundDrawable (drawable) after I started the transition, it will update, but the situation will become even stranger:
- some buttons work correctly
- some buttons flicker at points during transition
- one button, for some reason, goes to black, and not to the second gradient.
I loop the transition with a recursive message into the view, but I donโt think the problem is because I get the same problems when I do not.
Does anyone know what I can do to fix this? Here is the relevant code:
private static void highlightView(View view, Context context) { TransitionDrawable drawable; GradientDrawable d1 = new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { HIGHLIGHT_COLOR_1, HIGHLIGHT_COLOR_G1 }); d1.setCornerRadius(3f); GradientDrawable d2 = new GradientDrawable(Orientation.TOP_BOTTOM, new int[] { HIGHLIGHT_COLOR_2, HIGHLIGHT_COLOR_G2 }); d2.setCornerRadius(3f); drawable = new TransitionDrawable(new Drawable[] { d1, d2 }); view.setBackgroundDrawable(drawable); drawable.setCrossFadeEnabled(true); animateBackground(drawable, true, view); } private static void animateBackground(final TransitionDrawable drawable, final boolean forward, final View view) { if (view.getBackground() != drawable) return; if (forward) { drawable.startTransition(HIGHLIGHT_CYCLE / 2); } else { drawable.reverseTransition(HIGHLIGHT_CYCLE / 2); } view.postDelayed(new Runnable() { @Override public void run() { animateBackground(drawable, !forward, view); } }, HIGHLIGHT_CYCLE / 2); view.refreshDrawableState();
source share