将canvas绘制到Android WebView中并进行缩放

发布于 2024-12-24 21:53:52 字数 1222 浏览 1 评论 0原文

我是 Android 编程新手。我想在图像上画线并允许用户放大和缩小。由于 ImageView 似乎没有内置缩放控件,因此我决定使用 WebView。我扩展了 WebView 类并重写了 onDraw 方法,以便我在画布上绘制线条。然后我将图像加载到 WebView 中。这显示了我在画布上绘制的线条,这些线条放置在 Web 视图中的图像上。

虽然我可以在 WebView 中放大和缩小图像,但我在画布中绘制的线条不会相应缩放;我的图像被放大,但线条(画布)在屏幕上保持相同的大小。当我放大 WebView 时,是否有某种方法也可以放大画布?

此代码位于我的主要活动中:

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

此代码位于我的 CustomWebView 中,并绘制一条直线:

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 中的画布上,然后在其上绘制线条。

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

但这不允许我放大画布。另外,平移图像时速度确实非常慢。

我应该选择这个而不是使用 WebView 方式吗?如果是的话,是否有某种方法可以提高效率?或者有更好的方法来实现我想要实现的目标吗?

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.

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?

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

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

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

莫多说 2024-12-31 21:53:52

使用该行

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

CustomWebView 的构造函数中

use

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

that lines in constructor of CustomWebView

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文