Android SurfaceView 滚动 [英] Android SurfaceView scrolling

查看:30
本文介绍了Android SurfaceView 滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 android 应用程序,该应用程序构建一个(比如说)640*480 像素的屏幕外位图,并将其显示在 SurfaceView 中.请注意,图片尺寸大于手机实际显示尺寸.

I'm writing an android application that builds an offscreen bitmap that is (say) 640*480 pixels and displays that in a SurfaceView. Note that the size of the image is larger than the actual display on the handset.

应用程序的工作方式是它最初显示图像的左上角 (0,0),然后我希望能够滚动图像.

The way that the application works is that it initially displays the upper left corner (0,0) of the image and then I would like to be able to scroll around the image.

我的Surfaceview类实现了onMeasure回调,返回大小为1024*768,显示了手机屏幕对应的图片区域.我想实现对滚动的支持,由于文档非常有限(只是一堆没有一致性的类和调用),我发现这几乎是不可能的.我试过只调用 scrollBy 但不起作用.

My Surfaceview class implements the onMeasure callback and returns a size of 1024*768 which displays the area of the image that corresponds to the phone screen. I'd like to implement support for scrolling which I'm finding near impossible due to the very limited documentation (just bunches of classes and calls with no coherency). I've tried just calling scrollBy and that doesn't work.

如果有人有任何关于如何进行的指示,我们将不胜感激.

If anyone has any pointers about how to proceed this would be much appreciated.

推荐答案

一旦你开始使用 SurfaceView,你就进入了一个世界,在那里你有一个空白的像素区域,你可以自己绘制它们如何你要.您自己做所有事情 - 这意味着您也需要自己实现滚动.做起来并不容易,但也不是太难.

Once you start using a SurfaceView, you are moving yourself into a world where you have a blank area of pixels, and you are on your own to draw them how you want. You do everything yourself - and that means you need to implement scrolling yourself as well. It's not trivial to do, but neither is it too difficult.

一种方法如下:- 扩展 SurfaceView 并实现 SurfaceHolder.Callback- 使用 getHolder().addCallback( this ) 获取回调- 使用 OnTouch 事件来实现滚动行为(即,响应时,从 MotionEvent 中选取 x,y 位置并适当设置偏移量).

One way to do it is the following: - Extend SurfaceView and implement SurfaceHolder.Callback - Use getHolder().addCallback( this ) to grab the callback - Use OnTouch events to implement the scrolling behavior (i.e., when responding, pick up the x,y position from the MotionEvent and set the offset appropriately).

实现一个线程来处理图像的更新.网上有这样的例子(抱歉 - 现在没有任何可用的).

Implement a thread to handle the updating of the image. There are examples of this around on the web (sorry - don't have any to hand right now).

或者,使用屏幕外位图并由普通 ImageView 绘制(将 ImageView 调整为位图大小并将其放置在滚动视图中).

Alternatively, use an off-screen bitmap and have it drawn by a normal ImageView (size the ImageView to the size of the bitmap and place it in a scroll view).

看到这仍然获得了意见,可能值得指出的是,从这一次开始,我已经实现并作为开源发布了一个小型库,该库实现了上述功能的框架(受到许多评论的启发)所以,等等).它可以在 Github 上找到:https://github.com/micabyte/android_game

Seeing that this is still getting views, it might be worth pointing out that since this time, I've implemented and released as open-source a small library that implements a framework for the above functionality (inspired by many comments here on SO, among others). It can be found on Github: https://github.com/micabyte/android_game

这篇关于Android SurfaceView 滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆