在显示上Surfaceview帆布位图 [英] Displaying a Bitmap on Canvas in Surfaceview

查看:190
本文介绍了在显示上Surfaceview帆布位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个Android应用程序中显示变焦能力,泛能图,(这仅仅是一个位图图像),也允许用户点击某些点在地图上。

I am trying to develop an android application to display a zoom-able, pan-able map, (which is just a bitmap image), that also allows the user to click on certain points on the map.

我苦苦寻找实施这样一个应用程序的最有效方法。尝试多种其他的方法(包括网页视图,的OpenLayers),最好的办法,(根据这个网站至少)后,好像是用一个画布SurfaceView。在code我至今从片段拼凑起来,我发现在互联网上,所以它只是崩溃。我在这里希望它列入反正它给了什么,我想实现的一些想法:

I am struggling to find the most effective way of implementing such an app. After trying numerous other methods, (including Webviews, OpenLayers), the best way, (according to this site at least), seems to be using a SurfaceView with a Canvas. The code I have so far is pieced together from snippets I found all over the internet, so it just crashes. I have included it here anyway in the hope it gives some idea of what I am trying to achieve:

public class Main extends Activity {

private Bitmap bmp; 
    private SurfaceView surfaceView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        surfaceView = (SurfaceView)findViewById(R.id.surface);
        bmp = BitmapFactory.decodeResource(getResources(),
             R.drawable.testmapbmp);
        //decode the bitmap file

        Canvas canvas = new Canvas(bmp);
        //create a canvas from the bitmap, then display it in surfaceview
        surfaceView.draw(canvas);
    }
}

这是实现我要为我的应用程序功能的最佳方式?还是有更好的办法,尤其是如果我能在以后遇到问题,实现地图的可点击的部分,例如

Is this the best way to achieve the functionality I want for my application? Or is there a better way, especially if I could run into problems later on, implementing the clickable sections of the map, for example.

我真的需要从一个谁创造了这样的事情以前有人帮助,所以我可以让我的火车的深思熟虑的问题,但我非常AP preciate任何帮助/指针/提示了!

I really need help from someone who has created something like this before, so I can get my train-of-thought on the problem, but I greatly appreciate any help/pointers/tips at all!

感谢

推荐答案

您一张code是不是真的正确。

Your piece of code is not really correct.

Usualy如果你想,如果你需要的东西(通过继承视图或SurfaceView)来创建自己的自定义视图做这样的。在你看来,你的子类需要重写方法的onDraw(Canvas)的并用帆布方法之一绘制在画布上的位图。

Usualy if you want to do this kind if things you need to create your own custom view (either by inheriting View or SurfaceView). In your View subclass you need to override method onDraw(Canvas) and draw your bitmap on canvas by using one of canvas methods.

要使它可平移和捏缩放,您需要使用滚轮 ScaleGestureDetector 类。通常平移缩放或通过应用仿射变换到画布上用它做的方法(旋转,缩放,平移)。

To make it pannable and pinch-zoomable you need to use Scroller and ScaleGestureDetector classes. Usually panning or scaling is done by applying affine transformations to a canvas by using it's methods (rotate, scale, translate).

SurfaceView和View之间的区别是,在SurfaceView可以从单独的线程,这意味着你可以组织排序渲染循环那里并从中直接绘制。这是良好的简单的游戏和动画效果。我相信,绘制一个位图,这是矫枉过正和更简单的子类化视图的目的。

Difference between SurfaceView and View is that in SurfaceView you can draw directly from separate thread, which means you can organize sort of rendering loop there and draw from it. It is good for simple games and animation effects. I believe, for purpose of drawing a bitmap this is overkill and much simpler to subclass View.

我与并的这个,如果​​我是你。

I'd start with this and this, if I were you.

这篇关于在显示上Surfaceview帆布位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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