2013-07-25 39 views
5

我已經在android中爲繪圖創建了一個簡單的演示應用程序,現在任何人都可以說我怎樣才能刪除視圖和清除屏幕只需點擊一下。我已經嘗試如下:請幫助我做到這一點.... thanx提前..!如何清除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

我不知道你的意思,但也許這可以幫助:http://stackoverflow.com/questions/8181191/how-to-clear-content-on-the-layout – 2013-07-25 05:30:40

+0

@ yser2617212- m發佈我的XML文件,以便你會明白...!我想通過點擊一下就清除所有的路徑......! – jigar

+0

親愛的* jigar *,如果您想清除繪製的草圖,您可以通過設置其顏色來清除畫布。您不需要刪除視圖。爲了清除'mPaths'的內容,只需將'@ null'賦值給它。垃圾收集器將清理內存。如果您的佈局中有可變數量的相同類型的視圖,我建議只添加或移除視圖。如果您有多個畫布並且您想要移除其中的一個。 – Trinimon

回答

4

使用...

mCanvas.drawColor(Color.BLACK); 

...用於清除畫布或任何其他背景顏色。例如。

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.:用於從其容器使用removeView(....)動態地移除視圖,例如,

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

希望這會有所幫助...乾杯!

+0

@ Trinimon-Brother ...我通過layout.removeAllViews()成功清除了屏幕。但是......之後我無法再用筆畫出來......! :( – jigar

+0

親愛的* jigar *,如果您刪除了視圖,則意味着包含畫布的底層對象將被刪除,因此如果您想繼續繪製,您必須先動態創建一個新的畫布,但如果您只有一個我不認爲你需要刪除/刪除它,只需清除畫布的內容,清除畫出的路徑,線條,弧線等,使用'drawColor(...)'就可以了。 – Trinimon

+0

請給我「繪製顏色()」的代碼請:) .. – jigar

-1

穿戴噸他要在一個佈局XML例如,在你的代碼在按鈕的onClick()清除

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

這時屏幕上的部分給予

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

編輯:

如果你想在ImageView清除使用:

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

這只是讓視圖消失:) – Trinimon

+0

它不是一個很好的做法..... !!!我想永遠清除它.... !!!不再apear ...! – jigar

+0

@ Trinimon - 你有什麼解決方案...兄弟? – jigar

4
remove.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 

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

看起來像一個有效的選項:) ...與我所說的相同:如果您刪除畫布(視圖),您需要創建一個新的畫布(視圖)... :) – Trinimon