2011-05-21 52 views
2

如何清除android中的surfaceView?在android中清理SurfaceView

我有這樣一類 -

class DrawingPanel extends SurfaceView implements SurfaceHolder.Callback { 

任何一個可以告訴哪個方法shud我打電話來清除我的自定義drawingPanel

這是我的課 -

package com.applenty.LearnToCount; 

import java.util.ArrayList; 

import android.app.Activity; 
import android.content.Context; 
import android.content.Intent; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.ColorFilter; 
import android.graphics.Paint; 
import android.graphics.Path; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Gravity; 
import android.view.KeyEvent; 
import android.view.MotionEvent; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.Window; 
import android.view.WindowManager; 
import android.view.ViewGroup.LayoutParams; 
import android.widget.Button; 
import android.widget.FrameLayout; 
import android.widget.LinearLayout; 
import android.widget.Toast; 

public class WhiteBoard extends Activity { 
    private ArrayList<Path> _graphics = new ArrayList<Path>(); 
    private Paint mPaint; 
    Button button_clear; 
    FrameLayout layout; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.shake); 

     layout = new FrameLayout(this); 
     final LinearLayout l1 = new LinearLayout(this); 
     final LinearLayout l2 = new LinearLayout(this); 

     final DrawingPanel drawingPanel = new DrawingPanel(this); 
     // drawingPanel.setBackgroundColor(Color.WHITE); 

     LayoutParams params = new LayoutParams(
       ViewGroup.LayoutParams.WRAP_CONTENT, 
       ViewGroup.LayoutParams.WRAP_CONTENT); 

     button_clear = new Button(this); 
     button_clear.setText("Clear Screen"); 
     button_clear.setGravity(Gravity.TOP | Gravity.LEFT); 
     button_clear.setVisibility(View.VISIBLE); 
     button_clear.setId(1); 
     button_clear.setLayoutParams(params); 
     //layout.addView(button_clear); 

     drawingPanel.setLayoutParams(params); 
     drawingPanel.setId(2); 


     l1.addView(button_clear); 
     l2.addView(drawingPanel); 

     layout.addView(l2); 
     layout.addView(l1); 
     setContentView(layout); 
     mPaint = new Paint(); 
     mPaint.setDither(true); 
     mPaint.setColor(Color.RED); 
     mPaint.setColorFilter(new ColorFilter()); 
     mPaint.setStyle(Paint.Style.STROKE); 
     mPaint.setStrokeJoin(Paint.Join.ROUND); 
     mPaint.setStrokeCap(Paint.Cap.ROUND); 
     mPaint.setStrokeWidth(5); 

     button_clear = (Button)findViewById(1); 
     button_clear.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       Toast t = Toast.makeText(WhiteBoard.this, "ISNIDE", 3); 
       t.show(); 
       Log.d("DEBUG","CLEARING***************************"); 
       //Canvas canvas = new Canvas(); 
       //canvas.drawColor(0); 
       //Canvas canvas = new Canvas(); 
       //canvas.drawColor(Color.WHITE); 
       drawingPanel.setBackgroundColor(Color.WHITE); 
       Canvas c = new Canvas(); 
       drawingPanel._thread.getSurfaceHolder().unlockCanvasAndPost(c); 


       //drawingPanel.getHolder().unlockCanvasAndPost(canvas); 
       //canvas = drawingPanel.getHolder().lockCanvas(null); 
      } 
     }); 

    } 

    class DrawingPanel extends SurfaceView implements SurfaceHolder.Callback { 
     private DrawingThread _thread; 
     private Path path; 

     public DrawingPanel(Context context) { 
      super(context); 
      getHolder().addCallback(this); 
      _thread = new DrawingThread(getHolder(), this); 
     } 

     public boolean onTouchEvent(MotionEvent event) { 
      synchronized (_thread.getSurfaceHolder()) { 
       if (event.getAction() == MotionEvent.ACTION_DOWN) { 
        path = new Path(); 
        path.moveTo(event.getX(), event.getY()); 
        path.lineTo(event.getX(), event.getY()); 
       } else if (event.getAction() == MotionEvent.ACTION_MOVE) { 
        path.lineTo(event.getX(), event.getY()); 
       } else if (event.getAction() == MotionEvent.ACTION_UP) { 
        path.lineTo(event.getX(), event.getY()); 
        _graphics.add(path); 
       } 

       return true; 
      } 
     } 

     @Override 
     public void onDraw(Canvas canvas) { 
      for (Path path : _graphics) { 
       // canvas.drawPoint(graphic.x, graphic.y, mPaint); 
       canvas.drawPath(path, mPaint); 
      } 
     } 

     public void surfaceChanged(SurfaceHolder holder, int format, int width, 
       int height) { 
      // TODO Auto-generated method stub 

     } 

     public void surfaceCreated(SurfaceHolder holder) { 
      // TODO Auto-generated method stub 
      _thread.setRunning(true); 
      _thread.start(); 
     } 

     public void surfaceDestroyed(SurfaceHolder holder) { 
      // TODO Auto-generated method stub 
      boolean retry = true; 
      _thread.setRunning(false); 
      while (retry) { 
       try { 
        _thread.join(); 
        retry = false; 
       } catch (InterruptedException e) { 
        // we will try it again and again... 
       } 
      } 
     } 
    } 

    class DrawingThread extends Thread { 
     private SurfaceHolder _surfaceHolder; 
     private DrawingPanel _panel; 
     private boolean _run = false; 

     public DrawingThread(SurfaceHolder surfaceHolder, DrawingPanel panel) { 
      _surfaceHolder = surfaceHolder; 
      _panel = panel; 
     } 

     public void setRunning(boolean run) { 
      _run = run; 
     } 

     public SurfaceHolder getSurfaceHolder() { 
      return _surfaceHolder; 
     } 

     @Override 
     public void run() { 
      Canvas c; 
      while (_run) { 
       c = null; 
       try { 
        c = _surfaceHolder.lockCanvas(null); 
        synchronized (_surfaceHolder) { 
         _panel.onDraw(c); 
        } 
       } finally { 
        // do this in a finally so that if an exception is thrown 
        // during the above, we don't leave the Surface in an 
        // inconsistent state 
        if (c != null) { 
         _surfaceHolder.unlockCanvasAndPost(c); 
        } 
       } 
      } 
     } 
    } 

    @Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
     if (keyCode == KeyEvent.KEYCODE_BACK) { 
      startActivity(new Intent(WhiteBoard.this, HomeActivity.class)); 
      finish(); 
      return true; 
     } 
     return true; 
    } 

} 

我想清除onClickListner上的屏幕。任何想法任何想法請...

回答

3

你不清楚它,你只是避免畫任何東西到畫布...只使用一個空的畫布(也許畫一個黑色的矩形或任何你想要的背景)

+0

可以用一些代碼開始。 – nasaa 2011-05-21 02:26:42

+3

使用'canvas.drawColor(Color.BLACK);'代替你的常規繪圖方法(使用你從'SurfaceHolder'獲得的Canvas)。 – Aleadam 2011-05-21 02:36:56

+1

你的意思是這樣 - drawingPanel.getHolder()。getSurface() 我很困惑:S – nasaa 2011-05-21 03:09:13

11

發現這對於其他人來說是一種很好的清除畫布的方法,只需將它畫成黑色或其他顏色就行不通。我的問題是我想清除覆蓋在系統屏幕上的透明畫布,所以將它塗成黑色是不夠的。這工作雖然:

canvas.drawColor(0, PorterDuff.Mode.CLEAR); 

感謝這個線程,我發現它。 clear transparent canvas - Google Groups

+1

之後,我得到這個錯誤:MediaPlayer:錯誤(1,-1004)和MediaPlayer不啓動任何視頻。 – 2014-03-12 21:04:54

+0

它適合我。順便說一句,通過使用「Color.TRANSPARENT」代碼可讀性可以獲得值「0」。 – webmaster 2015-08-06 13:29:31

0
button_clear = (Button)findViewById(1); 
button_clear.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) { 
    Intent i = new Intent(getBaseContext(),Drawing.class); 
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    startActivity(i); 
    } 
}); 
+0

添加一些描述和答案 – 2012-10-26 05:36:19