5

I have created a simple demo application in android for drawing ,now can any one say me how can i remove view and clear screen in just one click .I have tried as below:please help me to do it....thanx in advance..!

main.java

package com.example.singletouch;

import com.example.singletouch.R.attr;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
    ImageView pen;
     SingleTouchView mDrawView;

     ImageView remove;
    LinearLayout pens;

    LinearLayout pen1, pen2, pen3, pen4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         mDrawView = (SingleTouchView) findViewById(R.id.myview);


        pen = (ImageView) findViewById(R.id.pen);
        pens = (LinearLayout) findViewById(R.id.linear);
        pens.setVisibility(View.GONE);
        pen1 = (LinearLayout) findViewById(R.id.pen1);
        pen2 = (LinearLayout) findViewById(R.id.pen2);
        pen3 = (LinearLayout) findViewById(R.id.pen3);
        pen4 = (LinearLayout) findViewById(R.id.pen4);
    remove=(ImageView)findViewById(R.id.remove);
        /*
         * pen1.setOnClickListener(this); pen2.setOnClickListener(this);
         * pen3.setOnClickListener(this); pen4.setOnClickListener(this);
         */

        pen.setOnClickListener(new OnClickListener() {

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

                pens.setVisibility(View.VISIBLE);



            }
        });pens.setVisibility(View.GONE);
        pen1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                 mDrawView.setPen(SingleTouchView.DrawingPens.PEN_1);
                pens.setVisibility(View.GONE);


            }
        });
        pen2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                 mDrawView.setPen(SingleTouchView.DrawingPens.PEN_2);
                pens.setVisibility(View.GONE);

            }
        });
        pen3.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                pens.setVisibility(View.GONE);
                 mDrawView.setPen(SingleTouchView.DrawingPens.PEN_3);

            }
        });
        pen4.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                pens.setVisibility(View.GONE);
                 mDrawView.setPen(SingleTouchView.DrawingPens.PEN_4);


            }
        });
        remove.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {


            }
        });


    }

}

SingleTouchView.java

    package com.example.singletouch;

    import java.util.AbstractMap;
    import java.util.Map;
    import java.util.concurrent.ConcurrentLinkedQueue;

    import android.R.color;
    import android.content.Context;
import android.graphics.Bitmap;
    import android.graphics.Canvas;
    import android.graphics.Color;
    import android.graphics.Paint;
    import android.graphics.Path;
import android.graphics.PorterDuff.Mode;
    import android.util.AttributeSet;
    import android.view.MotionEvent;
    import android.view.View;
    import android.widget.ImageView;
import android.widget.Switch;

     public class SingleTouchView extends View{
         public int width;
         public  int height;
            public Bitmap  mBitmap;
            public Canvas  mCanvas;
            public Path    mPath;
            public Paint   mBitmapPaint;
            Context context;

            public Paint circlePaint;
            public Path circlePath;

         public enum DrawingPens {
                PEN_1(6),
                PEN_2(4),
                PEN_3(2),
                PEN_4(1);

                final public Paint mPaint;

                /**
                 * Constructor
                 *
                 * @param width width of stroke
                 * @param color color of stroke
                 */
                private DrawingPens(final int width) {
                    mPaint = new Paint();

                    mPaint.setAntiAlias(true);
                    mPaint.setStrokeWidth(width);
                    //mPaint.setColor(color);
                    mPaint.setStyle(Paint.Style.STROKE);
                    mPaint.setStrokeJoin(Paint.Join.ROUND);
                }


                /**
                 * @return corresponding paint
                 */
                Paint getPaint() {
                    return mPaint;
                }
            }




            public SingleTouchView(final Context context) {
                super(context);

                init(context);
            }

            public SingleTouchView(final Context context, final AttributeSet attrs) {
                super(context, attrs);

                init(context);
            }

            public SingleTouchView(final Context context, final AttributeSet attrs, final int defStyle) {
                super(context, attrs, defStyle);

                init(context);
            }

            /** To store Paint - Path relation */
            // TODO: depending on exact limits, more optimal ways can be found
            private ConcurrentLinkedQueue<Map.Entry<Path, DrawingPens>> mPaths = new ConcurrentLinkedQueue<Map.Entry<Path, DrawingPens>>();

            /** Cached current path, <b>NOTE:</b> this field is tail at mPaths and is used it only for caching, not drawing */
            private Path mCurrentPath;

            /**
             * Inits internal views data, should be called from every constructor
             *
             * @param context {@link Context}
             */
            private void init(final Context context) {
                /* TODO: if some values of paints cannot be determined in static context (in enum),
                   then Paints should be created here and used via EnumMap */

                // Initial pen
                setPen(DrawingPens.PEN_1);

            }



            @Override
            public void onDraw(Canvas canvas){
                // just to draw background
                super.onDraw(canvas);

                // Draw all paths
                for (Map.Entry<Path, DrawingPens> entry : mPaths) {
                    canvas.drawPath(entry.getKey(), entry.getValue().getPaint());
                }
            }

            @Override
            public boolean onTouchEvent(MotionEvent me){
                float eventX = me.getX();
                float eventY = me.getY();

                switch (me.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        mCurrentPath.moveTo(eventX, eventY);
                        return true;
                    case MotionEvent.ACTION_MOVE:
                        mCurrentPath.lineTo(eventX, eventY);
                        break;
                    case MotionEvent.ACTION_UP:
                        break;
                }

                invalidate();

                return true;
            }

            /**
             * Setter for new pen
             *
             * @param pen {@link DrawingPens} to be used for next drawing
             */
            public void setPen(final DrawingPens pen) {
                // put latest item to the queue
                mCurrentPath = new Path();
                mPaths.add(new AbstractMap.SimpleImmutableEntry<Path, DrawingPens>(mCurrentPath, pen));
            }
           public void clear(View v){

           }

        }

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context=".MainActivity" >

    <com.example.singletouch.SingleTouchView
        android:id="@+id/myview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/pen" />

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:background="@drawable/menubar"
        android:padding="2dp"
        android:weightSum="4" >

        <ImageView
            android:id="@+id/pen"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentBottom="true"
            android:layout_weight="1"
            android:gravity="center"
            android:src="@drawable/pen" />

        <ImageView
            android:id="@+id/eraser"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentBottom="true"
            android:layout_weight="1"
            android:src="@drawable/eraser" />

        <ImageView
            android:id="@+id/color"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentBottom="true"
            android:layout_weight="1"
            android:src="@drawable/color" />

        <ImageView
            android:id="@+id/remove"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_alignParentBottom="true"
            android:layout_weight="1"
            android:src="@drawable/remove" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linear"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linearLayout1"
        android:layout_alignParentLeft="true"
        android:background="#eeeeee"
        android:orientation="horizontal"
        android:visibility="gone"
        android:weightSum="4" >

        <LinearLayout
            android:id="@+id/pen1"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center" >

            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:src="@drawable/pen1" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/pen2"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="fill_parent"
                android:src="@drawable/pen2" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/pen3"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/pen3" />
        </LinearLayout>

        <LinearLayout
            android:id="@+id/pen4"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:gravity="center" >

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:src="@drawable/pen4" />
        </LinearLayout>
    </LinearLayout>

</RelativeLayout>
jigar
  • 1,571
  • 6
  • 23
  • 46
  • I'm not sure what you mean, but maybe this can help: http://stackoverflow.com/questions/8181191/how-to-clear-content-on-the-layout –  Jul 25 '13 at 05:30
  • @yser2617212-m posting my xml file so that u will get idea...!i want to clear all the paths i drawn by just one click...! – jigar Jul 25 '13 at 06:32
  • Dear *jigar*, if you want to clear the drawed pathes you could just clear your canvas by setting its color. You don't need to remove the view. In order to clear the content of `mPaths` just assign `@null` to it. The garbage colletector will clean up memory. I'd recommend to add or remove views only, if you have a mutable number of views of same type in your layout, e.g. if you have more than one canvases and you want to remove one of them. – Trinimon Jul 25 '13 at 06:41
  • @Trinimon-can you put some code...brother..! – jigar Jul 25 '13 at 06:54

4 Answers4

5

Jus use ...

mCanvas.drawColor(Color.BLACK);

... for clearing the canvas or any other color you want to have in background. E.g.

remove.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // clear canvas contents  
        mCanvas.drawColor(Color.BLACK);

        // create new path list; old one will be garbage collected 
        ConcurrentLinkedQueue<Map.Entry<Path, DrawingPens>> mPaths = 
                  new ConcurrentLinkedQueue<Map.Entry<Path, DrawingPens>>();

        pens.setVisibility(View.VISIBLE);
    }

p.s.: for removing a view dynamically from its container use removeView(....), e.g.

((ViewGroup)viewToRemove.getParent()).removeView(viewToRemove);

Hope this helps ... Cheers!

Trinimon
  • 13,839
  • 9
  • 44
  • 60
  • @Trinimon-Brother...i successfully clear screen by layout.removeAllViews(); but...after that i am not able to draw again with my pen...! :( – jigar Jul 25 '13 at 06:46
  • Dear *jigar*, if you remove the view it means that the underlying object that contains the canvas will be deleted. So if you want to continue drawing you have to create a new canvas dynamically first. However If you have only one canvas all the time, I don't think you need to remove/delete it. Just clear the painted paths, lines, arcs, etc. by clearing the content of the canvas. Use `drawColor(...)` for this. – Trinimon Jul 25 '13 at 06:57
  • Check out added `onClick()` above :) – Trinimon Jul 25 '13 at 07:06
  • I picked up the wrong before (`pen`) - was intended to be `remove` ;) put the `drawColor(...)` in the `onClick(...)` listener of your *remove* button. – Trinimon Jul 25 '13 at 07:19
4
remove.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                layout.removeView(mDrawView);
                mDrawView = new SingleTouchView(MainActivity.this);
                layout.addView(mDrawView);
            }
        });
jigar
  • 1,571
  • 6
  • 23
  • 46
  • Looks like a valid option :) ... same as I said: if you remove the canvas (view) you need create a new canvas (view) ... :) – Trinimon Jul 25 '13 at 07:54
1

Put the portion of the screen you want cleared in a single layout in xml eg

<RelativeLayout
android:id="@+id/layout"
android:layout_width="wrap_content"
android:layout_height= "wrap_content">

    The Views that have to be gone on click

</RelativeLayout>

Then in your code in onClick() of the button give

((RelativeLayout) findViewById(R.id.layout)).setVisiblity(View.GONE));

EDIT :

If you want the ImageView cleared use :

((ImageView) findViewById(R.id.imageView)).setImageDrawable(null);
Satan Pandeya
  • 3,747
  • 4
  • 27
  • 53
user2041902
  • 593
  • 1
  • 6
  • 21
  • This just lets the view disappear :) – Trinimon Jul 25 '13 at 06:01
  • its not a good practice.....!!!i want to clear it forever ....!!!that doesnt apear again...! – jigar Jul 25 '13 at 06:06
  • @Trinimon-do you have any solution...bro.? – jigar Jul 25 '13 at 06:08
  • I'm a bit confused what you are looking for. First I thought you want to clear the (drawing) canvas, but that doesn't seem to be the case. Right now, it looks like you are aiming for something like described in http://stackoverflow.com/questions/3805599/add-delete-view-from-layout (i.e. remove the view from the parent layout). To be honest: I don't think this is a good idea, but may be it helps though ;) – Trinimon Jul 25 '13 at 06:14
  • @jigar So what exactly do you want? Do you want to the view to appear again? If so you can set visibility as VISIBLE whenever you want that to happen – user2041902 Jul 25 '13 at 06:50
  • @user2041902-no man i want it clear whenever it appears again..!but it appears with drawing......!not clean – jigar Jul 25 '13 at 06:51
  • If you want that the view to be removed forever you can get hold of its parent and call parent.removeView(View); – user2041902 Jul 25 '13 at 06:52
  • @jigar Its an imageView right u can set the image in it as null – user2041902 Jul 25 '13 at 06:57
  • @jigar ((ImageView) findViewById(R.id.imageview)).setImageDrawable(null); – user2041902 Jul 25 '13 at 07:00
0

instead of drawing solid color you can draw transparent color this help you don't cover the other views if you use them as a background for the canvas

mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);

I got this from this answer: https://stackoverflow.com/a/10882301/8113211

  • 2
    This is not a complete answer. Please always describe what you are doing, and for answers to questions with many other answers, please tell why we actually need your answer / in which ways it is different. Please edit it. – finnmglas Sep 14 '20 at 09:41
  • @finnmglas The accepted answer elaborated on where it might be used, but this is an alternative to the relevant code. It is different because it uses transparency, instead of black. All of this is obvious from the current answer. Try not to deter useful information because you choose not to invest any effort. – Abandoned Cart Apr 11 '22 at 09:12