如何立即刷新图像视图 [英] How to refresh image view immediately

查看:102
本文介绍了如何立即刷新图像视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序,我从云端下载图像。下载是在一个线程执行和在同一线程我还设置与新下载的图像的图像图。设置图像后,我再调用 postinvalidate()

In my android app, I download an image from the cloud. The download is performed in a thread and in that same thread I also set an image view with the newly downloaded image. After setting the image, I then call postinvalidate().

但图像不会立即显示出来。有没有一种方法,使重绘立即执行?我需要一种方法来触发绘图周期。

However the image does not immediately show up. Is there a way to make the redraw happen immediately? I need a way to trigger a drawing cycle.

推荐答案

这是从View类派生的每个类有无效() postInvalidate()方法。如果无效()被调用它告诉,目前来看已经修改了系统,它应该被重新绘制的尽快。由于这种方法只能从被称为您的 UIThread 另一种方法是必要的,当你的不可以 UIThread 并仍想通知系统你的观点已经发生了变化。该 postInvalidate()方法通知系统从非UIThread 并查看被重新绘制在接下来的事件循环的尽快的UIThread。

Each class which is derived from the View class has the invalidate() and the postInvalidate() method. If invalidate() gets called it tells the system that the current view has changed and it should be redrawn as soon as possible. As this method can only be called from your UIThread another method is needed for when you are not in the UIThread and still want to notify the system that your View has been changed. The postInvalidate() method notifies the system from a non-UIThread and the View gets redrawn in the next eventloop on the UIThread as soon as possible.

在你的情况,你可以达到你想要的的AsyncTask 的帮助(智能底色线程).AsyncTask能够正确且易于使用的用户界面线程。该类允许无需操作线程和/或处理(在你的情况下载图像背景)进行后台操作并公布结果(设为您的位图到您的ImageView)在UI线程上。

In your case you can achieve what you want with the help of AsyncTask (an intelligent backround thread).AsyncTask enables proper and easy use of the UI thread. This class allows to perform background operations (download image in background in your case) and publish results (set your bitmap to your ImageView) on the UI thread without having to manipulate threads and/or handlers.

这是异步任务是由在后台线程运行的计算,其结果发表在UI线程上定义。异步任务由 3一般类型,叫PARAMS,进展和成效,和 4步骤,叫开始,doInBackground,processProgress和最终确定。

An asynchronous task is defined by a computation that runs on a background thread and whose result is published on the UI thread. An asynchronous task is defined by 3 generic types, called Params, Progress and Result, and 4 steps, called begin, doInBackground, processProgress and end.

4步骤

当执行一个异步任务,任务经过4个步骤:

When an asynchronous task is executed, the task goes through 4 steps:

在preExecute(),执行任务后,立即在UI线程调用。这个步骤是通过示出下载从云图像之前在用户界面的进度条通常用于设置任务,例如而这用于提供良好的用户体验。

onPreExecute(), invoked on the UI thread immediately after the task is executed. This step is normally used to setup the task, for instance by showing a progress bar in the user interface before downloading the image from cloud and that's used to provide a good user experience .

doInBackground(参数...),在后台线程上preExecute后立即调用()完成执行。这个步骤是用于执行后台计算,可以采取很长的时间。异步任务的参数被传递到这一步。计算的结果,必须通过此步骤被返回,将被传递回的最后一步。这一步也可以使用publishProgress(进展...)公布进展情况的一个或多个单元。这些值公布在UI线程上,在onProgressUpdate(进展...)一步。

doInBackground(Params...), invoked on the background thread immediately after onPreExecute() finishes executing. This step is used to perform background computation that can take a long time. The parameters of the asynchronous task are passed to this step. The result of the computation must be returned by this step and will be passed back to the last step. This step can also use publishProgress(Progress...) to publish one or more units of progress. These values are published on the UI thread, in the onProgressUpdate(Progress...) step.

onProgressUpdate(进展...),调用 publishProgress(进展...)后在UI线程上调用。的执行的计时是不确定的。此方法用于显示的任何形式的用户界面中的进展而背景计算仍在执行

onProgressUpdate(Progress...), invoked on the UI thread after a call to publishProgress(Progress...). The timing of the execution is undefined. This method is used to display any form of progress in the user interface while the background computation is still executing.

onPostExecute(结果),后台计算完成后在UI线程调用。后台计算的结果传递到这步作为参数的方法,你可以设置位图到您的ImageView和无效的意见

onPostExecute(Result), invoked on the UI thread after the background computation finishes. The result of the background computation is passed to this step as a parameter in that method you can set the bitmap to your imageView and invalidate your views

这篇关于如何立即刷新图像视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆