Android 等待图像加载到 ImageView
我在滚动磁盘上的图像时遇到问题,这些图像相当大(每个图像大约 1 Mb) 现在我遇到的问题不是每个图像之间的延迟,而是加载图像时图像的滚动(我用它来表示图像的滚动)加载图像时不断检测到更改),导致最终加载时图像发生移动。我的问题很简单。有没有办法等到图像加载(这样我可以阻止屏幕命令,直到图像完全加载)。
感谢您的任何答复!
PD:顺便说一句,我正在 ImageView 中加载页面;
编辑:
发布AsyincCode:
这是在我的测试活动itests.java中
public class ImageLoader extends AsyncTask<String, Void, Void > {
private final ProgressDialog Loading = new ProgressDialog(itests.this);
protected void onPreExecute(){
Loading.setMessage("Loading...");
Loading.show();
}
@Override
protected Void doInBackground(String... params) {
pvm.setPages(params[0], params[1], params[2]);
return null;
}
protected void onPostExecute(Void... params){
if (Loading.isShowing()){
Loading.dismiss();
}
}
现在pvm是我的PageViewManager(PageView是我的自定义ImageView,它接收所有触摸事件)
这就是它的作用:
public void setPages(String left, String center, String right){
//if (!timer.isAlive()){
Left = left;
Center = center;
Right = right;
DoublePageMode = true;
toggleDoublePageMode();
System.err.println("Waiting...");
//centerPage.blockCommands();
//timer.start();
//}
}
@Override
public void toggleDoublePageMode(){
if (DoublePageMode){
//Going into Normal View
threePageLayout.setOrientation(LinearLayout.HORIZONTAL);
leftPage.setVisibility(View.VISIBLE);
LinearLayout.LayoutParams cll = new LinearLayout.LayoutParams(DWidth,DHeight);
LinearLayout.LayoutParams zero = new LinearLayout.LayoutParams(0,DHeight);
centerPage.setImage(Center, DWidth, DHeight, PageState.NORMAL);
centerPage.setLayoutParams(cll);
leftPage.setImage(Left, DWidth, DHeight, PageState.NORMAL);
leftPage.setLayoutParams(zero);
rightPage.setImage(Right, DWidth, DHeight, PageState.NORMAL);
rightPage.setLayoutParams(zero);
DoublePageMode = false;
}
else{
DoublePageMode = true;
threePageLayout.setOrientation(LinearLayout.VERTICAL);
leftPage.setVisibility(View.GONE);
LinearLayout.LayoutParams cll = new LinearLayout.LayoutParams(DWidth,DHeight,1f);
LinearLayout.LayoutParams zero = new LinearLayout.LayoutParams(DWidth,DHeight,1f);
centerPage.setImage(Center, DWidth, DHeight, PageState.TWOPAGEMODE);
centerPage.setLayoutParams(cll);
rightPage.setImage(Right, DWidth, DHeight, PageState.TWOPAGEMODE);
rightPage.setLayoutParams(zero);
}
}
@Override符号是因为该函数也必须是从 LongTouch 上的 PageView 调用。不过我没有做任何长按,所以这不是问题。由于 AsyncTask 尝试移动显示图像的活动的布局,因此我收到了我告诉过您的错误,这是完全有道理的。
当我需要调用此方法时它来自此代码(在itests.java上):
public void movePages(boolean next) {
//Code that initializes left,center,right
new ImageLoader().execute(left, center, right);
}
此方法由PageViewManager(pvm)通过接口调用。
我希望这能给你一个想法!
非常感谢!
EDIT2:
我已经解决了!实际上更像是我找到了解决方法,但我工作得很好!
我所做的就是创建一个 Timer 类,让我知道特定时间已经过去。当我需要加载图像时,我启动它(它是另一个线程)并设置一个布尔值,使我的页面忽略触摸事件(通过在 onTouchEvent 中返回 false)。完成后,它会调用我的 pvm 中的一个函数,该函数只需再次将该布尔值设置为 true,效果正是我想要的。计时器仅等待大约 500 毫秒。
感谢您的帮助!
I have problem that scroll through images on disk which are rather large (about 1 Mb each) Now the problem I have is not the latency between each image but rather that while loading the image the scroll of the image (which I use to signal a change) keeps being detected while the image is loaded resulting in a moved image when it finally loads. My question is simple. Is there a way to wait until the image is loaded (that way I can block screen commands until the image is completely loaded).
Thanks for any answers!
PD: By the way I'm loading the pages in an ImageView;
EDIT:
Posting AsyincCode:
This is in my test activity itests.java
public class ImageLoader extends AsyncTask<String, Void, Void > {
private final ProgressDialog Loading = new ProgressDialog(itests.this);
protected void onPreExecute(){
Loading.setMessage("Loading...");
Loading.show();
}
@Override
protected Void doInBackground(String... params) {
pvm.setPages(params[0], params[1], params[2]);
return null;
}
protected void onPostExecute(Void... params){
if (Loading.isShowing()){
Loading.dismiss();
}
}
Now pvm is my PageViewManager (PageView is my Custom ImageView that receives all my touch events)
Here is what it does:
public void setPages(String left, String center, String right){
//if (!timer.isAlive()){
Left = left;
Center = center;
Right = right;
DoublePageMode = true;
toggleDoublePageMode();
System.err.println("Waiting...");
//centerPage.blockCommands();
//timer.start();
//}
}
@Override
public void toggleDoublePageMode(){
if (DoublePageMode){
//Going into Normal View
threePageLayout.setOrientation(LinearLayout.HORIZONTAL);
leftPage.setVisibility(View.VISIBLE);
LinearLayout.LayoutParams cll = new LinearLayout.LayoutParams(DWidth,DHeight);
LinearLayout.LayoutParams zero = new LinearLayout.LayoutParams(0,DHeight);
centerPage.setImage(Center, DWidth, DHeight, PageState.NORMAL);
centerPage.setLayoutParams(cll);
leftPage.setImage(Left, DWidth, DHeight, PageState.NORMAL);
leftPage.setLayoutParams(zero);
rightPage.setImage(Right, DWidth, DHeight, PageState.NORMAL);
rightPage.setLayoutParams(zero);
DoublePageMode = false;
}
else{
DoublePageMode = true;
threePageLayout.setOrientation(LinearLayout.VERTICAL);
leftPage.setVisibility(View.GONE);
LinearLayout.LayoutParams cll = new LinearLayout.LayoutParams(DWidth,DHeight,1f);
LinearLayout.LayoutParams zero = new LinearLayout.LayoutParams(DWidth,DHeight,1f);
centerPage.setImage(Center, DWidth, DHeight, PageState.TWOPAGEMODE);
centerPage.setLayoutParams(cll);
rightPage.setImage(Right, DWidth, DHeight, PageState.TWOPAGEMODE);
rightPage.setLayoutParams(zero);
}
}
The @Override notation is because the function also has to be called from the PageView on a LongTouch. However I'm not doing any sort of long press so it's not a problem. Since the AsyncTask tries to move the layout of the activity showing the images I get the error I told you, which makes perfect sense.
When I need to call this method It comes from this code (on itests.java):
public void movePages(boolean next) {
//Code that initializes left,center,right
new ImageLoader().execute(left, center, right);
}
This method is called by the PageViewManager (pvm) through an inteface.
I hope this gives you an idea!
Thank you very much!
EDIT2:
I've solved it! Actually more like I found a way around it but I works beautifully!
All I did is create a Timer class that lets me know when a certain time has passed. When I need to load the images I start it (it is another thread) and set a boolean that makes my pages ignore touch events (by returning false in onTouchEvent). When it is done it call a function in my pvm that simply set that boolean again to true and the effect is just what I wanted. The timer waits for only about 500 miliseconds.
Thanks for all the help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我希望您使用 aSyncTask 来加载数据!您可以在加载数据时简单地添加一个进度对话框。这可以通过添加以下内容来完成:
在开始加载数据之前,一旦所有数据准备就绪,只需调用
pDialog.dismiss();
I hope your using a aSyncTask to load the data! You could simply add a progress dialog whilst loading the data. this can be done by adding:
before you start loading the data, then once all your data is ready simply call
pDialog.dismiss();
我能想到的一种(较长的)解决方案是实现您自己的 ImageView 类,该类加载 BitmapDrawable 并在 onDraw() 中绘制它。这样,您就可以确定 onDraw() 何时完成,并调用不同的回调(例如 onDrawComplete() )来切换触摸事件处理。
One (longish) solution I can think of is to implement your own ImageView class, which loads a BitmapDrawable and draws it in onDraw(). This way, you can know for sure when onDraw() completes, and call a different callback (e.g. onDrawComplete() ) to toggle touch event handling.