我正在编写一个从服务器下载图像的Android应用程序,但我想在适配器中加载图像时显示加载图像。 这是我的适配器:
public class GridViewImageAdapter extends BaseAdapter {
Context context;
public ImageView imageView;
private Activity _activity;
private ArrayList<String> _filePaths = new ArrayList<String>();
private int imageWidth;
public GridViewImageAdapter(Activity activity, ArrayList<String> filePaths,
int imageWidth,Context context1) {
this._activity = activity;
this._filePaths = filePaths;
this.imageWidth = imageWidth;
this.context=context1;
}
public int getCount() {
return this._filePaths.size();
}
public Object getItem(int position) {
return this._filePaths.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
imageView = new ImageView(_activity);
} else {
imageView = (ImageView) convertView;
}
imageView.setBackgroundResource(R.drawable.gallery_image);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(imageWidth,
imageWidth));
Log.e("Position",position+"");
new RetreiveBitMap().execute(_filePaths.get(position));
return imageView;
}
class RetreiveBitMap extends AsyncTask<String,Integer,Bitmap> {
private Exception exception;
protected Bitmap doInBackground(String... urls) {
Bitmap bm = null;
URL myFileUrl =null;
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 1;
try {
myFileUrl= new URL(urls[0]);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
String encodedfilename = Base64.encodeToString(urls[0].getBytes(),0);
//Controllo presenza della cache
try {
InputStream check = context.openFileInput(encodedfilename);
} catch (FileNotFoundException e) {
// Se non presente, scarico il file
HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
//Salvataggio cache
FileOutputStream fos = context.openFileOutput(encodedfilename, Context.MODE_PRIVATE);
byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = is.read(buffer)) > 0 ) {
fos.write(buffer,0, len1);
}
fos.close();
}
bm = BitmapFactory.decodeStream(context.openFileInput(encodedfilename),null,options);
} catch (IOException e) {
Log.e("errore download",e.getMessage());
}
return bm;
}
protected void onPreExecute() {
imageView.setImageResource(R.drawable.spinner);
}
protected void onPostExecute(Bitmap imageBit) {
imageView.setImageBitmap(imageBit);
}
}
}
此适配器显示加载图像,但只有最后一个网格更改为下载的图像,并且它位于错误的位置,因为它应该是网格的第一个。
答案 0 :(得分:2)
您可以这样做:执行下面的onPreExecute()
和onPostExecute()
:
protected void onPreExecute() {
imageView.setImageResource(R.drawable.spinner);//Loading image
protected void onPostExecute(Bitmap imageBit) {
if(imagefit!=null){
imageView.setBitmap(imagefit);//Final image
}
}
答案 1 :(得分:0)
您可以 Square 使用 Picasso 库。
该库为您提供了占位符选项,同时它还可以执行必要的网络下载图像。如果您在自定义适配器上加载图像,则无需在此处使用Asynctask。
这一行将起到作用
Picasso.with(context)
.load(url)
.placeholder(R.id.placeholder)
.error(R.id.placeholder_error)
.into(imageView)
您还可以使用.resize(width, height)
和.centerCrop()
或.fit()
希望它有所帮助!
答案 2 :(得分:0)
确定您的AsyncTask覆盖onPreExecute()
和onPostExecute()
就像这样
Dialog dialog;
@Override
protected void onPreExecute() {
//initialize inflate and customize your dialog
dialog.setMessage("Loading...");
dialog.setIcon(R.drawable.youricon);
dialog.setCancelable(false);
dialog.show();
}
@Override
protected void onPostExecute(Void result) {
if (dialog.isShowing())
dialog.dismiss();
}
看到这个 How to create a Custom Progress Dialog over riding the default one in Android?
希望它有助于欢呼:)
答案 3 :(得分:0)
您可以使用Universal Image Loader之类的通用图片加载库。它可以非常快速地加载图像。