触摸时放大画布的一部分 [英] Magnifying part of the canvas when touched
问题描述
我有一个延伸SurfaceView自定义视图。我在画布上绘制图像。当用户触摸的点在画布上,我想显示周围被触摸坐标的区域的放大图。而preferably,随着手指移动的时候,我想更新相应的放大显示的内容。
I have a custom view that extends SurfaceView. I am drawing an image on the canvas. When the user touches a point on the canvas, i want to show a magnified view of the area around the touched co-ordinates. And preferably, as the finger moves around, i would like to update the magnified view's content accordingly.
我在想,如果Android平台支持这样的功能本身。如果没有,一个你点我可以让我开始或分享如何实现它的想法一个很好的例子。我没有做很多2D或这类的3D图形,现在还在试图了解Canvas和矩阵类,看看有什么我可以使用。
I was wondering if the android platform supports such a functionality natively. If not, can one of you point me to a good example that can get me started or share ideas on how to implement it. I don't do much 2D or 3D graphics of this sort and am still trying to understand the Canvas and Matrix Classes to see what i can be use.
我搜索了论坛,类似的问题,但找不到任何。所以,请不要标志我要问一个已经存在的问题。
I searched the forum for a similar question but could not find any. So, pls don't flag me for asking a question that already exists.
和没有 - 我没有使用OpenGL-ES或任何这样的第三方库(还)
And no - i am not using OpenGL-ES or any such 3rd party library (yet).
感谢所有。
推荐答案
要放大你画在画布上的图像:
To zoom the image you're drawing on the canvas:
创建BitmapShader(使用你绘制的图像的位图),矩阵和油漆:
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);
在一个触摸事件记录触摸位置(例如,在一个的PointF):
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);
然后在绘图code,使用着色颜料画了一个圈。
Then in the drawing code, draw a circle using the shader Paint.
canvas.drawCircle(zoomPos.x, zoomPos.y, size_of_the_circle, shaderPaint);
修改
这两条线:
matrix.postScale(2f, 2f);
matrix.postTranslate(-zoomPos.x, -zoomPos.y);
可替换为一个:
Can be replaced with one:
matrix.postScale(2f, 2f, mZoomPos.x, mZoomPos.y);
这允许比例因子被改变,而不会破坏偏置
This allows the scale factor to be changed without breaking the offset.
这篇关于触摸时放大画布的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!