我希望public class ScreenActivity extends AppCompatActivity {
ImageView img;
int width,height;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_screen);
img=(ImageView)findViewById(R.id.imageView);
Display display = getWindowManager().getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
// while (true)
for (int i=0;i<100;i++)
new GetImg().execute();
}
Bitmap imgscr;
public class GetImg extends AsyncTask<Object,Void,Bitmap> {
@Override
protected Bitmap doInBackground(Object[] params) {
Socket client= null;
try {
client = new Socket("192.168.1.5",6767);
InputStream in=client.getInputStream();
imgscr=Bitmap.createScaledBitmap(BitmapFactory.decodeStream(in), width, height, false);
} catch (Exception e) {
e.printStackTrace();
}
return imgscr;
}
@Override
protected void onPostExecute(Bitmap bm)
{
img.setImageBitmap(bm);
}
}
}
和函数修改变量close
,使填充更大,直到它等于变量gate
。我不知道为什么它不起作用,请帮忙。
open
答案 0 :(得分:2)
JS中的超时没有阻塞 - 它们在一个单独的线程中运行。关于他们如何在线工作的资源很多,所以我不会深入研究。
要修复代码,您可以重构是进行递归调用:
var closed = 1;
var opened = 19;
var gate = opened;
var gateEl = document.getElementById('gate');
function close() {
if (gate !== closed) {
gate -= 1;
gateEl.style.paddingTop = gate + '%'; // open stays the same
setTimeout(close, 100);
}
}
close
函数gate
代替open
来实际更改填充这是一个实例:
var closed = 1;
var opened = 19;
var gate = opened;
var gateDiv = document.getElementById('gate');
function close() {
console.log(gate);
if (gate > closed) {
gate -= 1;
gateDiv.style.paddingTop = gate + '%';
setTimeout(close, 100);
}
}
close();
#gate {
height: 50px;
width: 50px;
background: green;
}
<div id="gate"></div>
答案 1 :(得分:1)
更好的实施方式如下:
// gate element
var gate = document.getElementById("gate");
// the space between the gate and the ground
var gap = 100;
// how many pixels/percentage you want it to move at each iteration
var increment = -2;
function close() {
// gap = 0 means it's closed
// as long as it's not closed run this
if (gap > 0) {
// decrease the gap
gap = gap + increment;
gate.style.top = (gap * -1) + "%";
} else {
// gate is closed, so we clear the interval
clearInterval(closing);
}
}
// a reference to interval
var closing = setInterval(close, 100);
&#13;
#container {
width: 300px;
height: 100px;
border: 1px solid rgba(0, 0, 0, .2);
position: relative;
overflow: hidden;
}
#gate {
width: 100%;
height: 100%;
background: brown;
position: absolute;
top: -100%;
}
&#13;
<div id="container">
<div id="gate"></div>
</div>
&#13;
答案 2 :(得分:0)
这是一个非常高级的话题!我强烈建议您阅读Promise
和async
/await
。拥有异步编程和JavaScript的经验也很有帮助。
将来你可以使用async
/await
来写这个和你一样:
function wait() {
return new Promise(function(resolve) {
setTimeout(resolve, 100)
});
}
async function close() {
var closed = 1;
var opened = 19;
var gate = opened;
while (gate !== closed) {
gate -= 1;
gateEl.style.paddingTop = open + '%';
await wait();
}
}
这有几个不同的部分,所以我会仔细阅读..
首先,我修复了您的代码的几个常见问题:
gate.style.paddingTop = open'%';
// changed to:
gateEl.style.paddingTop = open + '%';
// First, we need to join the open variable and the '%' string, so
// I add a + for that; second, gate is a number, not a DOM element,
// so I assumed there is a new variable called "gateEl" that means
// the actual element. Supply that in the actual code.
然后我在代码中实现了async
/ await
。这涉及一些变化。我重新定义了函数wait
:
function wait() {
return new Promise(function(resolve) {
setTimeout(resolve, 100)
});
}
看起来有点令人困惑,但基本上我给出了一个函数参数的新Promise
对象。 Promise
使async
/ await
成功。在我的wait
函数中,当调用await wait()
时,在100毫秒后调用resolve
。 resolve
表示await wait()
之后的代码可以运行。
接下来,我在async
之前添加了function close() {...}
- 这使await
在其中工作。
最后,我将wait()
替换为await wait()
。这会将您的浏览器从“调用wait
函数”改为“调用wait
函数”,然后等到promise 实际上是可用对象 < / sup>它回归解决。“而且我们知道,在wait
被调用之后,承诺会解决100ms - 因为我们将其定义为这样做。
关于await fn()
的好处是循环和其他控制结构能够很好地工作。