0

Here's my code:

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;
    }


}

But it is not working. Kindly help! :-)

3
  • "But it is not working." You mean asking questions that people can answer?
    – zapl
    Mar 18, 2012 at 13:01
  • No. my clearCanvas method does set the Canvas black for a second, and then redraws the contents all over. Have a look now. Mar 18, 2012 at 13:02
  • 1
    because you onDraw new contents all the time. Clear the pointsToDraw or so.
    – zapl
    Mar 18, 2012 at 13:06

1 Answer 1

2

onDraw() is constantly called and each time that happens you draw all those pointsToDraw.

If you want to to clear the canvas then you have to stop that from happening for example by clearing the pointsToDraw in your clearCanvas().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.