首页 新闻 会员 周边 捐助

Android 下载网络图片,实现图片的ViewFlipper切换

-1
悬赏园豆:20 [待解决问题]

功能就不多说了!问题是我在第一次点击跳转到这个activity时项目就崩溃???但再次点击跳转时就可以实现加载缓存图片,并能实现切换;求高手..谢谢!代码如下

public class PreviewPictureActivity extends BaseActivity implements
  android.view.GestureDetector.OnGestureListener {
 private GestureDetector gestureDetector = null;
 private ViewFlipper viewFlipper = null;
 private Activity mActivity = null;
 private Drawable drawable;
 public String[] photos = {"网络图片地址1","网络图片地址2","网络图片地址3","网络图片地址4","网络图片地址5" };

 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  super.setContentView(R.layout.report_pictrue_item);
  mActivity = this;
  viewFlipper = (ViewFlipper) findViewById(R.id.viewflipper);
  gestureDetector = new GestureDetector(this); // 声明检测手势事件
  new BackgroundAsyncTask().execute();
 }

 protected void onDestroy() {
  super.onDestroy();
  unbindDrawables(findViewById(R.id.RootView));
  System.gc();
 }

 private void unbindDrawables(View view) {
  if (view.getBackground() != null) {
   view.getBackground().setCallback(null);
  }
  if (view instanceof ViewGroup) {
   for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
    unbindDrawables(((ViewGroup) view).getChildAt(i));
   }
   ((ViewGroup) view).removeAllViews();
  }
 }

 public boolean onTouchEvent(MotionEvent event) {
  viewFlipper.stopFlipping(); // 点击事件后,停止自动播放
  viewFlipper.setAutoStart(false);
  return gestureDetector.onTouchEvent(event); // 注册手势事件
 }

 public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
   float velocityY) {
  if (e2.getX() - e1.getX() > 120) { // 从左向右滑动(左进右出)
   Animation rInAnim = AnimationUtils.loadAnimation(mActivity,
     R.anim.push_right_in); // 向右滑动左侧进入的渐变效果(alpha 0.1 -> 1.0)
   Animation rOutAnim = AnimationUtils.loadAnimation(mActivity,
     R.anim.push_right_out); // 向右滑动右侧滑出的渐变效果(alpha 1.0 -> 0.1)

   viewFlipper.setInAnimation(rInAnim);
   viewFlipper.setOutAnimation(rOutAnim);
   viewFlipper.showPrevious();
   return true;
  } else if (e2.getX() - e1.getX() < -120) { // 从右向左滑动(右进左出)
   Animation lInAnim = AnimationUtils.loadAnimation(mActivity,
     R.anim.push_left_in); // 向左滑动左侧进入的渐变效果(alpha 0.1 -> 1.0)
   Animation lOutAnim = AnimationUtils.loadAnimation(mActivity,
     R.anim.push_left_out); // 向左滑动右侧滑出的渐变效果(alpha 1.0 -> 0.1)

   viewFlipper.setInAnimation(lInAnim);
   viewFlipper.setOutAnimation(lOutAnim);
   viewFlipper.showNext();
   return true;
  }
  return true;
 }

 public boolean onDown(MotionEvent e) {
  return false;
 }

 public void onLongPress(MotionEvent e) {
 }

 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
   float distanceY) {
  return false;
 }

 public void onShowPress(MotionEvent e) {
 }

 public boolean onSingleTapUp(MotionEvent e) {
  return false;
 }

 /**
  *
  * @描述
  * @param
  * @return
  * @throws Exception
  */
 public static Object fetch(String address) throws MalformedURLException,
   IOException {
  URL url = new URL(address);
  Object content = url.getContent();
  return content;
 }

 /**
  *
  * @描述
  * @param
  * @return
  * @throws Exception
  */
 public static Drawable ImageOperations(String url) {
  try {
   InputStream is = (InputStream) fetch(url);
   Drawable d = Drawable.createFromStream(is, "src");

   return d;
  } catch (MalformedURLException e) {
   return null;
  } catch (IOException e) {
   return null;
  }
 }

 /**
  *
  * @Title:
  * @Description:
  * @Copyright:Copyright (c) 2012
  * @File name: PreviewPictureActivity.java
  * @Create 2012 2012-10-24
  * @Version: 1.0.0
  * @Others: @
  */
 private class BackgroundAsyncTask extends
   AsyncTask<Integer, Integer, Integer> {

  Map<String, Drawable> imagesMap = new HashMap<String, Drawable>();
  int photosCounter = 0;

  protected void onPreExecute() {
  }

  protected void onProgressUpdate(Integer... values) {
  }

  protected Integer doInBackground(Integer... params) {
   for (int i = 0; i < photos.length; i++) {
    Drawable image = ImageOperations(photos[i]);
    imagesMap.put(String.valueOf(i), image);
    photosCounter++;

    if (i == 2)
     break;
   }

   return null;
  }

  protected void onPostExecute(Integer result) {
   for (int i = 0; i < photosCounter; i++) {
    final ImageView imageView = new ImageView(
      getApplicationContext());
    imageView.setImageDrawable(imagesMap.get(String.valueOf(i)));
    viewFlipper.addView(imageView);
   }

  }

 }

}

小祁的博客的主页 小祁的博客 | 初学一级 | 园豆:110
提问于:2012-10-25 10:47
< >
分享
所有回答(1)
0

博主,你的问题解决了吗?

码虫韩小呆 | 园豆:202 (菜鸟二级) | 2017-12-11 10:46
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册