Android:如何从android中的getHolder()中删除数据

时间:2012-11-27 06:24:39

标签: java android

我在触摸事件的基础上在画布上绘制多行。当我点击我需要通过Last in first out方式删除行时,我在活动中有一个清除按钮。我在arrayobject中存储触摸坐标,我在ondraw()方法中使用它。我在按钮单击时清除矢量。但它在onDraw()方法中没有效果。请帮助我。

public class ImageViewTest extends SurfaceView implements ClearData,SurfaceHolder.Callback {
    int xpoint, ypoint, xendpoint, yendpoint;
    Vector v, v1;
    Canvas canvas;
    private ViewThread mThread;
    boolean motionevent = false ,clear =false;
    ArrayList<Vector> objects = new ArrayList<Vector>();
    ArrayList<Vector> objects1 = new ArrayList<Vector>();
    Vector vobj = new Vector();

    public ImageViewTest(Context context) {
        super(context);
        getHolder().addCallback(this);
        mThread = new ViewThread(this);
        // TODO Auto-generated constructor stub
    }

    public ImageViewTest(Context context, AttributeSet attr) {

        super(context, attr);
        getHolder().addCallback(this);
        mThread = new ViewThread(this);
        // TODO Auto-generated constructor stub
    }

    public void cleardata() {
         System.out.println(" cleardata");
         this.objects.clear();
         this.objects1.clear();
        // v = null;
        //
        //
        // System.out.println(" v value "+v);
    //  onDraw(canvas);
        invalidate();

    }


    public void doDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        //super.onDraw(canvas);

        Paint p = new Paint();

        System.out.println("ondraw " + objects.size() + " // "
                + objects1.size()+ "CANVAS "+canvas);

        DisplayMetrics metrics = getResources().getDisplayMetrics();

        Bitmap mapImg = BitmapFactory.decodeResource(getResources(),
                R.drawable.film2);
        Rect dest = new Rect(0, 0, getWidth(), getHeight());
        p.setFilterBitmap(true);
        canvas.drawColor(Color.BLACK);
        canvas.drawBitmap(mapImg, null, dest, p);
        if(motionevent)
        {
            p.setColor(Color.RED);
            p.setStrokeWidth(2);
            canvas.drawLine(xpoint, ypoint, xendpoint, yendpoint, p);

        }else{
            canvas.drawBitmap(mapImg, null, dest, p);
        //  System.out.println("Objects size "+objects.size()+" // "+objects1.size());
//
        for (int index = 0; index < objects.size(); index++) {



            p.setColor(Color.RED);
            p.setStrokeWidth(2);



            Vector vv = objects.get(index);
            Vector vv1 = new Vector();



            xpoint = (int) Float.parseFloat(vv.elementAt(0).toString());
            ypoint = (int) Float.parseFloat(vv.elementAt(1).toString());

            if (objects1.size() > index) {
                vv1 = objects1.get(index);
            //  System.out.println("VV SIZE " + vv.size() + " // " + vv1.size());
                if (vv1.size() == 2) {
                    xendpoint = (int) Float.parseFloat(vv1.elementAt(0)
                            .toString());
                    yendpoint = (int) Float.parseFloat(vv1.elementAt(1)
                            .toString());
                }
            }

            canvas.drawLine(xpoint, ypoint, xendpoint, yendpoint, p);

        }
        }

        // canvas.drawLine(xpoint, ypoint, xendpoint, yendpoint, p);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //System.out.println("getAction  " + event.getAction());
        //System.out.println("X CORdinates " + event.getX() + " Y coornaf  "
        //      + event.getY() + " V " + objects.size());
        v = new Vector();
        v1 = new Vector();
        switch (event.getAction()) {
        case 0: // down
            xpoint = (int) event.getX();
            ypoint = (int) event.getY();
            v.add(event.getX());
            v.add(event.getY());
            objects.add(v);

            break;
        case 1: // up
            motionevent = false;
            v1.add(event.getX());
            v1.add(event.getY());
            objects1.add(v1);
            break;
        case 2: // move
            motionevent = true;
            xendpoint = (int) event.getX();
            yendpoint = (int) event.getY();

            break;

        }

        // if(v.isEmpty())
        // {
        // v.add(event.getX());
        // v.add(event.getY());
        // System.out.println("hi ");
        // xpoint =(int)Float.parseFloat(v.get(0).toString());
        // ypoint = (int)Float.parseFloat(v.get(1).toString());
        //
        //
        // }
        // xendpoint = (int)event.getX();
        // yendpoint = (int)event.getY();
        // DisplayMetrics metrics = getResources().getDisplayMetrics();
        invalidate();
        return true;
    }

    @Override
    public void clearData() {
        // TODO Auto-generated method stub
        System.err.println(" hi m");



         this.objects.clear();
         this.objects1.clear();
         System.out.println("OBJECTS VALUE "+objects.size()+ " // "+objects1.size() );
         clear =true;

         System.err.println(clear+" CLEAR");

         //  postInvalidate();
        // onDraw(canvas);

    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width,
            int height) {
        System.err.println("Srface changed");
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        System.out.println("Surface created");
        if (!mThread.isAlive()) {
            mThread = new ViewThread(this);
            mThread.setRunning(true);
            mThread.start();
        }

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub

    }

    class ViewThread extends Thread {
        private ImageViewTest mPanel;
        private SurfaceHolder mHolder;
        private boolean mRun = false;

        public ViewThread(ImageViewTest panel) {
            System.out.println("ViewThREd constructor");
            mPanel = panel;
            mHolder = mPanel.getHolder();
        }

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

        @Override
        public void run() {
            Canvas canvas = null;
            while (mRun) {
                  System.out.println("Inside Run  "+mPanel.clear);
                canvas = mHolder.lockCanvas(); 
                if (canvas != null) {
                    if(clear)
                    {
                        System.out.println("clear call back");
                        mHolder.removeCallback(mPanel);
                    }
                    mPanel.doDraw(canvas);
                    mHolder.unlockCanvasAndPost(canvas);
                }
            }
        }

}
}

0 个答案:

没有答案