saj*_*joo 5 android image zoom android-canvas
我在缩放画布时遇到了问题.我已经制作了一个自定义视图,我现在绘制关系图时,我缩小画布到位置(0,0).我看到了不同的主题和问题,但找不到合适的答案.
我在onDraw方法中做的是.
canvas.scale(mScaleFactor, mScaleFactor);
Run Code Online (Sandbox Code Playgroud)
我也看过canvas.scale(x,y,px,py)方法,但我不知道如何获得x和y的轴心点.
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
// Don't let the object get too small or too large.
mScaleFactor = Math.max(0.4f, Math.min(mScaleFactor, 5.0f));
if(mScaleFactor>=1)
mScaleFactor=1f;
invalidate();
return true;
Run Code Online (Sandbox Code Playgroud)
}
枢轴点基本上是画布将被转换的点,因此使用0,0的轴进行缩放会使其朝向该点缩小.使用以下方法,您可以将轴心点更改为您想要的位置:
canvas.scale(x, y, px, py);
Run Code Online (Sandbox Code Playgroud)
现在换新的东西:如果你想让你的画布向它的中心缩放,你只需要知道画布中间的点:
float cX = canvas.getWidth()/2.0f; //Width/2 gives the horizontal centre
float cY = canvas.getHeight()/2.0f; //Height/2 gives the vertical centre
Run Code Online (Sandbox Code Playgroud)
然后你可以使用这些坐标来缩放它:
canvas.scale(x, y, cX, cY);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5034 次 |
| 最近记录: |