使用Android抬起手指后绘制的线条消失

时间:2018-07-29 13:05:22

标签: android

我正在尝试在我的应用程序中实现绘图功能。我能够绘制三角形和其他不同形状。我有以下问题。 1.但是我无法画直线,当我抬起手指时,它消失了,尝试了所有的运气。 2. drawRect()和drawOval()不会在画布上绘制任何内容,在某些设备上可以正常工作,在某些设备上则不显示任何内容。 请帮助我解决该问题。

@Override
    public boolean onTouch(View v, MotionEvent event) {
        float x= event.getX();
        float y= event.getY();

        switch(event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            startX = event.getX();
            startY = event.getY();
            /*touchStart(x, y);
            invalidate();*/


            break;

        case MotionEvent.ACTION_MOVE:

        //  touchMove(x, y);
            endX = event.getX();
            endY = event.getY(); 
            invalidate();                       

            break;
        case MotionEvent.ACTION_UP:
                endX = event.getX();
                endY = event.getY();
                can.drawLine(startX, startY, endX, endY, brush);
            //    touchUp();

            break;



        }
        return true;
    }

@Override
    protected void onDraw(Canvas canvas) {
        if(imageToEdit!=null) {

            can.drawColor(0x323232);                                                                               
        }
        else{
            can.drawColor(0xdcdcdc);                                                                               
        }
        super.onDraw(can);                                                                                         

        if(resizerCount < 5) {
            resizedImage=resizeImageForImageView(imageToEdit);
            resizerCount++;

        }

        if(resizedImage!=null) {
            imageStartX=(getWidth() - resizedImage.getWidth()) / 2;
            imageStartY=(getHeight()- resizedImage.getHeight()) /2;
            int possibleStartX= getWidth()/20;
            if(possibleStartX + resizedImage.getWidth() < getWidth()) {
                imageStartX=possibleStartX;
            }

            graffitiModel.setImageBounds(imageStartX, imageStartX + resizedImage.getWidth(), imageStartY, imageStartY + resizedImage.getHeight());

            canvas.drawBitmap(resizedImage, imageStartX, imageStartY, brush);
        }


        Paint paint= new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setStyle(Paint.Style.FILL_AND_STROKE);
        brush.set(paint);
        brush.setColor(Color.BLACK);
        brush.setStrokeWidth(4);

//      canvas.drawLine(startX, startY, endX, endY, brush);

        canvas.drawRect(100, 100, 300, 400, brush);

        canvas.drawPath(path, brush);


    }

请帮助我。谢谢

0 个答案:

没有答案