2011-04-01 53 views
1

我感到有点困惑这段代码:膨胀一个视图分配一个新的对象?

for(int i = 0; i < poi.getCommenti().size();i++){ 
      item = poi.getCommenti().get(i); 
      commento = li.inflate(R.layout.commento, null); 
      commento_data = (TextView) commento.findViewById(R.id.commento_data); 
      commento_descrizione =(TextView) commento.findViewById(R.id.commento_descrizione); 
      commento_name = (TextView) commento.findViewById(R.id.commento_nome); 
      commento_foto = (ImageView) commento.findViewById(R.id.commento_foto); 
      Log.d(TAG, "commento_foto:"+commento_foto); 
      commento_data.setText(item.getData()); 
      commento_descrizione.setText(item.getTesto()); 
      commento_name.setText(item.getUtente().getName()); 
      contenitore_commenti.addView(commento); 
       image[i] = new ImageViewURL(commento_foto, item.getAnteprimaURL()); 
     } 

     // I start only one thread for all images 
     thread_image_commenti = new ImageThreadURL(this); 
     thread_image_commenti.execute(image); 

线图像是一个简单的线程里面做一个下载的图像,并采取2 PARAM:

1)ImageView的地方线程调用setImage(位图)

2)表示URL(下载图像的字符串)

的问题是,我可以看到我所有的照片闪烁,当单个图像被下载,所有的照片具有相同的图像(URL AR不一样)。

这是正确的方法吗?为每个元素填充一个NEW视图为每个迭代创建一个NEW VIEW OBJECT或者它们是相同的对象?

怎么了?

非常感谢。

pedr0

这是我的线程的代码:

public class ImageThreadURL extends AsyncTask<SingleImageURL, SingleImageURL,Void>{ 

    public final static String TAG = "ImageThreadURL"; 
    static final int MAX_CONN = 25; 
    static final int TIMEOUT_DATA = 0; 
    static final int TIMEOUT_CONNECTION = 10000; 

    Activity caller; 
    Animation animation; 


    public ImageThreadURL(Activity caller) { 
     super(); 
     this.caller = caller; 
     this.animation = AnimationUtils.loadAnimation(caller, R.anim.alphaanimation); 
     animation.setRepeatCount(0); 
    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     caller.setProgressBarIndeterminateVisibility(true); 
    } 

    @Override 
    protected Void doInBackground(SingleImageURL... arg0) { 
     Log.d(TAG, "Lunghezza array input:"+arg0.length); 
     HttpParams parameters = new BasicHttpParams(); 
     HttpProtocolParams.setVersion(parameters, HttpVersion.HTTP_1_1); 
     HttpProtocolParams.setContentCharset(parameters, HTTP.UTF_8); 
     HttpProtocolParams.setUseExpectContinue(parameters, false); // some webservers have problems if this is set to true 
     ConnManagerParams.setMaxTotalConnections(parameters, MAX_CONN); 
     HttpConnectionParams.setConnectionTimeout(parameters, TIMEOUT_CONNECTION); 
     HttpConnectionParams.setSoTimeout(parameters,TIMEOUT_DATA); 
     SchemeRegistry schReg = new SchemeRegistry(); 
     schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); 
     ClientConnectionManager conMgr = new ThreadSafeClientConnManager(parameters,schReg); 
     DefaultHttpClient client_http = new DefaultHttpClient(conMgr, parameters); 
     HttpGet method; 
     HttpResponse response; 
     HttpEntity entity; 
     InputStream in ; 
     Bitmap image ; 

     SingleImageURL actual=null; 

     try{ 
      for(int i = 0; i < arg0.length ;i++){ 
       actual = arg0[i]; 
       Log.d(TAG,"Preparo download:"+ actual.getUrl()); 
       if(actual.getUrl() == null) 
        continue; 
       method = new HttpGet(Uri.decode(actual.getUrl())); 
       response = client_http.execute(method); 
       entity = response.getEntity(); 
       BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); 
       in = bufHttpEntity.getContent(); 
       image = BitmapFactory.decodeStream(in); 
       // problema scaricamento immagine 
       if(image == null){throw new Exception("BitmapFactory.decodeStream() return null!");} 
       else{  
        Log.d(TAG,"Eseguito Download:"+ actual.getUrl()); 
        actual.setImage(image); 
        publishProgress(arg0[i]); 
       } 
      } 

     }catch (Exception e) {e.printStackTrace();} 
     return null; 
    } 

    @Override 
    protected void onProgressUpdate(SingleImageURL... values) { 
     Log.d(TAG, "onProgressUpdate"); 
     values[0].getView().startAnimation(animation); 
     values[0].setImage(); 
    } 


    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     caller.setProgressBarIndeterminateVisibility(false); 
    } 



} 

回答

0

你也应该张贴线程代码。在Android中,不能从UI线程以外的其他线程更改UI对象(ImageView)。这可能是正在发生的事情。

如果您有多个视图,则应考虑使用ListView小部件以及自定义适配器(以扩大每行的布局)。


编辑:

  1. 您是否尝试过在第一次摆脱了动画,看看问题是否仍然存在?

  2. 我还注意到你正在创建两个ImageViewURL对象。

    image[i] = new ImageViewURL(commento_foto, item.getAnteprimaURL()); 
        thread_image_commenti = new ImageThreadURL(this); 
        thread_image_commenti.execute(new ImageViewURL(commento_foto, item.getAnteprimaURL())); 
    

难道不应该是:

 image[i] = new ImageViewURL(commento_foto, item.getAnteprimaURL()); 
     new ImageThreadURL(this).execute(image[i]); 

+0

太长我觉得,我在onProgressUpdate()中执行setImage代码,它在UI线程中执行。 – pedr0 2011-04-01 08:21:22

+0

也看到我编辑的答案 – 2011-04-01 08:25:06

+0

有一个没有问题的动画做了这个闪烁效果。 :-) 谢谢! – pedr0 2011-04-01 08:33:53