2012-06-06 91 views
1

開發框架應用程序時,從Url和ImageView中放置。爲此,我想顯示來自url的圖像(幀)。該網址有超過50個圖像。對於這個我使用gridview,但它缺乏一些點,如查看圖片當用戶選擇

1.它的速度非常緩慢加載圖像。

2.我們在代碼時聲明圖像的名稱和大小,以便在發佈應用程序後不添加圖像。

我需要這些asap的解決方案。請任何人給我建議。

+0

請告訴我你做了什麼? –

+0

我正在使用gridview。但需要解決方案而不是那個。顯示來自url的圖像,並選擇一個,並在imageview中修復它 – Shalini

回答

1

使用下面的延遲加載列表視圖的鏈接,這將幫助你。

Lazy Loading ListView

使用上面的鏈接代碼,並添加其他活動和其他佈局選擇用於顯示圖像,如果u有不是告訴我任何問題,我會在這裏把完整的代碼。

+0

謝謝迪帕克。它的工作現在 – Shalini

+0

嗨迪帕克,我經歷了懶惰的名單。但不能得到更改列表中圖像大小的代碼。你會知道嗎? – Shalini

+0

請參閱item.xml,並將imageview 50dip的大小更改爲100dip或更多。 –

0

1.加載圖像速度非常慢。

這取決於你的帶寬和設備緩存。

2.我們在代碼時聲明圖像的名稱和大小,以便在發佈應用程序後不添加圖像。

您可以預先定義的URL,以便在代碼時,可以追加圖像名稱url.and一旦你準備好網址後使用的AsyncTask下載圖片逐個\

下面的代碼片段會幫助你。

DownloadHelper.java

public interface DownloadHelper 
{ 
    public void OnSucess(Bitmap bitmap); 
    public void OnFailure(String response); 
} 

MainActivity.java

public class GalleryExample extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.main); 

     DownloadHelper downloadHelper = new DownloadHelper() 
     { 
      @Override 
      public void OnSucess(Bitmap bitmap) 
      { 
       ImageView imageView=(ImageView)findViewById(R.id.imageView); 
       imageView.setImageBitmap(bitmap); 
      } 

      @Override 
      public void OnFailure(String response) 
      { 
       Toast.makeText(context, response, Toast.LENGTH_LONG).show(); 
      } 
     }; 
     new MyTask(this,downloadHelper).execute("image url"); 
    } 

MyTask.java

public class DownloadTask extends AsyncTask<String, Integer, Object> 
{ 
    private Context context; 
    private DownloadHelper downloadHelper; 
    private ProgressDialog dialog; 


    public DownloadTask(Context context,DownloadHelper downloadHelper) 
    { 
     this.context = context; 

    } 

    @Override 
    protected void onPreExecute() 
    { 
     dialog = new ProgressDialog(context); 
     dialog.setTitle("Please Wait"); 
     dialog.setMessage("Fetching Data!!"); 
     dialog.setCancelable(false); 
     dialog.show(); 
     super.onPreExecute(); 
    } 

    @Override 
    protected Object doInBackground(String... params) 
    { 
     URL aURL = new URL(myRemoteImages[position]); 
     URLConnection conn = aURL.openConnection(); 
     conn.connect(); 
     InputStream is = conn.getInputStream(); 

     BufferedInputStream bis = new BufferedInputStream(is); 
     /* Decode url-data to a bitmap. */ 
     Bitmap bm = BitmapFactory.decodeStream(bis); 
     bis.close(); 
     is.close(); 
     return bm; 
    } 

    @Override 
    protected void onPostExecute(Object result) 
    { 
     dialog.dismiss(); 
     if (result != null) 
     { 
      downloadHelper.OnSucess((Bitmap)result); 
     } 
     else 
     { 
      downloadHelper.OnFailure("Error in Downloading Data!!"); 
     } 
     super.onPostExecute(result); 
    } 
} 
+0

感謝您的回答。但我需要用戶在網址中選擇圖片。該網址有更多的圖像用戶選擇一個和我需要在imageview中顯示的圖像。 – Shalini