SurfaceView中的多任务处理

时间:2014-04-29 19:20:31

标签: android surfaceview

在我的简单代码中,我创建了一个具有随机颜色和不透明度的圆圈,它会移动一段时间然后消失。它按预期工作。

class Circle1Thread extends Thread {

@Override 
public void run() {
    prev_time = System.currentTimeMillis();
    while(running) {
            Canvas canvas = null;

                switch(state) {
                case States.NONE:
                    try {
                        canvas = surfaceHolder.lockCanvas();
                        if (!counted) {
                            width = canvas.getWidth();
                            height = canvas.getHeight();
                            counted = true;
                        }
                        got_direction = false;
                        paint = new Paint(); 
                        paint.setColor(States.getRandomColor()); // random color
                        paint.setAlpha(random.nextInt(250)); //random alpha
                        radius = (float) random.nextInt(width/4); // random radius
                        x = random.nextInt((int)(width-2*radius)) + radius; 
                        y = random.nextInt((int)(height-2*radius)) + radius; // random x & y positions
                        current_radius = 1;
                        synchronized(surfaceHolder) {
                            canvas.drawColor(Color.WHITE);
                            canvas.drawCircle(x, y, current_radius, paint); // draw almost unseen circle, than increasing its radius
                        }
                    }
                    finally {
                        if (canvas!= null) surfaceHolder.unlockCanvasAndPost(canvas);
                        else Log.d("canvas","canvas is null in thread 1");
                        state = States.APPEARING;
                        creation_time = System.currentTimeMillis();
                    }
                    break;
                case States.APPEARING:
                    if ((now_time = System.currentTimeMillis() - prev_time) >=15) {
                        current_radius = current_radius +3;
                        if (current_radius >=radius) {
                            state = States.MOVING;
                            break;
                        }
                    }
                        try {
                            canvas = surfaceHolder.lockCanvas();
                            synchronized (surfaceHolder) {
                                canvas.drawColor(Color.WHITE);
                                canvas.drawCircle(x, y, current_radius, paint);
                            }
                        }
                        finally {
                            surfaceHolder.unlockCanvasAndPost(canvas);
                            prev_time = now_time;
                        }
                    break;
                case States.MOVING:
                    if (!got_direction) {
                        direction = States.getDirection();
                        got_direction = true;
                    }
                    if (((now_time = System.currentTimeMillis()) - prev_time) >=15) {
                        switch(direction) {
                        case States.DIRECTION_DOWN:
                            y = y+2;
                            break;
                        case States.DIRECTION_LEFT:
                            x =x-2;
                            break;
                        case States.DIRECTION_RIGHT:
                            x = x+2;
                            break;
                        case States.DIRECTION_UP:
                            y = y-2;
                            break;
                        }
                    }
                    if((now_time - creation_time) >=4000) {
                        state = States.DISAPPEARING;
                        break;
                    }
                    try {
                        canvas = surfaceHolder.lockCanvas();
                        synchronized (surfaceHolder) {
                            canvas.drawColor(Color.WHITE);
                            canvas.drawCircle(x, y, radius, paint);
                        }
                    }
                    finally {
                        surfaceHolder.unlockCanvasAndPost(canvas);
                        prev_time = now_time;
                    }
                    break;
                case States.DISAPPEARING:
                    if ((now_time = System.currentTimeMillis() - prev_time) >=15) {
                        current_radius = current_radius -3;
                        if (current_radius <=1) {
                            state = States.NONE;
                            break;
                        }
                    }
                        try {
                            canvas = surfaceHolder.lockCanvas();
                            synchronized (surfaceHolder) {
                                canvas.drawColor(Color.WHITE);
                                canvas.drawCircle(x, y, current_radius, paint);
                            }
                        }
                        finally {
                            surfaceHolder.unlockCanvasAndPost(canvas);
                            prev_time = now_time;
                        }
                    break;
                }
            }
        }
    }

但我不知道如何创建几个圆圈,这些圆圈的行为方式类似。我尝试这样做,但它无法正常工作。

class Circle1Thread extends Thread {

public Circle1Thread(SurfaceHolder holder) {
    surfaceHolder = holder;
    random = new Random();
    canvas = surfaceHolder.lockCanvas();
    canvas.drawColor(Color.WHITE);
    surfaceHolder.unlockCanvasAndPost(canvas);
}

public void setRunning(boolean run) {
    running = run;
}

@Override 
public void run() {
    while(running) {
        for(int count=0; count<5; count++) {
            canvas = surfaceHolder.lockCanvas();
            canvas.drawColor(Color.WHITE);
            surfaceHolder.unlockCanvasAndPost(canvas);
            Log.d("canvas" ,"operating in count = "+String.valueOf(count));
            Canvas canvas = null;
            if (!was_created[count]) prev_time[count] = System.currentTimeMillis();
                switch(state[count]) {
                case States.NONE:
                    try {
                        canvas = surfaceHolder.lockCanvas();
                        if (!counted) {
                            width = canvas.getWidth();
                            height = canvas.getHeight();
                            counted = true;
                        }
                        got_direction[count] = false;
                        paint[count] = new Paint(); 
                        paint[count].setColor(States.getRandomColor()); // random color
                        paint[count].setAlpha(random.nextInt(250)); //random alpha
                        radius[count] = (float) random.nextInt(width/4); // random radius
                        x[count] = random.nextInt((int)(width-2*radius[count])) + radius[count]; 
                        y[count] = random.nextInt((int)(height-2*radius[count])) + radius[count]; // random x & y positions
                        current_radius[count] = 1;
                        was_created[count] = true;
                        synchronized(surfaceHolder) {
                            Log.d("canvas","color drawn in count = "+String.valueOf(count));
                            canvas.drawCircle(x[count], y[count], current_radius[count], paint[count]); // draw almost unseen circle, than increasing its radius
                        }
                    }
                    finally {
                        if (canvas!= null) surfaceHolder.unlockCanvasAndPost(canvas);
                        else Log.d("canvas","canvas is null in thread 1");
                        state[count] = States.APPEARING;
                        creation_time[count] = System.currentTimeMillis();
                    }
                    break;
                case States.APPEARING:
                    if (((now_time[count] = System.currentTimeMillis()) - prev_time[count]) >=15) {
                        current_radius[count] = current_radius[count] +3;
                        Log.d("radius",String.valueOf(radius[count])+" in count = "+String.valueOf(count));
                        if (current_radius[count] >=radius[count]) {
                            state[count] = States.MOVING;
                            break;
                        }
                    }
                        try {
                            canvas = surfaceHolder.lockCanvas();
                            synchronized (surfaceHolder) {
                                Log.d("canvas","appearing in count = "+String.valueOf(count)+", radius = "+String.valueOf(current_radius[count]));
                                canvas.drawCircle(x[count], y[count], current_radius[count], paint[count]);
                            }
                        }
                        finally {
                            surfaceHolder.unlockCanvasAndPost(canvas);
                            prev_time[count] = now_time[count];
                        }
                    break;
                case States.MOVING:
                    if (!got_direction[count]) {
                        direction[count] = States.getDirection();
                        got_direction[count] = true;
                    }
                    if (((now_time[count] = System.currentTimeMillis()) - prev_time[count]) >=15) {
                        switch(direction[count]) {
                        case States.DIRECTION_DOWN:
                            y[count] = y[count]+2;
                            break;
                        case States.DIRECTION_LEFT:
                            x[count] =x[count]-2;
                            break;
                        case States.DIRECTION_RIGHT:
                            x[count] = x[count]+2;
                            break;
                        case States.DIRECTION_UP:
                            y[count] = y[count]-2;
                            break;
                        }
                    }
                    if((now_time[count] - creation_time[count]) >=4000) {
                        state[count] = States.DISAPPEARING;
                        break;
                    }
                    try {
                        canvas = surfaceHolder.lockCanvas();
                        synchronized (surfaceHolder) {
                            Log.d("canvas","moving in count = "+String.valueOf(count));
                            canvas.drawCircle(x[count], y[count], radius[count], paint[count]);
                        }
                    }
                    finally {
                        surfaceHolder.unlockCanvasAndPost(canvas);
                        prev_time = now_time;
                    }
                    break;
                case States.DISAPPEARING:
                    if ((now_time[count] = System.currentTimeMillis() - prev_time[count]) >=15) {
                        current_radius[count] = current_radius[count] -3;
                        if (current_radius[count] <=1) {
                            state[count] = States.NONE;
                            break;
                        }
                    }
                        try {
                            canvas = surfaceHolder.lockCanvas();
                            synchronized (surfaceHolder) {
                                Log.d("canvas","disappearing in count = "+String.valueOf(count));
                                canvas.drawCircle(x[count], y[count], current_radius[count], paint[count]);
                            }
                        }
                        finally {
                            surfaceHolder.unlockCanvasAndPost(canvas);
                            prev_time[count] = now_time[count];
                        }
                    break;
                }
            }
        }
    }}

有人可以帮忙吗?

0 个答案:

没有答案