如何绘制大于屏幕尺寸的画布,并且能够在android中水平和垂直滚动/平移? [英] How to draw on canvas greater than screen size and be able to scroll / pan horizontally and vertically in android?
问题描述
我正在寻找一个关于如何在画布上绘制大于屏幕尺寸的示例代码的真实示例(截至目前我正在绘制正常并且无法查看外部屏幕大小)。更重要的是能够滚动/平移屏幕以查看整个画布。
如果需要更多信息,请发表评论。
私人浮动_scaleFactor;
私人浮动_xoffset;
私人浮动_yoffset;
@override
public bool onScale(ScaleGestureDetector检测器){
_scaleFactor * = detector.ScaleFactor;
_scaleFactor = Math.Max(0.1f,Math.Min(_scaleFactor,5.0f));
invalidate();
返回true;
@override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
canvas.save();
canvas.scale(_scaleFactor,_scaleFactor); //用于缩放
canvas.translate(_xoffset,_yoffset); //用于平铺
//正常绘图
画布。恢复();
}
@override
public bool onTouchEvent(MotionEvent e){
switch(e.action){
case MotionEvent.ACTION_DOWN:{
_prevx = e.getX();
_prevy = e.getY();
}
break;
case MotionEvent.ACTION_UP:{
_xoffset + = e.getX() - _prevx;
_yoffset + = e.getY() - _prevy;
invalidate();
_prevx = e.getX();
_prevy = e.getY();
}
break;
}
return _scaleGestureDetector.onTouchEvent(e);
}
是一个自定义VIEW对象 - 所以从View继承并实现IOnScaleGestureListener
I am looking for a real example with sample code on how to draw on canvas greater than screen size ( As of now I am drawing normally and not able to view outside screen size). More stress is on being able to scroll / pan the screen to view the whole canvas. If anymore info is required please comment.
Update: Found my answer here Image in Canvas with touch events
My version is converted from Monodroid, but the implementation should look about the same. (I tried putting it back into java, apologies if it is not exact)
In order to draw outside the screen, just draw where ever you want, it will be drawn outside the screen. The trick is to see it by zooming and panning. For zooming your view needs to implement ScaleGestureDetector.IOnScaleGestureListener and implement the onScale method as below.
For panning you just need to implement the onTouchEvent, which is required for zooming anyway.
private float _scaleFactor; private float _xoffset; private float _yoffset; @override public bool onScale(ScaleGestureDetector detector){ _scaleFactor *= detector.ScaleFactor; _scaleFactor = Math.Max(0.1f, Math.Min(_scaleFactor, 5.0f)); invalidate(); return true; } @override protected void onDraw(Canvas canvas){ super.onDraw(canvas); canvas.save(); canvas.scale(_scaleFactor, _scaleFactor);//for zoom canvas.translate(_xoffset, _yoffset);//for pan //DO NORMAL DRAWING HERE canvas.restore(); } @override public bool onTouchEvent(MotionEvent e){ switch (e.action){ case MotionEvent.ACTION_DOWN:{ _prevx = e.getX(); _prevy = e.getY(); } break; case MotionEvent.ACTION_UP:{ _xoffset += e.getX() - _prevx; _yoffset += e.getY() - _prevy; invalidate(); _prevx = e.getX(); _prevy = e.getY(); } break; } return _scaleGestureDetector.onTouchEvent(e); }
NOTE: This code is for a custom VIEW object - so inherit from View and implement IOnScaleGestureListener
这篇关于如何绘制大于屏幕尺寸的画布,并且能够在android中水平和垂直滚动/平移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!