// initialize background drawable from xml TransitionDrawable bgDrawable = (TransitionDrawable) ContextCompat.getDrawable(getContext(), R.drawable.animated_background); Drawable[] layerDrawables = new Drawable[bgDrawable.getNumberOfLayers()]; for (int i = 0; i < layerDrawables.length; i++) { layerDrawables[i] = bgDrawable.getDrawable(i); }
// if you want to create background drawable manually final GradientDrawable.Orientation orientation = GradientDrawable.Orientation.TR_BL; layerDrawables = new Drawable[]{ new GradientDrawable(orientation, newint[]{0xFFFF0000, 0xFF00FF00}), // red to green new GradientDrawable(orientation, newint[]{0xFF0000FF, 0xFF00FF00}), new GradientDrawable(orientation, newint[]{0xFF0000FF, 0xFFFFFF00}), new GradientDrawable(orientation, newint[]{0xFFFF00FF, 0xFFFFFF00}), }; bgDrawable = new TransitionDrawable(layerDrawables); bgDrawable.setId(0, R.id.bg_even); bgDrawable.setId(1, R.id.bg_odd);
if ((newIdx % 2) == 0) { // next page is even number bgDrawable.setDrawableByLayerId(R.id.bg_even, nextDrawable); bgDrawable.reverseTransition(duration); // odd -> even } else { // next page is odd number bgDrawable.setDrawableByLayerId(R.id.bg_odd, nextDrawable); bgDrawable.startTransition(duration); // even -> odd } }