清除android canvas的内容

时间:2012-03-18 12:40:16

标签: android android-canvas

这是我的代码:

public class RT extends Activity implements OnTouchListener{

    DrawPanel dp;
    private ArrayList<Path> pointsToDraw = new ArrayList<Path>();
    private Paint mPaint;
    Path path;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        dp = new DrawPanel(this);
        dp.setOnTouchListener(this);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setColor(Color.WHITE);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(30);

        TextView tv1 = new TextView(this);  
        tv1.setText("Generated Text Goes Here");  
        tv1.setTextSize(30);  
        tv1.setTextColor(Color.WHITE);
        tv1.setGravity(Gravity.BOTTOM);
        tv1.setId(R.id.TGText);

        LinearLayout ll = new LinearLayout(this);
        ll.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));


        Button b1 = new Button(this);  
        b1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));  
        b1.setTextSize(20);  
        b1.setText("Generate");  
        b1.setTextColor(Color.WHITE);
        b1.setId(R.id.TGButton);
        b1.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
            }
        });

        ll.addView(b1);

        FrameLayout fl = new FrameLayout(this);  
        fl.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));  
        fl.addView(dp);  
        fl.addView(tv1);  
        fl.addView(ll);
        setContentView(fl);  

    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        dp.pause();
    }



    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        dp.resume();
    }



    public class DrawPanel extends SurfaceView implements Runnable{

        Thread t = null;
        SurfaceHolder holder;
        boolean isItOk = false ;

        public DrawPanel(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            holder = getHolder();
        }

        @Override
        public void run() {
            // TODO Auto-generated method stub
            while( isItOk == true){

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

                Canvas c = holder.lockCanvas();
                c.drawARGB(255, 0, 0, 0);
                onDraw(c);
                holder.unlockCanvasAndPost(c);
            }
        }

        @Override
        protected void onDraw(Canvas canvas) {
            // TODO Auto-generated method stub
            super.onDraw(canvas);
            synchronized(pointsToDraw)
            {
                for (Path path : pointsToDraw) {
                    canvas.drawPath(path, mPaint);
            }
            }
        }

        public void pause(){
            isItOk = false;
            while(true){
                try{
                    t.join();
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
                break;
            }
            t = null;
        }

        public void resume(){
            isItOk = true;  
            t = new Thread(this);
            t.start();

        }

        public void clearCanvas(){
            Canvas c = holder.lockCanvas();
            c.drawColor(Color.BLACK);
            holder.unlockCanvasAndPost(c);
            invalidate();
        }



    }


    @Override
    public boolean onTouch(View v, MotionEvent me) {
        synchronized(pointsToDraw)
        {
        // TODO Auto-generated method stub
        if(me.getAction() == MotionEvent.ACTION_DOWN){
            path = new Path();
            path.moveTo(me.getX(), me.getY());
            //path.lineTo(me.getX(), me.getY());
            pointsToDraw.add(path);
        }else if(me.getAction() == MotionEvent.ACTION_MOVE){
            path.lineTo(me.getX(), me.getY());
        }else if(me.getAction() == MotionEvent.ACTION_UP){
            //path.lineTo(me.getX(), me.getY());
        }
        }
        return true;

    }

    @Override
    public void onBackPressed() {
       return;
    }


}

但它不起作用。请帮忙! : - )

1 个答案:

答案 0 :(得分:2)

onDraw()会不断被调用,每次发生这种情况时,都会绘制所有pointsToDraw

如果您要清除画布,则必须通过清除pointsToDraw中的clearCanvas()来阻止其发生。