WebView无法正确缩放?(WebView not scaling correctly?)
我正在使用WebView将HTML转换为图像。 HTML具有600px的固定(使用CSS定义)。
WebView布局是:
<WebView android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbars="none"/>
在加载html文档时,我通过执行以下操作来获取webview大小:
int width = webView.getMeasuredWidth(); int height = webView.getMeasuredHeight();
我注意到的第一个奇怪的事情是宽度是650.文档显示,我需要水平滚动才能查看它(lo-res设备)。
下一步是将webview转换为图像:
bitmap = Bitmap.createBitmap(600, height, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(bitmap); webView.draw(c);
但是,将webview转换为位图会从右侧部分(webview中不可见的区域)中移除。
当我在HTML文档中定义600时,为什么webview宽度为650? 为什么成像裁剪?
I'm using a WebView to convert an HTML to an image. The HTML has a fixed with of 600px (defined using CSS).
The WebView layout is:
<WebView android:layout_width="fill_parent" android:layout_height="wrap_content" android:scrollbars="none"/>
On loading the html document, I get the webview size by doing:
int width = webView.getMeasuredWidth(); int height = webView.getMeasuredHeight();
The first weird thing that I notice is that the width is 650. The document is displayed and I need to scroll horizontally to view it all (lo-res device).
The next step is to convert the webview into an image:
bitmap = Bitmap.createBitmap(600, height, Bitmap.Config.ARGB_8888); Canvas c = new Canvas(bitmap); webView.draw(c);
However, converting the webview into the bitmap crops off the right part (the area where is not visible in the webview).
Why is the webview width 650 when I defined 600 in my HTML document? And why is imaged cropped?
原文:https://stackoverflow.com/questions/31378150
最满意答案
从MDN上的箭头功能文章:
(param1, param2, …, paramN) => { statements } (param1, param2, …, paramN) => expression
您可以看到,无支撑语法要求箭头右侧的代码是一个表达式 ,这是语言本身所做的(不幸的) 区分 。
由于
debugger
是一种语句,因此在任何需要使用表达式的地方使用它都是语法错误。 有一件事你可以去解决这个问题,就是把你的调试器语句转换成一个表达式,然后你就可以把JavaScript当作评估对象而不是返回,例如:function debug(args) { debugger; return true; } params => debug() && params + 1 // or params => console.log(params) || params + 1
这样做的方式是因为逻辑运算符在JavaScript中的作用方式,这是真的:
truthyA && B === B falsyA || B === B
链接逻辑运算符时,JavaScript会从左到右评估子表达式,然后根据其布尔等效值进行操作。 这就是为什么你有时会看到
&&
用来代替if
语句:if (smth) doStuff(); // is equivalent to: smth && doStuff();
From the MDN article on arrow functions:
(param1, param2, …, paramN) => { statements } (param1, param2, …, paramN) => expression
You can see that the brace-less syntax requires the code on the right of the arrow to be an expression, which is an (unfortunate) distinction made by the language itself.
Since
debugger
is a statement, using it anywhere an expression is expected is a syntax error. One thing you could to to work around this is to transform your debugger statement in an expression which you trick JavaScript into evaluating but not returning, e.g.:function debug(args) { debugger; return true; } params => debug() && params + 1 // or params => console.log(params) || params + 1
The way this works is that because of the way logical operators function in JavaScript, this is true:
truthyA && B === B falsyA || B === B
When chaining logical operators, JavaScript evaluates sub-expressions left to right and then act depending on their boolean equivalent. That's why you'll sometimes see
&&
used in place ofif
statements:if (smth) doStuff(); // is equivalent to: smth && doStuff();
相关问答
更多-
如何在ES6箭头函数调用的函数中使用`this`?(How to use `this` in a function called by an ES6 arrow function? [duplicate])[2023-07-02]
this在handleAuthResult是undefined ,因为handleAuthResult不是箭头函数,因此没有词法。 这是一个普通的原型函数(松散地,“方法”),这意味着它是由它的调用方式定义的。 你打电话的方式: authenticate(callback) { callback({ value: true }); } 不会this设置为任何特定的,因此this是undefined (因为您处于严格模式)。 要解决this问题,请将其传递给authentic ... -
箭头功能提供了一个词汇。 它使用在评估函数时可用的this一点。 它在逻辑上相当于(以下不是有效的代码,因为你不能有一个名为this的变量): (function(this){ // code that uses "this" })(this) 在你的第一个例子中,箭头函数在构造函数中,并且指向新生成的实例。 在你的第三个例子中,没有使用箭头函数,标准的this行为一如既往(这在函数范围中)。 在你的第二个例子中,你使用了一个箭头函数,但是在它被评估的范围内, this是全局/未定义的。 Arro ...
-
为什么我们必须在es6箭头函数中用括号括起来[重复](Why we have to wrap throw with brackets in es6 arrow functions [duplicate])[2022-12-10]
箭头函数定义如下: ArrowFunction[In, Yield] : ArrowParameters[?Yield] [no LineTerminator here] => ConciseBody[?In] ConciseBody[In] : [lookahead ≠ { ] AssignmentExpression[?In] { FunctionBody } 而throw是一个声明: ThrowStatement[Yield] : throw [no LineTer ... -
从MDN上的箭头功能文章: (param1, param2, …, paramN) => { statements } (param1, param2, …, paramN) => expression 您可以看到,无支撑语法要求箭头右侧的代码是一个表达式 ,这是语言本身所做的(不幸的) 区分 。 由于debugger是一种语句,因此在任何需要使用表达式的地方使用它都是语法错误。 有一件事你可以去解决这个问题,就是把你的调试器语句转换成一个表达式,然后你就可以把JavaScript当作评估对象而不是返回, ...
-
在这个特殊情况下,库正在改变this在decorate回调中引用的内容。 使用箭头函数( => )时, this与外部作用域的作用相同。 这意味着你基本上坚持使用这个function 。 In this particular case, the library is changing what this refers to inside the callback for decorate. When using arrow functions (=>), this is equivalent to the ...
-
您可以使用event.currentTarget来引用处理程序的目标元素 $(document).on('click', '.inserted-el', function(event) { snippet.log('old -> ' + this.innerHTML + ':' + event.currentTarget.innerHTML); }); $(document).on('click', '.inserted-el', (event) => { snippet.log('new ...
-
如何使用带有promise绑定的ES6箭头函数(bluebird)(How to use ES6 arrow functions with promise binding (bluebird))[2023-05-25]
如果你不想要词汇限制,那么就不需要使用箭头功能了。 如果你想动态绑定this ,只是不要使用箭头功能。 当然,您可以废弃.bind({})作为一个整体,并使用通过将所有内容放入对象方法(或示例中的IIFE)中绑定到对象的箭头函数: (function() { this; // the value that everything is bound to double(2).then(result => { this.double = result; // store result here ... -
为什么在浏览器中使用ES6(Why use ES6 in browsers)[2022-05-24]
在所有使用的浏览器都支持使用ES6语法之前,它是否聪明? 不是真的。 你可以将它转换为例如babel (这就是你举起的Bootstrap v4, 也是如此 )。 实际上,即使您确保使用现代JS运行时,也可以使用babel进行转换,以使用尚未完全标准化的语言功能 。 重点不是新的用户可见功能,而是程序员的工作效率,您可以通过ES6以多种方式提升。 您将在此博客文章中找到有关如何使用babel转换浏览器预期代码的明确说明。 因为在没有ES6的情况下,ES6可能实现的所有功能都是可能的,所以我看不出如何使用新语法 ... -
ES6箭头功能[重复](ES6 arrow functions [duplicate])[2023-07-26]
示例一和示例二之间的主要区别在于示例一使用调用范围,而示例二使用Meteors范围。 如果我不得不猜测它会不起作用,因为你正在使用this并期望一个不同的范围。 这是一个演示此功能的快速示例... (function () { var Example = (function () { function Example() { setTimeout(function() { console.log(this); //this == ... -
我想你想要的 var F = s => /[^aeiou]{3}|[jqxz]/.test(s); 这会创建一个箭头函数 ,它接收一个参数s并返回/[ /[^aeiou]{3}|[jqxz]/.test(s) 。 并且该函数被赋值给变量F 请注意,您还可以使用在ES5中工作的不太长的替代方案,并且应该执行得更好,因为它不会在每次调用时重新创建正则表达式对象。 var F = /(?:)/.test.bind(/[^aeiou]{3}|[jqxz]/); I think you want var F = ...