在脑中减少笔划缩放和重绘

时间:2015-11-05 12:45:53

标签: android-canvas scaling ontouchlistener redraw stroke

当用户在屏幕上涂鸦并在相同位置重绘相同笔划时,尝试获取笔划数据,基于显示器的屏幕尺寸缩小尺寸。我的问题是这个代码有效,它可以缩小前锋,但是所有按比例缩小的笔划都是连接起来的,这是我不想要的。

我的ontouch代码段:

    @Override
public boolean onTouch(View arg0, final MotionEvent event) {
     x = event.getX();
     y =event.getY();
/*  float x=(float) (xx*0.7);
    float y=(float) (yy*0.7);*/
    array_x.add(x);
    array_y.add(y);
    if(event.getPointerCount() == 2){
        timevar2 = event.getEventTime();
        float newx = event.getX(0);
        float newy = event.getY(0);
        float oldx = event.getX(1);
        float oldy = event.getY(1);
        mPath.reset();
        float equa = (float) (Math.pow(newx - oldx, 2) + Math.pow(newy - oldy, 2));
        float cscale = (float) Math.sqrt(equa)/100;
        float scaled = (cscale - old_scale);
        if(scaled < -0.1){
            if(scale > 0.1){
              scale -= 0.03;
            }
        }
        if(scaled > 0.1){
            scale += 0.03;
        }

        old_scale = cscale;
        invalidate();
        return true;
    }
    long dt = event.getEventTime() - timevar2;
    if(dt < 100){
        return true;
    }

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
        touch_start(x, y);
        Log.v("TOUCH=======start","X=="+x + "Y=="+y);
        invalidate();

        break;
    case MotionEvent.ACTION_MOVE:
        touch_move(x, y);
        Log.v("TOUCH=======move","X=="+x + "Y=="+y);
        invalidate();
        break;
    case MotionEvent.ACTION_UP:
        touch_up(x,y);
        Log.v("TOUCH=======up","X=="+x + "Y=="+y);
        //invalidate();

        Handler mHandler = new Handler();
         mHandler.postDelayed(new Runnable() {
                public void run() {

                    for(int i=0;i<array_x.size();i++)
                    {
                        x= array_x.get(i);
                        y= array_y.get(i);

                        if(i == 0){
                            x=(float) (x*0.7);
                            y=(float) (x*0.7);
                            Log.i("ARRAYYYYYY", "X=="+x+"Y=="+y);
                            touch_start(x, y);
                            invalidate();
                        }else if(i == array_x.size()-1){

                            x=(float) (x*0.7);
                            y=(float) (x*0.7);

                            touch_up(x, y);
                            invalidate();
                            mPath.reset();
                        }else{
                            x=(float) (x*0.7);
                            y=(float) (y*0.7);

                            touch_move(x, y);
                            invalidate();

                        }
                    }
                }
            }, 2000);

        break;
    }

    return true;
}

private void touch_move(float x, float y) {

    float dx = Math.abs(x - mX);
    float dy = Math.abs(y - mY);
     if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
    mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
    mX = x;
    mY = y;
     }
    if (this.isErase) {
        erasePath(mPath, x, y, mX, mY);
        return;
    }
    PointF point = new PointF();
    point.x = x;
    point.y = y;
    newPoint.points.add(point);
    mCanvas.drawPath(mPath, mPaint);

}

private void touch_start(float x, float y) {
    undonePaths.clear();
    mPath = new Path();
    newPoint = new Stroke();
    PointF point = new PointF();
    point.x = x;
    point.y = y;
    newPoint.points.add(point);
    mCanvas.drawPath(mPath, mPaint);

    mPath.moveTo(x, y);
    mX = x;
    mY = y;

}
private void touch_up(float x, float y) {

    if (this.isErase) {
        return;
    }
    PointF point = new PointF();
    point.x = x;
    point.y = y;
    newPoint.points.add(point);
    mPath.lineTo(mX, mY);
    // commit the path to our offscreen
    mCanvas.drawPath(mPath, mPaint);
    Pair p = new Pair(mPath, newPoint);
    this.Strokepaths.add(p);

    }

0 个答案:

没有答案