【问题标题】:How to draw on canvas greater than screen size and be able to scroll / pan horizontally and vertically in android?如何在大于屏幕尺寸的画布上绘制并能够在 android 中水平和垂直滚动/平移?
【发布时间】:2023-03-21 21:33:01
【问题描述】:

我正在寻找一个真实示例,其中包含有关如何在大于屏幕尺寸的画布上绘制的示例代码(截至目前,我正在正常绘制并且无法查看外部屏幕尺寸)。更多的压力在于能够滚动/平移屏幕以查看整个画布。 如果需要更多信息,请发表评论。

更新:在这里找到我的答案Image in Canvas with touch events

【问题讨论】:

    标签: android graphics android-canvas android-scrollview


    【解决方案1】:

    我的版本是从 Monodroid 转换而来的,但实现看起来应该差不多。 (我试着把它放回java,如果不准确,请道歉)

    1. 为了在屏幕外绘制,只要在你想画的地方画,它就会在屏幕外绘制。诀窍是通过缩放和平移来查看它。要缩放您的视图,需要实现 ScaleGestureDetector.IOnScaleGestureListener 并实现 onScale 方法,如下所示。

    2. 对于平移,您只需要实现 onTouchEvent,无论如何缩放都是必需的。

      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);
      }
      

    注意:此代码用于自定义 VIEW 对象 - 因此从 View 继承并实现 IOnScaleGestureListener

    【讨论】:

    • 我正在使用 SurfaceView,无法覆盖 ScaleGestureDetector。有什么解决方法吗?
    • 这会带来更多问题——我这个周末使用的是 SurfaceView,你不能在表面视图上绘图。您必须制作一个覆盖视图,然后将它们都放在 FrameLayout 中。(stackoverflow.com/questions/2933882/…)虽然,当我刚才进一步查看时 - 我确实找到了这个:android-coding.blogspot.com/2011/05/drawing-on-surfaceview.html 但至于平移和缩放我不确定是否我会帮你的。我还是觉得最好的方法是叠加法。
    【解决方案2】:

    您可以简单地将整个画布放在ScrollView 中。这样,Android 会为您处理所有滚动。但是,如果您希望能够让用户与 Canvas 进行交互而不是滚动,则需要拦截来自 ScrollView 的一些触摸事件。可以通过here 找到这样做的示例。

    【讨论】:

    • 我知道这是一篇旧帖子,但我想知道您是否愿意在此链接中查看相关问题:stackoverflow.com/questions/16392717/how-to-create-一个捏缩放可滚动的gridview-for-a-strategy-game。如果您能提供帮助,非常感谢。
    【解决方案3】:
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        CustomView customView;
        ScrollView scroll_view;
        HorizontalScrollView h_scroll_view;
        super.onCreate(savedInstanceState);
        scroll_view = new ScrollView(this);
        h_scroll_view = new HorizontalScrollView(this);
        customView = new CustomView(this);
    
        scroll_view.addView(customView);
        h_scroll_view.addView(scroll_view);
        setContentView(h_scroll_view);
    }
    
    
    public class CustomView extends View
    {
        private Paint paint;
        Context app_context;
    
        public CustomView(Context context) {
            super(context);
            paint = new Paint();
            paint.setColor(Color.GRAY);
            app_context = context;
        }
    
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
        {
            int width = 1000;
            int height = 1200;
            setMeasuredDimension(width, height);
        }
    
        @Override
        public void onDraw(Canvas canvas)
        {
           // ToDo: Put drawing code in here
                }
            }
        }
    }
    

    【讨论】:

    • 我的版本是做垂直和水平滚动查看#Canvas绘图的最简单方法。
    • 对您所做的更改以及现在的工作方式提供一个简短的解释被认为是一种很好的礼仪。
    猜你喜欢
    • 2016-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多