大家好我是Android的新手,我很難通過URL將多個圖像加載到Recycler視圖中,我的任務是不使用任何第三方庫和也不要在xml文件中添加字符串數組。是否有可能循環一個URL?例如「http://onethousandpaintings.com/imgs/numbers/number_1.png」,如果該數字改變圖像相應改變,一次嘗試!我正在想辦法在「For循環」中增加該數字,但無法弄清楚。請爲我提供一個解決方案。在Recyclerview中加載多個Url圖像而不使用第三方庫
-1
A
回答
0
您可以使用AsynTask,以便從Url加載多個圖像,該圖像提供了在後臺工作的功能。因此,您的主線程不會受到影響,並且圖像將不斷下載到後端。我希望這回答你的問題。
2
@sample AsycTask Code,你可以通過執行方法將url傳遞給這個類。
public class ShowImage extends AsyncTask<String,Void,Bitmap>{
private WeakReference<ImageView> imageview;
public ShowImage(ImageView imv){
imageview=new WeakReference<ImageView>(imv);
}
/** Background process
* input:url
* output: Bitmap image
* It passed into onPostExecute method
**/
@Override
protected Bitmap doInBackground(String... urls) {
return getBitMapFromUrl(urls[0]);
}
/** This method called after the doINputBackground method
* input:Bitmap image
* output: image set into the image view
* Image view passed from RecyclerViewOperation to ShowImage class through constructor
**/
@Override
protected void onPostExecute(Bitmap result) {
if((imageview!=null)&&(result!=null)){
ImageView imgview=imageview.get();
if(imgview!=null){
imgview.setImageBitmap(result);
}
}
}
/** This method called by doInBackground method
* input:url
* output: Bitmap image
*
**/
private Bitmap getBitMapFromUrl(String imageuri){
HttpURLConnection connection=null;
try {
URL url=new URL(imageuri);
// Log.d("bucky","bitmap" + imageuri);
connection= (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream is=connection.getInputStream();
Bitmap mybitmap=BitmapFactory.decodeStream(is);
return mybitmap;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
finally {
if(connection!=null) {
connection.disconnect();
}
}
}
}
相關問題
- 1. 如何使recyclerview可擴展而不使用第三方庫
- 2. 如何在android中使用第三方庫而不使用第三方庫
- 3. RecyclerView加載方形圖像
- 4. 在SAPUI5中加載第三方庫
- 5. 如何在cordova webview中加載第三方不安全圖像?
- 6. 在Cordova inAppBrowser中加載第三方不安全圖像?
- 7. Recyclerview不加載圖像
- 8. 圖像到PDF轉換,而不使用C#中的第三方庫#
- 9. 加載圖像Recyclerview
- 10. 使用superslim庫加載更多recyclerview
- 11. 使用Glide爲RecyclerView加載圖像
- 12. 加載第三方非CakePHP庫
- 13. 加載第三方庫角CLI
- 14. 圖像像素操作:使用本機API或第三方庫?
- 15. 在加載中加載多個圖像
- 16. 在Python中使用第三方庫
- 17. 在GWT中使用第三方庫(libphonenumber)
- 18. 在TornadoFX中使用第三方UI庫
- 19. RecyclerView重新加載圖像
- 20. Recyclerview未加載圖像
- 21. 在Visual Studio中加載第三方DLL
- 22. 使用第三方庫
- 23. 使用Picasso在DataObject中加載RecyclerView中的圖像
- 24. 無法使用Cocoa Pod加載第三方庫
- 25. 影響第三方庫使用的類加載器
- 26. NPAPI加載第三方DLL
- 27. OSX部署:第三方庫在/ Applications而非bundle中加載插件(自定義)
- 28. 在第三方服務器上保存第三方圖像
- 29. 添加第三方庫Laravel
- 30. 笨第三方類不加載
第三方庫會在更大程度上減少您的工作,如果您想選擇總是受歡迎的困難方式。我認爲這是一個任務問題 –
你知道任何解決方案嗎? –
然後使用它在工作室的庫中構建的抽象圖像加載器 –