解决方案 »
- android spinner setOnItemSelectedListener的问题
- monkeyrunner 相对目录问题
- 急!win7和Ubuntu10.04双系统,安装了android-x86 4.0后怎么恢复以前的ubuntu?
- addView()出错,怎么回事啊?
- 急:android 签名打包问题,在线求解
- 有关指纹识别?
- android webview 焦点背景问题
- 找android的工作,希望给点建议
- android初学者 遇到一个小问题 请大家帮忙
- 求MediaCodec.createInputSurface视频编码dequeueOutputBuffer返回-1问题
- Android WebView falsh 全屏播放白屏
- apk受到新安装的apk影响,没有找出原因,求大神帮忙一起找!
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;
}
}