如何在 android 中保持 UI 刷新?

How to keep UI refreshing in android?

这是我的 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

中连续显示

Logcat 按照 Mozarty 的建议更新我的代码后。

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.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.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.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

你 运行 任务只有一次,而且你只能得到一张图片

将此行添加到您的 post 执行以获取另一张图片

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

试试这个逻辑:

public class ScreenCast extends Activity {
    ImageView iv;
    Socket socket;
    ObjectInputStream ois;

    @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 onPreExecute() {
      socket = new Socket("192.168.237.1", 3434);
      ois = new ObjectInputStream(
                        socket.getInputStream());
       }

    @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.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 {
                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();
            }
        }
     }
 }