2013-07-25 3 views
5

J'ai créé une application de démonstration simple dans Android pour le dessin, maintenant quelqu'un peut-il me dire comment puis-je supprimer vue et effacer l'écran en un seul clic. J'ai essayé comme suit: s'il vous plaît aidez-moi à le faire .... en avance..!comment effacer la vue dans android?

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> 
+0

Je ne suis pas sûr de ce que vous voulez dire, mais peut-être cela peut aider: http://stackoverflow.com/questions/8181191/how-to-clear-content-on-the-layout –

+0

@ yser2617212- m poster mon fichier xml pour que vous ayez une idée ...! Je veux effacer tous les chemins que j'ai dessiné en un seul clic ...! – jigar

+0

Cher * jigar *, si vous voulez effacer les chemins dessinés, vous pouvez effacer votre toile en définissant sa couleur. Vous n'avez pas besoin de supprimer la vue. Pour effacer le contenu de 'mPaths', il suffit de lui attribuer' @ null'. Le collecteur d'ordures nettoiera la mémoire. Je recommande d'ajouter ou de supprimer des vues uniquement si vous avez un nombre modifiable de vues de même type dans votre mise en page, par exemple. si vous avez plus d'une toile et que vous voulez en supprimer une. – Trinimon

Répondre

4

Jus utilisation ...

mCanvas.drawColor(Color.BLACK); 

... pour effacer la toile ou toute autre couleur que vous voulez avoir en arrière-plan. Par exemple.

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.: pour supprimer dynamiquement une vue de son conteneur, utilisez removeView(....), par ex.

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

Espérons que cela aide ... À votre santé!

+0

@ Trinimon-Brother ... j'ai réussi à effacer l'écran par layout.removeAllViews(); mais ... après ça je ne suis plus capable de dessiner avec mon stylo ...! :( – jigar

+0

Cher * jigar *, si vous supprimez la vue, cela signifie que l'objet sous-jacent qui contient le canevas sera supprimé, donc si vous voulez continuer à dessiner, vous devez d'abord créer un nouveau canevas de façon dynamique. Tout le temps, je ne pense pas que vous ayez besoin de le supprimer/effacer, effacez simplement les chemins peints, les lignes, les arcs, etc. en effaçant le contenu de la toile Utilisez 'drawColor (...)' pour cela. – Trinimon

+0

s'il vous plaît donnez-moi "dessiner couleur()" code s'il vous plaît .. :) – jigar

-1

Put t il partie de l'écran que vous souhaitez effacera, une mise en page simple en XML par exemple

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

Puis dans votre code onClick() du bouton donne

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

EDIT:

Si vous voulez que ImageView utilisation autorisée:

((ImageView) findViewById(R.id.imageView)).setImageDrawable(null); 
+0

Cela laisse juste la vue disparaître :) – Trinimon

+0

ce n'est pas une bonne pratique ..... !!! Je veux effacer pour toujours .... !!! qui ne ressort pas encore ...! – jigar

+0

@ Trinimon-avez-vous une solution ... bro.? – jigar

4
remove.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

       layout.removeView(mDrawView); 
       mDrawView = new SingleTouchView(MainActivity.this); 
       layout.addView(mDrawView); 
      } 
     }); 
+0

On dirait une option valide :) ... même que je l'ai dit: si vous supprimez la toile (vue), vous devez créer une nouvelle toile (vue) ... :) – Trinimon

Questions connexes