0

我想在触摸时画线,屏幕应该是透明的。

public class Draw extends Activity implements OnTouchListener

{

    DrawPanel drawPanel;

    private ArrayList<Path> pointsToDraw = new ArrayList<Path>();

    private Paint mPaint;

    Path path;



    @Override

    public void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_draw);

        drawPanel = new DrawPanel(this); 

        drawPanel.setOnTouchListener(this);





        mPaint = new Paint();

        mPaint.setDither(true);

        mPaint.setColor(Color.RED);

        mPaint.setStyle(Paint.Style.STROKE);

        mPaint.setStrokeJoin(Paint.Join.ROUND);

        mPaint.setStrokeCap(Paint.Cap.ROUND);

        mPaint.setStrokeWidth(10);



        FrameLayout framelayout = new FrameLayout(this);  

        framelayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,      LayoutParams.FILL_PARENT));  

        framelayout.addView(drawPanel);  

        setContentView(framelayout);  



    }



    @Override

    protected void onPause()

    {

        super.onPause();

        drawPanel.pause();

    }



    @Override

    protected void onResume()

    {

        super.onResume();

        drawPanel.resume();

    }   

    @Override

    public boolean onTouch(View arg0, MotionEvent arg1)

    {

        synchronized(pointsToDraw)

        {

                    if(arg1.getAction() == MotionEvent.ACTION_DOWN)

                    {

                        path = new Path();

                        path.moveTo(arg1.getX(), arg1.getY()); 

                        pointsToDraw.add(path);

                        path.lineTo(arg1.getX()+1, arg1.getY()+1);

                    }

                    else if(arg1.getAction() == MotionEvent.ACTION_MOVE)

                    {

                        path.lineTo(arg1.getX(), arg1.getY());

                    }

                    else if(arg1.getAction() == MotionEvent.ACTION_UP)

                    {

                        //path.lineTo(arg1.getX()+2, arg1.getY()+2);

                    }

                }       

                return true;

    }





    public class DrawPanel extends SurfaceView implements Runnable

    {

        Thread thread = null;

        SurfaceHolder holder;

        boolean isItOk = false ;



        public DrawPanel(Context context)

        {

            super(context);

            holder = getHolder();

            holder.setFormat(PixelFormat.TRANSPARENT);

        }



        public void run()

        {

            while( isItOk == true){



                if(!holder.getSurface().isValid()){

                    continue;

                }



                Canvas c = holder.lockCanvas();

                c.drawARGB(0, 0, 0, 0);

                onDraw(c);

                holder.unlockCanvasAndPost(c);

            }

        }



        @Override

        protected void onDraw(Canvas canvas)

        {



            super.onDraw(canvas);

            synchronized(pointsToDraw)

            {

                for (Path path : pointsToDraw)

                {

                canvas.drawPath(path, mPaint);

                }

            }

        }



        public void pause()

        {

            isItOk = false;

            while(true)

            {

                try

                {

                                thread.join();

                }

                catch(InterruptedException e)

                {

                    e.printStackTrace();

                }

                break;

            }

            thread = null;

        }



        public void resume()

        {

            isItOk = true;  

            thread = new Thread(this);

            thread.start();



        }

    }

}

使用此代码,我得到了预期的结果。但现在的问题是当我使用 xml 放置一个按钮时,我看不到该按钮。

我想在按下按钮时清除屏幕。所以当主题是半透明的时候请帮我看看按钮。

4

1 回答 1

1

我不认为它可能是因为LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)。你可以试试LayoutParams(LayoutParams.FILL_PARENT,300)。否则,您可以添加一个menuitem而不是button

于 2013-01-09T10:39:28.923 回答