如何在Android中保持UI刷新?

时间:2015-01-11 10:05:48

标签: android android-asynctask

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

现在我遇到的问题是  显示第一个屏幕截图,但图像未进行流式处理(如视频)。但是当我更改方向时,更新的屏幕截图显示。 那么如何在屏幕上连续显示截图图像

按照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

2 个答案:

答案 0 :(得分:4)

您只运行一次任务,只能获得一个图像

将此行添加到您的帖子上,以获取另一张图片

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

答案 1 :(得分:0)

试试这个逻辑:

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