我是android的新手,所以请耐心等待我,我正在创建此代码来绘制图像的gridview,我正在使用asynctask,但是我的问题是asynctask正在运行所以绑定图像变得迟到和图像没有显示,当我调试它显示,因为我延迟它所以我知道问题但我希望有人修复我的语法,非常感谢。
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
public class ImagesAdapter extends BaseAdapter implements ListAdapter {
private Context mContext;
ImageView currentImageView;
Bitmap bmImg;
// Constructor
public ImagesAdapter(Context c) {
super();
mContext = c;
GridViewConfig.addImageUrls();
}
@Override
public int getCount() {
return GridViewConfig.getResim_list().size();
}
@Override
public Object getItem(int position) {
return GridViewConfig.getResim_list().get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null)
{
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(100, 100));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(5, 5, 5, 5);
} else {
imageView = (ImageView) convertView;
}
downloadFile(GridViewConfig.getResim_list().get(position));
imageView.setImageBitmap(bmImg);
// imageView.setImageDrawable(LoadImageFromURL(GridViewConfig
// .getResim_list().get(position)));
return imageView;
}
void downloadFile(String fileUrl) {
AsyncTask<String, Object, String> task = new AsyncTask<String, Object, String>() {
@Override
protected String doInBackground(String... params) {
URL myFileUrl = null;
try {
myFileUrl = new URL(params[0]);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bmImg = BitmapFactory.decodeStream(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String unused) {
/* if (currentImageView == null) {
currentImageView = new ImageView(mContext);
}
currentImageView.setImageBitmap(bmImg); */
}
};
task.execute(fileUrl);
}
gridviewconfig类返回所需的图像。
答案 0 :(得分:0)
看看http://developer.android.com/training/displaying-bitmaps/index.html有一个带有网格的BitmapFun示例项目/应用程序和许多有用的东西。我建议你不要重新发明轮子,但要使用谷歌的代码,他们是Android的创造者 我还建议你研究一些LazyList实现