如何在 Android 应用程序的网格视图中加载图像时显示进度条
在我的应用程序中,当启动某个活动时,我会点击一个 url,并从中获取一个 xml 文件。 xml文件包含各种图像的url。我正在解析这些值并将它们存储在数组列表中。从该列表中,图像已加载到网格视图中。当此过程启动时,有时会在从我的网络服务加载图像时出现黑屏。所以在这种情况下我想显示一个进度条。
以下是我显示进度条的代码,
class Image extends AsyncTask<Void, Void, Void>
{
ProgressDialog dialog = new ProgressDialog(ProfileImages.this);
protected void onPreExecute()
{
dialog.setMessage("Please wait...");
dialog.setIndeterminate(true);
dialog.show();
}
protected void onPostExecute(Void unused)
{
i.setImageBitmap(bm);
dialog.dismiss();
}
@Override
protected Void doInBackground(Void... params)
{
parsing();
grid.setAdapter(new ImageAdapter(this));
return null;
}
}
但我的应用程序崩溃了。解析函数做得很好,但当它达到它时
grid.setAdapter(new ImageAdapter(this));
public View getView(final int position, View convertView,ViewGroup parent)
{
ImageView i = new ImageView(this.myContext);
if (convertView == null)
{
i = new ImageView(myContext);
i.setLayoutParams(new GridView.LayoutParams(105, 105));
i.setScaleType(ImageView.ScaleType.CENTER_CROP);
i.setPadding(8, 8, 8, 8);
i.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
dialog = new ProgressDialog(ProfileGridView.this);
dialog.setMessage("Please wait...");
dialog.setIndeterminate(true);
dialog.show();
new Thread()
{
public void run()
{
try
{
Thread.sleep(300);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
Bundle bundle = new Bundle();
bundle.putInt("key1",position);
Appconstant.showLog("position clicked "+position);
Intent myIntent = new Intent(getBaseContext(),ProfileImages.class);
myIntent.putExtras(bundle);
startActivityForResult(myIntent, 0);
dialog.dismiss();
}
}.start();
}
});
}
else
{
i = (ImageView) convertView;
}
try
{
URL aURL = new URL(myRemoteImages[position]);
Appconstant.showLog("Grid View URL " + aURL);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
i.setImageBitmap(bm);
}
catch (IOException e)
{
Appconstant.showErrorLog("DEBUGTAG Remtoe Image Exception" + e);
}
return i;
}
public float getScale(boolean focused, int offset)
{
return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
}
}
就会崩溃。如何做到这一点,请帮助我
in my app when an activity is launched i am hitting an url and from that an xml file is got. The xml file contains the url of various images. I am parsing those values and storing them in an array list. From that list the images are been loaded in grid view. When this process gets started some times a black screen use to appear while loading images from my web service. So in that case i want to show a progress bar.
Following is my code to show progress bar
class Image extends AsyncTask<Void, Void, Void>
{
ProgressDialog dialog = new ProgressDialog(ProfileImages.this);
protected void onPreExecute()
{
dialog.setMessage("Please wait...");
dialog.setIndeterminate(true);
dialog.show();
}
protected void onPostExecute(Void unused)
{
i.setImageBitmap(bm);
dialog.dismiss();
}
@Override
protected Void doInBackground(Void... params)
{
parsing();
grid.setAdapter(new ImageAdapter(this));
return null;
}
}
But my app i getting crashed. The parsing functions are doing good but when it reaches the
grid.setAdapter(new ImageAdapter(this));
public View getView(final int position, View convertView,ViewGroup parent)
{
ImageView i = new ImageView(this.myContext);
if (convertView == null)
{
i = new ImageView(myContext);
i.setLayoutParams(new GridView.LayoutParams(105, 105));
i.setScaleType(ImageView.ScaleType.CENTER_CROP);
i.setPadding(8, 8, 8, 8);
i.setOnClickListener(new View.OnClickListener()
{
public void onClick(View view)
{
dialog = new ProgressDialog(ProfileGridView.this);
dialog.setMessage("Please wait...");
dialog.setIndeterminate(true);
dialog.show();
new Thread()
{
public void run()
{
try
{
Thread.sleep(300);
}
catch (InterruptedException e)
{
e.printStackTrace();
}
Bundle bundle = new Bundle();
bundle.putInt("key1",position);
Appconstant.showLog("position clicked "+position);
Intent myIntent = new Intent(getBaseContext(),ProfileImages.class);
myIntent.putExtras(bundle);
startActivityForResult(myIntent, 0);
dialog.dismiss();
}
}.start();
}
});
}
else
{
i = (ImageView) convertView;
}
try
{
URL aURL = new URL(myRemoteImages[position]);
Appconstant.showLog("Grid View URL " + aURL);
URLConnection conn = aURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
Bitmap bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
i.setImageBitmap(bm);
}
catch (IOException e)
{
Appconstant.showErrorLog("DEBUGTAG Remtoe Image Exception" + e);
}
return i;
}
public float getScale(boolean focused, int offset)
{
return Math.max(0, 1.0f / (float) Math.pow(2, Math.abs(offset)));
}
}
it gets crashed. How to do this, please help me
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要一种从 AsyncTask 中访问您的活动的方法 - 您不能简单地编写“ProfileGridView.this”,因为它不是一个封闭的任务。您应该重写 asynctask 的构造函数,并将调用活动的上下文与其一起传递。
You need a way to access your activity from within the AsyncTask - you can't simply write "ProfileGridView.this" because it's not an enclosing task. You should override the constructor for your asynctask and pass the context of the calling activity in along with it.