/**
* 新房信息按图片查看楼盘列表
*
* @author lsx
*/
public class NewhouseInfoPicActivity extends Activity implements OnTouchListener,
OnGestureListener, OnClickListener {
private ViewFlipper vf = null;
private NewhouseInfoPicAdapter picAdapter;// viewFlipper数据适配器,它本身没有setAdapter
vf = (ViewFlipper) findViewById(R.id.viewflipper);
vf.setOnTouchListener(this); // 注册OnTouch监听器
// 获取ViewFlipper的图片数据
picAdapter = new NewhouseInfoPicAdapter(this,vf,globalApp.getAreaId(),NewhouseInfoListActivity.searchText);
// 实现OnFling方法
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// 对手指滑动的距离进行了计算,如果滑动距离大于120像素,就做切换动作,否则不做任何切换动作。
// 从左向右滑动
Log.i("OnFling", e1 + ":" + e2);
if (e1.getX() - e2.getX() > 120) {
if(vf.getChildCount()>=curImg+1){
vf.setDisplayedChild(curImg);
} else {
picAdapter.getView(curImg, null, null);
// 添加动画
this.vf.setInAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_in));
this.vf.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_left_out));
this.vf.showNext();
}
curImg = vf.getDisplayedChild() + 1;// 设置当前页面数字
return true;
}// 从右向左滑动
else if (e1.getX() - e2.getX() < -120) {
this.vf.setInAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_in));
this.vf.setOutAnimation(AnimationUtils.loadAnimation(this,
R.anim.push_right_out));
if (vf.getDisplayedChild() == 0) {// 处理向左滑动到最后一张图片
curImg = vf.getChildCount();
} else {
curImg = vf.getDisplayedChild();
}
this.vf.showPrevious();
return true;
}
return true;
}
}
下面是adapter中getView内容和异步加载图片的方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView image = new ImageView(mcontext);
image.setImageBitmap(this.defaultBit);
image.setScaleType(ImageView.ScaleType.FIT_XY);
// 添加到viewFlipper中
vf.addView(image, position, new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
// 获取网络图片
String url = (String) this.getListData().get(position).get("url");
Log.i("URL", position+url);
if (!url.equals(StringManage.FS_EMPTY)) {
LoadImageTask task = new LoadImageTask(position,image);
task.execute(url);
} else {
image.setImageDrawable(mcontext.getResources().getDrawable(R.drawable.nopic));
}
return image;
}
/**
* 加载图片的异步任务AsyncTask
*
* @author lsx
*/
class LoadImageTask extends AsyncTask {// 这里的Bitmap也使用软引用试一下
int position;
ImageView image;
LoadImageTask(int position,ImageView image) {
this.position = position;
this.image = image;
}
// doInBackground完成后才会被调用
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null)
Log.i("onPostExecute", "setTag");
else
Log.i("onPostExecute", "not setTag");
// 添加到viewFlipper中
this.image.setImageBitmap(bitmap);
this.cancel(true);
}
// 从网上下载图片
@Override
protected Bitmap doInBackground(String... params) {
Bitmap image = null;
try {
checkLockSize(this.position);
// 手动进行GC回收
if (this.position % 2 == 0) {
System.gc();
}
String file_path = "lesohome/house/gallery";// 文件存储路径
BitmapManager bm = new BitmapManager(file_path);// 图片管理类
image = bm.loadDataFromUrl(params[0], 1);
if (image == null) {
Log.i("捕获bitmap", "捕获bitmap已经被释放或且bitmap==null的异常");
image = defaultBit;
}
NewhouseInfoPicAdapter.lockSize++;
} catch (Exception e) {
Toast.makeText(mcontext, "异步下载图片发生异常" + e.getMessage(),
Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
return image;
}
}
以上是用addView()方法动态添加切换图片,实现了异步下载显示的图片,当有大量图片需要用作切换时,会造成内存溢出
转载请标明出处:点击打开链接