简体   繁体   English

从Webview获取图像到ImageView

[英]Getting image from Webview to ImageView

I have a custom WebView . 我有一个自定义的WebView Inside it I've got an image. 在里面,我有一张图片。 The WebView is zoomable. WebView是可缩放的。 I want to reset zoom when I click a button, extract image from there and place inside ImageView . 我想在单击按钮时重设缩放,从那里提取图像并放在ImageView

So far, with the help of x-code I've managed to write this in onClickListener : 到目前为止,借助x代码,我设法在onClickListener编写了此代码

while (wv2.zoomOut()){
    wv2.setDrawingCacheEnabled(true);
    bitmap = Bitmap.createBitmap(wv2.getDrawingCache());
    bitmapPath = bitmap;
    wv2.setDrawingCacheEnabled(false);

    new Thread(new Runnable() {
        public void run() {
            SaveImage(bitmap);
        }
    }).start();

    tstImage.setImageBitmap(bitmap);
}

The WebView zooms out completely, but the Bitmap I get in ImageView is slightly zoomed in. I need to click 1 more time to get the whole bitmap inside the ImageView WebView完全缩小,但是ImageView获得的Bitmap会稍微放大。我需要再单击1次才能将整个bitmap放入ImageView

I would say that the Javadoc of WebView.getDrawingCache() method explains your problem. 我会说WebView.getDrawingCache()方法的Javadoc解释了您的问题。

public Bitmap getDrawingCache (boolean autoScale)

... ...

Note about auto scaling in compatibility mode: When auto scaling is not enabled, this method will create a bitmap of the same size as this view . 有关在兼容模式下自动缩放的注意事项:如果未启用自动缩放功能,则此方法将创建与该视图相同大小的位图 Because this bitmap will be drawn scaled by the parent ViewGroup, the result on screen might show scaling artifacts . 由于此位图将由父ViewGroup按比例绘制, 因此屏幕上结果可能会显示比例尺伪影 To avoid such artifacts, you should call this method by setting the auto scaling to true . 为避免此类伪影,应通过将自动缩放设置为true来调用此方法 Doing so, however, will generate a bitmap of a different size than the view. 但是,这样做将生成与视图大小不同的位图。 This implies that your application must be able to handle this size. 这意味着您的应用程序必须能够处理此大小。

In your code you simply have to make this change 在您的代码中,您只需要进行此更改

bitmap = Bitmap.createBitmap(wv2.getDrawingCache(true));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM