开发者

Clearing SurfaceView in android

开发者 https://www.devze.com 2023-03-07 04:43 出处:网络
how to clear the surfaceView in android? I have a class like this - class DrawingPanel extends SurfaceView implements SurfaceHolder.Callback {

how to clear the surfaceView in android?

I have a class like this -

class DrawingPanel extends SurfaceView implements SurfaceHolder.Callback {

can any one tell as to which method shud i call to clear my custom drawingPanel

This is my class -

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 Su开发者_如何转开发rfaceHolder.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;
    }

}

and i want to clear the screen onClickListner. Any body any idea pls...


Found this for anyone else lookign for a good way to clear canvas where simply painting it black or another color wont work. My problem was that I wanted to clear a transparent canvas that was overlayed over the system screen, so painting it black would not suffice. This worked though:

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

thanks to this thread where I found it. clear transparent canvas - Google Groups


You don't clear it, you just avoid drawing anything to the canvas... just use an empty canvas (perhaps drawing a black rectangle or whatever you want as background)


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

精彩评论

暂无评论...
验证码 换一张
取 消