画布,在Android中重绘图像

时间:2013-12-06 09:30:37

标签: android drawing android-canvas

我有一个应用程序,用户可以在屏幕上绘图。在用户绘制,图片擦除之后,以及在没有用户的情况下重新绘制3秒后,我该怎么做。现在我只能清除屏幕,但不能重新绘制它。

代码:

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        final int buttonState = event.getButtonState();
        int pressedButtons = buttonState & ~mOldButtonState;
        mOldButtonState = buttonState;

        if ((pressedButtons & MotionEvent.BUTTON_SECONDARY) != 0) {
            advanceColor();
        }

        PaintMode mode;
        mode = PaintMode.Draw;

        final int action = event.getActionMasked();
        if (action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE
                || action == MotionEvent.ACTION_HOVER_MOVE) {
            final int N = event.getHistorySize();
            final int P = event.getPointerCount();
            for (int i = 0; i < N; i++) {
                for (int j = 0; j < P; j++) {
                    paint(mode,
                            event.getHistoricalX(j, i),
                            event.getHistoricalY(j, i),
                            event.getHistoricalPressure(j, i),
                            event.getHistoricalTouchMajor(j, i),
                            event.getHistoricalTouchMinor(j, i),
                            event.getHistoricalOrientation(j, i),
                            event.getHistoricalAxisValue(MotionEvent.AXIS_DISTANCE, j, i),
                            event.getHistoricalAxisValue(MotionEvent.AXIS_TILT, j, i));
                }
            }
            for (int j = 0; j < P; j++) {
                paint(mode,
                        event.getX(j),
                        event.getY(j),
                        event.getPressure(j),
                        event.getTouchMajor(j),
                        event.getTouchMinor(j),
                        event.getOrientation(j),
                        event.getAxisValue(MotionEvent.AXIS_DISTANCE, j),
                        event.getAxisValue(MotionEvent.AXIS_TILT, j));
            }
            x1 = event.getX();
            y1 = event.getY();
            long[] pattern = {0, 100, 0};
            v.vibrate(pattern, 0);
        }
        else if (action == MotionEvent.ACTION_UP) {
            x2 = event.getX();
            y2 = event.getY();
            v.cancel();
timer = new CountDownTimer(3000, 1000) {

                @Override
                public void onFinish() {
                    mView.clear();
                }
                @Override
                public void onTick(long millisUntilFinished) {
                    // TODO Auto-generated method stub
                }
            }.start();

        }
        return true;
    }


    private void paint(PaintMode mode, float x, float y, float pressure,
            float major, float minor, float orientation,
            float distance, float tilt) {
        if (mBitmap != null) {

            if (major <= 0 || minor <= 0) {
                // If size is not available, use a default value.
                major = minor = 16;
            }

            mPaint.setColor(COLORS[mColorIndex]);
            mPaint.setStrokeJoin(Join.ROUND);
            mPaint.setMaskFilter(new BlurMaskFilter(30, BlurMaskFilter.Blur.SOLID));
            drawOval(mCanvas, x, y, major, minor, orientation, mPaint);
            invalidate();
        }
    }
    private final RectF mReusableOvalRect = new RectF();
    private void drawOval(Canvas canvas, float x, float y, float major, float minor,
            float orientation, Paint paint) {
        canvas.save(Canvas.MATRIX_SAVE_FLAG);
        minor = 2*minor;
        major = 2*major;
        if(Math.abs(x2-x1) > Math.abs(y2-y1)) {
            tmp = minor;
            minor = major;
            major = minor;
        } 
        mReusableOvalRect.left = x - minor;// / 2;
        mReusableOvalRect.right = x + minor;// / 2;
        mReusableOvalRect.top = y - major;// / 2;
        mReusableOvalRect.bottom = y + major;// / 2;
        canvas.drawOval(mReusableOvalRect, paint);
        canvas.restore();
    }

0 个答案:

没有答案