listview加载图片有时候不能正常显示
--------------------编程问答--------------------
要看你程序是怎么写的。这都是程序逻辑问题。
--------------------编程问答--------------------
能不能写一个进度条提示框,当加载完成之后再让用户操作?这样就不会因为加载时间过长导致图片显示不出来。
--------------------编程问答--------------------
package com.lantinx.ac;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AbsListView.OnScrollListener;
import entity.ActivityCard;
public class ActivityAdapter extends BaseAdapter {
public int state;
public String name;
ArrayList<ActivityCard> cards = null;
Context mContext;
HashMap<String, SoftReference<Bitmap>> bitmapHashMap = new HashMap<String, SoftReference<Bitmap>>();
HashMap<View, String> items = new HashMap<View, String>();
boolean flag;
// 更细listview handler
Handler handler;
// 加载位图线程handler
Handler loadImgeHandler;
SharedPreferences setting = null;
Thread loadImage = null;
boolean isFirst = true;
public ActivityAdapter(Context context, ArrayList<ActivityCard> cards) {
// TODO Auto-generated constructor stub
this.cards = cards;
this.mContext = context;
handler = new Handler();
setting = context.getSharedPreferences("setting", Context.MODE_PRIVATE);
if (loadImage == null) {
loadImage = new Thread(new Runnable() {
@SuppressLint("NewApi")
@Override
public void run() {
// TODO Auto-generated method stub
Looper.prepare();
loadImgeHandler = new Handler() {
};
Looper.loop();
}
});
loadImage.setName("loadImage");
loadImage.start();
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return cards.size() + 1;
// return 1;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
View v = null;
@SuppressLint("NewApi")
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
Log.e("-----", "获取位置" + name + position + "---convertView"
+ convertView);
Log.e("===", "Left--getView");
if (position == cards.size()) {
DisplayMetrics dm = mContext.getResources().getDisplayMetrics();
// Log.e("---", "宽度" + dm.widthPixels);
// Log.e("---", "高度" + dm.heightPixels);
// Log.e("----", "密度" + dm.density);
if (v == null) {
v = new View(mContext);
}
v.setMinimumHeight((int) (70 * dm.density));
v.setBackgroundColor(Color.parseColor("#ffffff"));
return v;
}
if (position > 4) {
isFirst = false;
}
View ca;
ViewHolder holder;
if (convertView == null || convertView == v) {
ca = (View) LayoutInflater.from(mContext).inflate(
R.layout.mainactivitylist, parent, false);
TextView text = (TextView) ca.findViewById(R.id.main_ListViewText);
RelativeLayout image = (RelativeLayout) ca
.findViewById(R.id.main_ListViewImage);
// ImageView imageView = (ImageView)
// ca.findViewById(R.id.main_list_image);
holder = new ViewHolder();
holder.title = text;
holder.back = image;
// holder.imageView = imageView;
ca.setTag(holder);
} else {
ca = convertView;
holder = (ViewHolder) convertView.getTag();
}
// 设置文字
holder.title.setText(cards.get(position).getTitle()
+ "\n"+cards.get(position).getStartTime());
// holder.back.setBackgroundResource(R.drawable.ic_launcher);
String userId = setting.getString("userId", "");
String filePath = null;
String[] images = cards.get(position).getImages().split(",");
for (String path : images) {
Log.e("----", path);
if (!path.equals("null")) {
filePath = path;
break;
}
}
final String tempFilePath = filePath;// new String(filePath);
Log.e("---", "准备加载图片" + tempFilePath);
final View tempCard = holder.back;
while (loadImgeHandler == null) {
}
Runnable runable = new Runnable() {
@Override
public void run() {
Bitmap bitmap = null;
// TODO Auto-generated method stub
Log.e("----", "=======" + tempFilePath);
bitmap = getBitmap(tempFilePath, tempCard);
final Bitmap bitmap2 = bitmap;
bitmapHashMap.put(tempFilePath, new SoftReference<Bitmap>(
bitmap));
Runnable runnable = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
// tempCard.setBackground(new
// BitmapDrawable(mContext.getResources(),bitmap2));
if(bitmap2 == null){
tempCard.setBackgroundResource(R.drawable.dog);
}else{
tempCard.setBackgroundDrawable(new BitmapDrawable(
mContext.getResources(), bitmap2));
}
}
};
handler.post(runnable);
// handler.post(runnable);
// handler.post(runnable);
}
};
if (state == OnScrollListener.SCROLL_STATE_FLING
|| state == OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
holder.back.setBackgroundResource(R.drawable.dog);
Log.e("===", "状态--" + state);
}
if (state != OnScrollListener.SCROLL_STATE_FLING) {
loadImgeHandler.post(runable);
}
return ca;
}
public Bitmap getBitmap(String fileNme, View ca) {
Bitmap bitmap = null;
String filePath = Environment.getExternalStorageDirectory().getPath()
+ "/ACImage/" + fileNme;
final BitmapFactory.Options option = new BitmapFactory.Options();
option.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, option);
option.inSampleSize = calculateImSampleSize(option, ca.getWidth(),
ca.getHeight());
option.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(filePath, option);
if (bitmap == null) {
Log.e("-----", " bitmap" + "------为空");
}
return bitmap;
}
static class ViewHolder {
TextView title;
View back;
// ImageView imageView;
}
public static int calculateImSampleSize(BitmapFactory.Options options,
int reqWith, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWith) {
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWith);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
}
--------------------编程问答--------------------
代码如上所示, loadImgeHandler.post(runable);通知子线程加载,加载完之后才通知主线程更新。
补充:移动开发 , Android