绘图画布到的Andr​​oid的WebView和缩放 [英] Drawing canvas into Android WebView and zooming

查看:269
本文介绍了绘图画布到的Andr​​oid的WebView和缩放的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来的Andr​​oid编程。我想画线到图像并允许用户放大和缩小。由于ImageView的似乎并不具有内置缩放控制,我决定使用的WebView。我伸出的WebView类,并覆盖了的onDraw方法,让我画在画布上线。然后我装我的形象到的WebView。这表明我,我在web视图中放置在图像画布绘制线条。

I'm new to Android programming. I want to draw lines onto an image and allow the user to zoom in and out. Since ImageView did not seem to have in-built zoom controls, I decided on using WebView. I extended WebView class and overwrote the onDraw method to let me draw lines on canvas. I then loaded my image into the WebView. This shows me the lines I draw in the canvas placed over the image within the WebView.

虽然我能够进出我的的WebView形象放大,行我画在画布上没有相应的规模;我的图像进行放大,但行(画布)保留在屏幕上的大小相同。是否有某种方式在我的画布也放大,当我在我的WebView放大?

Though I am able to zoom in and out of my image in WebView, the lines I drew in the canvas do not scale accordingly; my image is enlarged, but the line (canvas) remains the same size on the screen. Is there some way to also zoom in my canvas when I zoom in my WebView?

这code是在我的主要活动:

This code is in my main activity:

    final CustomWebView webview = (CustomWebView) findViewById(R.id.webView1);
    webview.loadUrl("file:///android_asset/myimage.png");
    WebSettings websettings;
    websettings = webview.getSettings();
    websettings.setBuiltInZoomControls(true);

这code是在我CustomWebView并绘制一条直线:

This code is in my CustomWebView and draws one straight line:

public CustomWebView (Context context, AttributeSet attrs)
{
    super (context, attrs);
}

@Override
protected void onDraw(Canvas canvas) 
{
    super.onDraw (canvas);

    Paint p = new Paint();
    p.setColor (Color.RED);
    canvas.drawLine (20, 10, 300, 400, p);
}

我也尝试使用位图上直接加载图像在CustomWebView画布上,然后画上线。

I also tried loading the image directly on to the canvas in CustomWebView using a bitmap and then drew the line on it.

map = BitmapFactory.decodeResource(getResources(), R.drawable.demo);
canvas.drawBitmap(map, 0, 0, null);

但是,这并不让我在画布放大。加上它平移图像时真的是很慢的。

But this does not allow me to zoom in the canvas. Plus it is really very slow when panning the image.

我应该选择这个而不是使用的WebView方法是什么?如果是的话,也许有一些方法,使之更有效率?或者是有一些更好的实现我想要实现的方式?

Should I choose this rather than using the WebView way? If yes, is there maybe some way to make it more efficient? Or is there some better way to implement what I'm trying to achieve?

推荐答案

使用

webview.loadUrl("file:///android_asset/myimage.png");
    WebSettings websettings;
    websettings = webview.getSettings();
    websettings.setBuiltInZoomControls(true); 

CustomWebView

这篇关于绘图画布到的Andr​​oid的WebView和缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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