最近拿到设计师给引导页效果,感觉不错,背景渐变,还有 Parallax scrolling,不过后面的 Parallax scrolling 没有做只做了滑动渐变有点可惜!APK下载 ,其实思路就是 根据 ViewPage的滑动距离去设置背景色(根据滑动距离计算),颜色计算


//两颜色根据百分比计算过度颜色
private  int calcColor(int startColor,int stopColor,int percent)
{
        int tempColor=0;
        float r=0,g=0,b=0;
        int start_rgb[]=retrieveRGBComponent(startColor);//retrieveRGBComponent,0xecf0f1转为RGB颜色。
        int stop_rgb[]=retrieveRGBComponent(stopColor);
        r=((float)stop_rgb[0]-(float)start_rgb[0])/100f;
        g=((float)stop_rgb[1]-(float)start_rgb[1])/100f;
        b=((float)stop_rgb[2]-(float)start_rgb[2])/100f;

        stop_rgb[0] =(int)(start_rgb[0]+r*percent);
        stop_rgb[1] =(int)(start_rgb[1]+g*percent);
        stop_rgb[2] =(int)(start_rgb[2]+b*percent);

        tempColor=generateFromRGBComponent(stop_rgb);//generateFromRGBComponent 与retrieveRGBComponent相反

        return Color.argb(255,stop_rgb[0],stop_rgb[1],stop_rgb[2]);
}
private  int[] retrieveRGBComponent(int color)
{
   int[] rgb = new int[3];
   rgb[0] = (color & 0x00ff0000) >> 16;
   rgb[1] = (color & 0x0000ff00) >> 8;
   rgb[2] = (color & 0x000000ff);
   return rgb;
}
private  int generateFromRGBComponent(int[] rgb)
{
   if (rgb == null || rgb.length != 3 || rgb[0] < 0 || rgb[0] > 255 || rgb[1] < 0|| rgb[1] > 255 || rgb[2] < 0 || rgb[2] > 255)
       return 0xfffff;
   return rgb[0] << 16 | rgb[1] << 8 | rgb[2];
}

源码下载