Magnifying part of the canvas when touched

ε祈祈猫儿з 提交于 2019-11-30 04:34:28

To zoom the image you're drawing on the canvas:

Create a BitmapShader (using the bitmap of the image you're drawing), a Matrix and a Paint:

shader = new BitmapShader(bmp, TileMode.CLAMP, TileMode.CLAMP);
matrix = new Matrix();
shaderPaint = new Paint();
shaderPaint.setShader(shader);

On a touch event record the touch position (e.g. in a PointF):

zoomPos.x = event.getX();
zoomPos.y = event.getY();

...and set up the shader's matrix (I do this on each touch, there's probably a better way):

matrix.reset();
matrix.postScale(2f, 2f);
matrix.postTranslate(-zoomPos.x, -zoomPos.y);
shader.setLocalMatrix(matrix);

Then in the drawing code, draw a circle using the shader Paint.

canvas.drawCircle(zoomPos.x, zoomPos.y, size_of_the_circle, shaderPaint);

Edit

The two lines:

matrix.postScale(2f, 2f);
matrix.postTranslate(-zoomPos.x, -zoomPos.y);

Can be replaced with one:

matrix.postScale(2f, 2f, zoomPos.x, zoomPos.y);

This allows the scale factor to be changed without breaking the offset.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!