在android中绘制动画圈

时间:2017-04-04 11:47:38

标签: android

我正在尝试创建一个圆形动画,但是屏幕不会自行刷新,它会在不同的位置绘制许多圆圈而不是绘制运动。我想以这种方式使用它,而不是使用invalidate()方法,因为我想将它用于实时应用程序。

以下是代码:

public class activity_layout_animation extends SurfaceView implements Runnable {

    Thread thread;
    boolean CanDraw = false;

    Paint red_fill;

    int circle_x, circle_y;

    Canvas canvas;
    SurfaceHolder surfaceHolder;


    public activity_layout_animation(Context context){
        super(context);
        surfaceHolder = getHolder();
        circle_x= 130;
        circle_y=130;
    }
    @Override
    public void run(){

        preparePaint();

        while(CanDraw){

            if ( !surfaceHolder.getSurface().isValid()){
                continue;
            }

            canvas = surfaceHolder.lockCanvas();
            motionCircle(10);
            canvas.drawCircle(circle_x,circle_y,50, red_fill);
            surfaceHolder.unlockCanvasAndPost(canvas);

        }
    }

    public void pause(){

        CanDraw = false;

        while(true) {
            try {
                thread.join();
                break;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        thread = null;
    }

    public void resume(){
        CanDraw = true;
        thread = new Thread(this);
        thread.start();
    }

    private void preparePaint(){
        red_fill = new Paint();
        red_fill.setColor(Color.RED);
        red_fill.setStyle(Paint.Style.FILL);
    }

    private void motionCircle (int speed){

        if ( (circle_y == 130) && ( circle_x < 650) ){
            circle_x = circle_x + speed;
        }
        if ( (circle_y < 650) && ( circle_x ==650) ){
            circle_y = circle_y + speed;
        }
        if ( (circle_y == 650) && ( circle_x > 130) ){
            circle_x = circle_x - speed;
        }
        if ( (circle_y > 130) && ( circle_x== 130) ){
            circle_y = circle_y - speed;
        }
    }


}
谢谢你!

1 个答案:

答案 0 :(得分:0)

postInvalidate()怎么样 - 你可以从后台线程调用它。您也可以致电canvas.drawColor(backgroundColor)以清除之前图纸中的整个画布。