2013-11-01 65 views
0

我已經實施了Gallery視圖,其中包含Google I \ O VolleyVolley,Volley presentation,併成功下載圖像並執行緩存。使用DiscLru緩存和Volley時,Android圖庫滾動速度很慢

首先,當我使用LruCache進行緩存時,它非常好,畫廊滾動很不錯。但LruCache沒有保持16MB的內存,只緩存4MB。我想緩存高達16MB。

所以我轉而使用DiscLruCache代替LruCache。它做我想要的緩存(16MB)。但問題是畫廊視圖滾動是非常緩慢,並且不平穩下載時和所有緩存(16MB)後。

這是我的代碼。

package com.technotalkative.volleyimageloading; 

import java.util.ArrayList; 
import java.util.List; 

import org.json.JSONArray; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.util.DisplayMetrics; 
import android.util.Log; 
import android.view.Display; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.ViewGroup.LayoutParams; 
import android.view.ViewGroup.MarginLayoutParams; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.BaseAdapter; 
import android.widget.Gallery; 
import android.widget.TextView; 

import com.android.volley.toolbox.NetworkImageView; 

public class SimpleViewFrag extends Fragment { 

    public static final String TAG = SimpleViewFrag.class.getSimpleName(); 
    private Gallery mGallery; 
    private List<MyCataLogData> simpleViewList = new ArrayList<MyCataLogData>(); 
    MainActivity mainActivity; 
    private LayoutInflater inflater; 
    private int height; 
    private int width; 
    private int mItemWidth; 
    private int mItemHeight; 
    private SimpleViewAdapter mAdapter; 

    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 
     mainActivity = (MainActivity) activity; 
    } 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Display display = getActivity().getWindowManager().getDefaultDisplay(); 
     height = display.getHeight(); 
     width = display.getWidth(); 

     mItemWidth = (int) (width/1.7f); 
     mItemHeight = (int) (height/3f); 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     this.inflater = inflater; 
     View v = null; 
     v = inflater.inflate(R.layout.simple_view, container, false); 


//  String s = getArguments().getString(UCConstants.CATALOG_CONTENT_KEY); 
     mGallery = (Gallery) v.findViewById(R.id.simpleViewGalleruy); 

     mAdapter = new SimpleViewAdapter(); 
     mGallery.setAdapter(mAdapter); 

//  mGallery.setSelection(1); 

     DisplayMetrics metrics = new DisplayMetrics(); 
     getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics); 

     // set gallery to left side 
     MarginLayoutParams mlp = (MarginLayoutParams) mGallery.getLayoutParams(); 
     mlp.setMargins(-((int) (metrics.widthPixels/3.8f)), mlp.topMargin, 
        mlp.rightMargin, mlp.bottomMargin); 

     parseJsonForCataLog(); 


     mGallery.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> arg0, View arg1, int position, 
        long arg3) { 
       Log.e(TAG, "position: ====="+position); 

      } 
     }); 

     return v; 
    } 

    public class SimpleViewAdapter extends BaseAdapter { 

     @Override 
     public int getCount() { 
      return simpleViewList.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      return simpleViewList.get(position); 
     } 

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      NetworkImageView networkImageView = null; 
      TextView tvTitle = null; 
      TextView tvText = null; 
      View childView = null; 


      if (convertView == null) { 
       childView = inflater.inflate(R.layout.simple_view_content_item, null); 
      } else { 
       childView = convertView; 
      } 

      if (childView != null) { 

       networkImageView = (NetworkImageView) childView 
         .findViewById(R.id.networkImageView); 

       networkImageView.getLayoutParams().width = mItemWidth; 
       networkImageView.getLayoutParams().height = LayoutParams.MATCH_PARENT; 

       //getting an object for gallery item 
       MyCataLogData cld = simpleViewList.get(position); 
       if (networkImageView != null) { 
         networkImageView 
          .setImageUrl(cld.getSimpleImageUrl(), ImageCacheManager.getInstance().getImageLoader()); 
       } 
       tvTitle = (TextView) childView.findViewById(R.id.title); 
       if (tvTitle != null) { 
        tvTitle.setText(cld.getSimpleTitle()); 
       } 

       tvText = (TextView) childView.findViewById(R.id.text); 
       if (tvText != null) { 
        tvText.setText(cld.getSimpleText()); 
       } 

      } 
      return childView; 
     } 
    } 

    protected void parseJsonForCataLog() { 
     try { 

      String jsonObjectString = getArguments().getString("JSONObject"); 

      JSONObject rootResponce = new JSONObject(jsonObjectString); 

      Log.e(TAG, "rootResponce: " + rootResponce); 
      JSONArray simpleArray = rootResponce.optJSONArray("SIMPLE"); 

      if(simpleArray == null) 
       return; 

      for (int i = 0; i < simpleArray.length(); i++) { 
       JSONObject childMenuObject = simpleArray.getJSONObject(i); 

       String textSimple = childMenuObject.optString("text"); 
       String titleSimple = childMenuObject.optString("title"); 
       String imageUrlSimple = childMenuObject.optString("imageUrl"); 

       MyCataLogData mcld = new MyCataLogData(null ,textSimple, titleSimple, imageUrlSimple); 
       simpleViewList.add(mcld); 
//    Log.e(TAG, "" + childMenuObject); 
      } 

      mAdapter.notifyDataSetChanged(); 

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

回答

1

如果我理解正確的話,你(廣東話」找到你的樣品中ImageLoader初始化),看來你是提供磁盤緩存的凌空ImageLoader構造,當你要提供一個內存緩存

如果您的內存緩存的大小太過有限,您應該編輯代碼以使其變大,或者找到/創建一個按您希望的方式工作的新接口實現。

無論您選擇什麼 - 不要使用磁盤高速緩存,其中預計有內存高速緩存。

3

我對ListView有同樣的問題,但我發現一個解決方案在谷歌iosched應​​用程序。您可以嘗試應用以下內容,但不推薦使用Gallery。我建議使用水平列表視圖,例如Lucas Rocha的TwoWayView。該解決方案可以適用於它,只是落實TwoWayView.OnScrollListener代替

添加AbsListView onscroll監聽方法和停止凌空而滾動快速/扔

公共類SimpleViewFrag擴展片段實現AbsListView.OnScrollListener {

ListView mListView; 
    mListView.setOnScrollListener(this); 

@Override 
public void onScrollStateChanged(AbsListView view, int scrollState) { 
    // Pause disk cache access to ensure smoother scrolling 
    if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) { 
     VolleyHelper.getImageLoader().getRequestQueue().stop(); 
    } else { 
     VolleyHelper.getImageLoader().getRequestQueue().start(); 
    } 
} 

@Override 
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { 

}