2015-03-25 49 views
0

我有一个游戏,我希望背景的屏幕颜色每10点从一种颜色过渡到另一种颜色。下面是我创建尝试的方法:如何实现背景屏幕颜色转换?

private void backgroundColorswitch(Rectangle rect) { 
    int color = mainView.getSolidColor(); /*I am trying to get the mainView's color for the transition's start color*/ 
    int red = (color >> 16) & 0xFF; 
    int green = (color >> 8) & 0xFF; 
    int blue = (color >> 0) & 0xFF; 
    int alpha = (color >> 24) & 0xFF; 
    colorFade = ObjectAnimator.ofObject(mainView, "backgroundColor", new ArgbEvaluator(), Color.argb(alpha, red, green, blue), rect.getColor() - 1000000); /*I want the transition to end on a faded version of the color of the rectangles that also change every 10 points*/ 
    colorFade.setDuration(2000); //length of transition 
    mainView.post(new Runnable() { 
     @Override 
     public void run() { 
      colorFade.start(); //Runs the animation 
     } 
    }); 
} 

的每10个部分的照顾,但我想知道是否有更好的方法来进行此事。如何获取FrameLayout的颜色,因为颜色变化结束的颜色是正确的,但是当我使用mainView.getSolidColor()获取布局的当前颜色时,过渡将以灰白色而不是布局的当前颜色开始。

任何解决方案或建议非常感谢!

回答

1

如果FrameLayout背景是纯色(它应该是你的情况),那么你就可以得到它的使用下面的代码

Drawable background = mainView.getBackground(); 
int color = ((ColorDrawable) background).getColor(); 
颜色