动画加载图像视图是重复

时间:2015-02-03 19:14:51

标签: android animation

我使用适配器视图查看列表视图"出版物"来自Parse.com,我使用动画从Parse加载图像。并且我正在使用其他功能来获取图像的圆形视图。

一切都很好,加载很完美但是当我在列表视图中向下/向上滑动时,每次我向上/向下滑动时图像都会再次动画。我无法阻止它。

这是我的适配器的代码:



public class adapterview extends ArrayAdapter<Message> {
	Bitmap image;
    public adapterview(Context context, ArrayList<Message> Messages) {
       super(context, 0, Messages);
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       // Get the data item for this position
       final Message m = getItem(position);    
       // Check if an existing view is being reused, otherwise inflate the view
       if (convertView == null) {
          convertView = LayoutInflater.from(getContext()).inflate(R.layout.custom2, parent, false);
       }
     
       TextView message = (TextView) convertView.findViewById(R.id.message);
       TextView date = (TextView) convertView.findViewById(R.id.date);
       TextView user = (TextView) convertView.findViewById(R.id.user);
       message.setText(m.getMessage());
       user.setText(m.getUser()); 
       new DownloadImageTask((ImageView) convertView.findViewById(R.id.imageView1))
       .execute(m.getImage());

       return convertView;
   }
    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
  	  ImageView bmImage;

  	  public DownloadImageTask(ImageView bmImage) {
  	      this.bmImage = bmImage;
  	  }

  	  protected Bitmap doInBackground(String... urls) {
  	      String urldisplay = urls[0];
  	      Bitmap mIcon11 = null;
  	      try {
  	        InputStream in = new java.net.URL(urldisplay).openStream();
  	        mIcon11 = BitmapFactory.decodeStream(in);
  	      } catch (Exception e) {
  	          Log.e("Error", e.getMessage());
  	          e.printStackTrace();
  	      }
  	      return mIcon11;
  	  }

  	  protected void onPostExecute(Bitmap result) {
  		TransitionDrawable td = new TransitionDrawable(new Drawable[]{
  		        new ColorDrawable(android.R.color.transparent),
  		        new BitmapDrawable(getContext().getResources(), getCircleBitmap(result))
  		});

  	 
  	      bmImage.setImageDrawable(td);
  	      td.startTransition(2000);
  	  }
  	}
   
    private Bitmap getCircleBitmap(Bitmap bitmap) {
   	 final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(),
   	  bitmap.getHeight(), Bitmap.Config.ARGB_8888);
   	 final Canvas canvas = new Canvas(output);

   	 final int color = Color.RED;
   	 final Paint paint = new Paint();
   	 final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
   	 final RectF rectF = new RectF(rect);

   	 paint.setAntiAlias(true);
   	 canvas.drawARGB(0, 0, 0, 0);
   	 paint.setColor(color);
   	 canvas.drawOval(rectF, paint);

   	 paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
   	 canvas.drawBitmap(bitmap, rect, rect, paint);

   	 bitmap.recycle();

   	 return output;
   	}
}
&#13;
&#13;
&#13;

如果您需要任何其他信息,您认为可以帮助您确定问题,请告诉我

1 个答案:

答案 0 :(得分:0)

当列表需要显示项时,它会调用其适配器getView()函数。当您滚动并且列表项不再可见时,它们将被释放而不会保留在内存中。

滚动每个列表项时,每次可见时都会执行DownloadImageTask()。从功能下载的图像仅存储在列表项的ImageView中,因此一旦滚动列表项,它就会从内存中释放。

解决方案是将下载的图像存储在getView()函数中,然后将图像分配到ImageView中。