문제

I have

ArrayList<ColorDrawable> colors = new ArrayList<ColorDrawable>();
for(int i = 0; i = intList.size(); i++){ //some list of ints
    colors.add(new ColorDrawable(intList.get(i)));

I want to fade from one color in the list to another, using a SurfaceView + Canvas method. Here's my attempt:

 public void run() {
    int maxIndex = colors.size() - 1;
        while (isItOK) {
            for (int i = 0; i <= maxIndex; i++) {
                int color = colors.get(i).getColor();
                int nextColor = (i == maxIndex) ? colors.get(0).getColor() : colors.get(i + 1).getColor();

                if(color < nextColor) {
                    for(; color <= nextColor; color++) {
                        Canvas c = holder.lockCanvas();
                        c.drawColor(color);
                        holder.unlockCanvasAndPost(c);
                    }
                }


                if(color > nextColor) {
                    for(; color >= nextColor; color--) {
                        Canvas c = holder.lockCanvas();
                        c.drawColor(color);
                        holder.unlockCanvasAndPost(c);
                    }
                }
            }

        }
    }

I feel like this should work as is and fade from the first color the second to the third, and so on...and end up looping, but instead, it starts from the first color, and fades to some unrelated color, over and over. (I've tested different data as well). This is my first time working with a SurfaceView so I'm not sure if how I have done my canvas method is correct. Using Log.d, I saw that once it gets into one of the inner for loops (the ones with the "if" statements before them), it will not leave that for loop....which doesn't make sense to me but I figure it has something to do with the canvas and holder. Help?

도움이 되었습니까?

해결책

I'm not sure, if i understood you corretly, if not, please let me know.

In my understanding you want to: loop through the colors and set each time the backgroundcolor as the i th element of your colors list.

UPDATE: Note, that i havent tested it jet!

int currentIndex = 0;
int nextIndex = 0;

while (isItOK) 
{
    nextIndex = (currentIndex + 1) % colors.size();

    int currentColor = colors.get(currentIndex).getColor();
    int nextColor = colors.get(nextIndex).getColor();
    while(currentColor != nextColor)
    {
        //extract red, green, blue, alpha from the current color

        int r = Color.red(currentColor); //android.graphics.Color
        int g = Color.green(currentColor);
        int b = Color.blue(currentColor);
        int a = Color.alpha(currentColor);

        //extract the same from nextColor
        int nr = Color.red(nextColor);
        int ng = Color.green(nextColor);
        int nb = Color.blue(nextColor);
        int na = Color.alpha(nextColor);

        //get currentColors values closer to nextColor 
        r = (r<nr) ? r+1 : ((r>nr) ? r-1 : r);
        g = (g<ng) ? g+1 : ((g>ng) ? g-1 : g);
        b = (b<nb) ? b+1 : ((b>nb) ? b-1 : b);
        a = (a<na) ? a+1 : ((a>ar) ? a-1 : a);

        // generate currentColor back to a single int
        currentColor = Color.argb(a,r,g,b);

        // paint it 
        Canvas canvas = holder.lockCanvas();
        canvas.drawColor(currentColor );
        holder.unlockCanvasAndPost(canvas);
    }
    currentIndex = (currentIndex + 1) % colors.size();
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top