简体   繁体   English

如何用两根手指缩放画布并移动?

[英]How to zoom canvas by two fingers and move?

I can draw by fingers in canvas, but I can not zoom it by two fingers without loss of scale drawn 我可以在画布上用手指画画,但不能用两根手指缩放它而不会失去画出的比例

and I want to do infinite my canvas size 我想做无限的画布尺寸

I have this code: 我有以下代码:

public class MainActivity1 extends AppCompatActivity {

DrawingView dv ;
private Paint mPaint;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    dv = new DrawingView(this);
    setContentView(dv);
    mPaint = new Paint();
    mPaint.setAntiAlias(true);
    mPaint.setDither(true);
    mPaint.setColor(Color.GREEN);
    mPaint.setStyle(Paint.Style.STROKE);
    mPaint.setStrokeJoin(Paint.Join.ROUND);
    mPaint.setStrokeCap(Paint.Cap.ROUND);
    mPaint.setStrokeWidth(12);
}

public class DrawingView extends View {

    public int width;
    public  int height;
    private Bitmap mBitmap;
    private Canvas mCanvas;
    private Path    mPath;
    private Paint   mBitmapPaint;
    Context context;
    private Paint circlePaint;
    private Path circlePath;

    public DrawingView(Context c) {
        super(c);
        context=c;
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        circlePaint = new Paint();
        circlePath = new Path();
        circlePaint.setAntiAlias(true);
        circlePaint.setColor(Color.BLUE);
        circlePaint.setStyle(Paint.Style.STROKE);
        circlePaint.setStrokeJoin(Paint.Join.MITER);
        circlePaint.setStrokeWidth(4f);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        mBitmap = Bitmap.createBitmap(1000, 1000, Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.drawBitmap( mBitmap, 0, 0, mBitmapPaint);
        canvas.drawPath( mPath,  mPaint);
        canvas.drawPath( circlePath,  circlePaint);
    }

    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;

    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }

    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;

            circlePath.reset();
            circlePath.addCircle(mX, mY, 30, Path.Direction.CW);
        }
    }

    private void touch_up() {
        mPath.lineTo(mX, mY);
        circlePath.reset();
        // commit the path to our offscreen
        mCanvas.drawPath(mPath,  mPaint);
        // kill this so we don't double draw
        mPath.reset();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();



        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touch_start(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_MOVE:
                touch_move(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                touch_up();
                invalidate();
                break;
        }
        return true;
    }
}


}

Please give me advice how to do it? 请给我建议怎么做?

And I also want to do infinite my canvas size - its optional. 而且我也想做无限的画布大小-它是可选的。 Main problem is zoom 主要问题是变焦

My advice - you can use opengl. 我的建议-您可以使用opengl。 It's looks like you want to create some drawer, but canvas not perfect solution for this. 看来您想创建一些抽屉,但是画布并不是为此的完美解决方案。 Currently almost all devices support opengl es. 当前,几乎所有设备都支持opengl。 It faster than canvas and you can made many another function(as scaling, image pasting, 3d objects etc.) much more easier. 它比画布快,您可以使许多其他功能(例如缩放,图像粘贴,3d对象等)更加容易。 Here you can find a good tutorial. 在这里您可以找到一个很好的教程。

Yoo should use ScaleGestureDetector to scale your canvas. Yoo应该使用ScaleGestureDetector缩放画布。 This is my sample code: 这是我的示例代码:

     private class MyScaleGestureListener implements ScaleGestureDetector.OnScaleGestureListener { 
           public boolean onScale(ScaleGestureDetector detector) {
              scaleFactor *= detector.getScaleFactor(); 
              return true;
        }
           public boolean onScaleBegin(ScaleGestureDetector detector) { 
              Log.d("myZoomTag", "SCALE STARTED"); 
              return true; 
        }
           public void onScaleEnd(ScaleGestureDetector detector) { 
             Log.d("myZoomTag", "SCALE ENDED"); 
        } 

  }

Here scaleFactor is global variable. 在这里, scaleFactor是全局变量。 In your onDraw method you should do something like this: 在您的onDraw方法中,您应该执行以下操作:

bitmapWidth *= scaleFactor;
bitmapHeight *= scaleFactor;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM