在哪里放置 AsyncTask() 以便我可以调用它来加载图像?
下面是我的代码,当调用此活动时它将加载图像。但我希望在从服务器检索图像时显示进度对话框,而不是仅向用户显示黑屏。我听说 AsyncTask 能够做到这一点,但我坚持在哪里放置 AsyncTask 函数并调用它。
public class LargeImageScroller extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private static class SampleView extends View {
private static Bitmap bmLargeImage; //bitmap large enough to be scrolled
private static Rect displayRect = null; //rect we display to
private Rect scrollRect = null; //rect we scroll over our bitmap with
private int scrollRectX = 0; //current left location of scroll rect
private int scrollRectY = 0; //current top location of scroll rect
private float scrollByX = 0; //x amount to scroll by
private float scrollByY = 0; //y amount to scroll by
private float startX = 0; //track x from one ACTION_MOVE to the next
private float startY = 0; //track y from one ACTION_MOVE to the next
public SampleView(Context context) {
super(context);
displayRect = new Rect(0, 0, displayWidth, displayHeight);
scrollRect = new Rect(0, 0, displayWidth, displayHeight);
bmLargeImage = Bitmap.createBitmap(1035 , 665, Config.ARGB_8888);
bmLargeImage = createMap(this.getContext());
}
public boolean onTouchEvent(MotionEvent event) {....}
protected void onDraw(Canvas canvas) {....}
private static Bitmap createMap(Context context) {...}
private static Drawable LoadImageFromWebOperations(String url) {....}
private static Bitmap decodeFile(File f, int requiredSize) {...}
}
}
我想知道我应该在哪里放置 AsyncTask 函数,以便在程序从服务器/缓存加载图像时显示进度对话框。我在互联网上找到的大多数来源都没有说太多。
Below is my coding where it would load the image when this Activity is called. But i wished to display the progressDialog when it was retrieving the images from the server instead of just a black screen presented to the user. I heard that AsyncTask is able to do but i stuck with where to place the AsyncTask function and calling it.
public class LargeImageScroller extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SampleView(this));
}
private static class SampleView extends View {
private static Bitmap bmLargeImage; //bitmap large enough to be scrolled
private static Rect displayRect = null; //rect we display to
private Rect scrollRect = null; //rect we scroll over our bitmap with
private int scrollRectX = 0; //current left location of scroll rect
private int scrollRectY = 0; //current top location of scroll rect
private float scrollByX = 0; //x amount to scroll by
private float scrollByY = 0; //y amount to scroll by
private float startX = 0; //track x from one ACTION_MOVE to the next
private float startY = 0; //track y from one ACTION_MOVE to the next
public SampleView(Context context) {
super(context);
displayRect = new Rect(0, 0, displayWidth, displayHeight);
scrollRect = new Rect(0, 0, displayWidth, displayHeight);
bmLargeImage = Bitmap.createBitmap(1035 , 665, Config.ARGB_8888);
bmLargeImage = createMap(this.getContext());
}
public boolean onTouchEvent(MotionEvent event) {....}
protected void onDraw(Canvas canvas) {....}
private static Bitmap createMap(Context context) {...}
private static Drawable LoadImageFromWebOperations(String url) {....}
private static Bitmap decodeFile(File f, int requiredSize) {...}
}
}
I wish to know where should i place the AsyncTask function so as to display the progressDialog while the program is loading the image from server/cache. Most of the source i found on the internet doesn't say much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
AsyncTask 在某种程度上类似于线程。它用于处理一些耗时的操作,使主线程不会被阻塞。
对于您的情况,AsyncTask 是合适的。然而,你想把它放在哪里取决于你的逻辑。
请阅读 SDK:AsyncTask 以获取更多信息。还有一个代码片段可以帮助您。
补充:
嗯,我理解你的担忧。如果您只是想显示一个进度条来告诉用户应用程序正在从服务器下载图像。您可以这样处理:
1. 创建一个包含ProgressBar的xml布局文件。
2. 在LargeImageScroller类的OnCreate中,调用
setContentView(R.layout.that_layout)
显示进度条,并启动AsyncTask下载图像。3.在AsyncTask内部,重写
onProgressUpdate(Integer...progress)
来告诉ProgressBar下载进度。4. 在 AsyncTask 中,重写
onPostExecute(Long result)
。当 AsyncTask 完成时将调用此方法。在这里您可以执行以下操作:SampleView sv=new SampleView(this)
,然后将图像传递给 sv 对象。然后调用setContentView(sv)
来显示您的图像。这只是我的思路。我认为您可以在 LargeImageScroller 类或 SampleView 类中使用 AsyncTask。希望此评论有所帮助。
AsyncTask is somehow like a Thread. It is used to handle some time-consuming operations so that the main thread won't be blocked.
In your case, AsyncTask is suitable. However, the place where you would like to put it depends on your logic.
Please read the SDK: AsyncTask to get more info about it. There is also a code snippet that may help you.
Added:
Well, I understand your concern. If you just want to show a progress bar to tell the users that the app is downloading the images from the server. You may handle this way:
1. Create a xml layout file containing a ProgressBar.
2. In OnCreate in Class LargeImageScroller, call
setContentView(R.layout.that_layout)
to show the progressbar, and Start a AsyncTask to download the images.3.Inside AsyncTask, override
onProgressUpdate(Integer... progress)
to tell the ProgressBar the donwloading progress.4. Inside AsyncTask, override
onPostExecute(Long result)
. This method will be called when the AsyncTask finishes. Here you can do : SampleView sv=new SampleView(this)
, and then pass the images to the sv object. Then callsetContentView(sv)
to show your images.This is just my train of thought. I think you can use AsyncTask in either LargeImageScroller class or SampleView class. Hope ithis comment helps.