2016-12-04 76 views
0

您好我正在使用自定義gridView包含imageview和2 textFields在我的程序中,在getview方法我正在使用圖像從互聯網使用url獲取圖像在ImageView中顯示圖像。我需要使用lrucache顯示圖像而不將下載的圖像存儲在外部存儲器中。有人幫我編碼。自定義Gridview中的LRUCACHE

預先感謝您


這是我的自定義的GridView類


public class CustomGridViewActivity extends ArrayAdapter<Actors> { 
private static final String TAG = "CustomGridActivity"; 
ArrayList<Actors> actorList; 
LayoutInflater vi; 
int Resource; 
ViewHolder holder; 

public CustomGridViewActivity(Context context, int resource, ArrayList<Actors> objects) { 
    super(context, resource, objects); 
    vi = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    Resource = resource; 
    actorList = objects; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    View v = convertView; 
    if(v == null){ 
     holder = new ViewHolder(); 
     v = vi.inflate(Resource, null); 
     holder.iconview = (ImageView) v.findViewById(R.id.IVicon); 
     holder.Name = (TextView) v.findViewById(R.id.tvName); 
     holder.Price = (TextView) v.findViewById(R.id.tvPrice); 
     v.setTag(holder); 
    } 
    else { 
     holder =(ViewHolder) v.getTag(); 
    } 
    holder.iconview.setImageResource(R.mipmap.appcatlog); 
    if(actorList.get(position).getUrl().equals("null")) { 
     try { 
      String base =actorList.get(position).getImagefile(); 
      String replacechar = "data:image/png;base64,"; 
      base = base.replace(replacechar,""); 
      byte[] imageAsBytes = Base64.decode(base.getBytes(), Base64.DEFAULT); 
      holder.iconview.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)); 
     }catch (Exception e){ 
      e.getMessage(); 
     } 
    }else{ 
     new DownloadImageTask(holder.iconview).execute(actorList.get(position).getUrl()); 
    } 
    holder.Name.setText(actorList.get(position).getName()); 
    holder.Price.setText(actorList.get(position).getPrice()); 
    return v; 
} 

static class ViewHolder { 
    public ImageView iconview; 
    public TextView Name; 
    public TextView Price; 
} 

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 { 
      if (urldisplay.contains("https:")||urldisplay.contains("http:")){ 
       urldisplay = urldisplay.replace("https:",""); 
       urldisplay = urldisplay.replace("http:",""); 
      } 
      InputStream in = new URL("https:"+urldisplay).openStream(); 
      mIcon11 = BitmapFactory.decodeStream(in); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
     return mIcon11; 
    } 
    protected void onPostExecute(Bitmap result) { 
     bmImage.setImageBitmap(result); 
    } 
} 

}實現這一功能是通過使用第三方庫

+0

歡迎SO,請出示你的代碼都試過了,你遇到什麼問題到和你有什麼到目前爲止已經試過。確保你只添加相關的代碼。 –

回答

0

的最佳方式。你可以試試畢加索,滑翔或其他的東西。

好運

+0

謝謝..我使用Glide ...它爲我工作 –

+0

很棒:) –