作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的 Android 客户端代码:我正在向客户端发送屏幕截图图像,它在客户端接收并使用 ImageView 在 UI 中显示。
public class ScreenCast extends Activity{
ImageView iv;
Socket socket;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyTask task=new MyTask();
task.execute();
}
private class MyTask extends AsyncTask<Void, Void, Bitmap>{
@Override
protected void onPostExecute(Bitmap bitmap) {
if(bitmap!=null){
Drawable ob=new BitmapDrawable(getResources(),bitmap);
iv=(ImageView) findViewById(R.id.imageView1);
iv.setBackgroundDrawable(ob);
}else{
Log.d("Bitmap null","Empty");
}
}
@Override
protected Bitmap doInBackground(Void... arg0){
try {
socket=new Socket("192.168.237.1",3434);
ObjectInputStream ois=new ObjectInputStream(socket.getInputStream());
byte[] buffer=(byte[]) ois.readObject();
BitmapFactory.Options options=new BitmapFactory.Options();
options.inMutable=true;
bitmap = BitmapFactory.decodeByteArray(buffer, 0, buffer.length,options);
return bitmap;
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
}
}
现在我遇到的问题是 第一个屏幕截图已显示,但图像未流式传输(如视频)。但是当我改变方向时,更新的屏幕截图显示。那么如何让屏幕截图图像在 UI 中连续显示
按照 Mozarty 的建议更新我的代码后的 Logcat。
01-13 18:12:29.665: E/AndroidRuntime(3915): FATAL EXCEPTION: AsyncTask #3
01-13 18:12:29.665: E/AndroidRuntime(3915): java.lang.RuntimeException: An error occured while executing doInBackground()
01-13 18:12:29.665: E/AndroidRuntime(3915): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-13 18:12:29.665: E/AndroidRuntime(3915): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-13 18:12:29.665: E/AndroidRuntime(3915): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-13 18:12:29.665: E/AndroidRuntime(3915): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-13 18:12:29.665: E/AndroidRuntime(3915): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-13 18:12:29.665: E/AndroidRuntime(3915): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-13 18:12:29.665: E/AndroidRuntime(3915): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-13 18:12:29.665: E/AndroidRuntime(3915): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-13 18:12:29.665: E/AndroidRuntime(3915): at java.lang.Thread.run(Thread.java:856)
01-13 18:12:29.665: E/AndroidRuntime(3915): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-13 18:12:29.665: E/AndroidRuntime(3915): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4867)
01-13 18:12:29.665: E/AndroidRuntime(3915): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:947)
01-13 18:12:29.665: E/AndroidRuntime(3915): at android.view.View.requestLayout(View.java:15430)
01-13 18:12:29.665: E/AndroidRuntime(3915): at android.view.View.requestLayout(View.java:15430)
01-13 18:12:29.665: E/AndroidRuntime(3915): at android.view.View.requestLayout(View.java:15430)
01-13 18:12:29.665: E/AndroidRuntime(3915): at android.view.View.requestLayout(View.java:15430)
01-13 18:12:29.665: E/AndroidRuntime(3915): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:292)
01-13 18:12:29.665: E/AndroidRuntime(3915): at android.view.View.requestLayout(View.java:15430)
01-13 18:12:29.665: E/AndroidRuntime(3915): at android.view.View.setBackgroundDrawable(View.java:14589)
01-13 18:12:29.665: E/AndroidRuntime(3915): at com.qube.screenshotapk.ScreenShotNew$MyTask.onProgressUpdate(ScreenShotNew.java:44)
01-13 18:12:29.665: E/AndroidRuntime(3915): at com.qube.screenshotapk.ScreenShotNew$MyTask.doInBackground(ScreenShotNew.java:67)
01-13 18:12:29.665: E/AndroidRuntime(3915): at com.qube.screenshotapk.ScreenShotNew$MyTask.doInBackground(ScreenShotNew.java:1)
01-13 18:12:29.665: E/AndroidRuntime(3915): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-13 18:12:29.665: E/AndroidRuntime(3915): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-13 18:12:29.665: E/AndroidRuntime(3915): ... 5 more
最佳答案
你只运行一次任务,那只会得到一张图像
将此行添加到您的帖子执行以获取另一张图片
MyTask task=new MyTask();
task.execute();
---更新---
检查此代码以帮助您解决延迟问题
public class ScreenCast extends Activity {
ImageView iv;
Socket socket;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyTask task = new MyTask();
task.execute();
}
private class MyTask extends AsyncTask<Void, Bitmap, Void> {
@Override
protected void onPostExecute(Void result) {
//all images loaded
super.onPostExecute(result);
}
@Override
protected void onProgressUpdate(Bitmap... values) {
super.onProgressUpdate(values);
Bitmap bitmap = values[0];
if (bitmap != null) {
Drawable ob = new BitmapDrawable(getResources(), bitmap);
iv = (ImageView) findViewById(R.id.imageView1);
iv.post(new Runnable (){
iv.setBackgroundDrawable(ob);
});
} else {
Log.d("Bitmap null", "Empty");
}
}
@Override
protected Void doInBackground(Void... arg0) {
//change this to your stopping condition i.e no more images
while (true) {
try {
socket = new Socket("192.168.237.1", 3434);
ObjectInputStream ois = new ObjectInputStream(
socket.getInputStream());
byte[] buffer = (byte[]) ois.readObject();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inMutable = true;
Bitmap bitmap = BitmapFactory.decodeByteArray(buffer, 0,
buffer.length, options);
onProgressUpdate(bitmap);
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
关于android - 如何在 Android 中保持 UI 刷新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27885857/
我是一名优秀的程序员,十分优秀!