Android的SurfaceView滚动 [英] Android SurfaceView scrolling

查看:844
本文介绍了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.

如果任何人有关于如何进行,这将是多大的AP preciated的指针。

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(这)抢回调 - 使用OnTouch事件来实现滚动行为(即响应时,拿起X,Y位置从MotionEvent并设置适当的偏移)

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).

编辑:

看,这仍然是获​​得意见,但可能值得指出的是,这段时间以来,我已经实现并发布为开放源代码的一个小库,实现了上述功能的框架(被许多评论灵感在这里SO,等等)。它可以在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天全站免登陆