2016-02-04 94 views
-2

我想將圖像設置爲在我的android應用程序中動態創建的ImageView。 我只是創建它:將src設置爲ImageView

ImageView image = new ImageView(this); 

,我想從一個鏈接設置圖像像

example image

我有什麼做的?

編輯:我的代碼:

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       View v = vi.inflate(R.layout.article, null); 



       img = (ImageView) v.findViewById(R.id.grid_image); 
       url=thumbnail; 

       Picasso.with(MainActivity.this).load(url).into(img); 

它是一個用於cicle

+2

有大量教程將圖像渲染到圖像視圖。你也可以使用像畢加索這樣的庫。 – thedarkpassenger

+2

可能重複[如何在Android中通過URL加載ImageView?](http://stackoverflow.com/questions/2471935/how-to-load-an-imageview-by-url-in-android) – null

+0

檢查此出https://github.com/pankajnimgade/Tutorial/blob/master/app/src/main/java/miscellaneous/list/activities/ImageViewDynamicallyActivity.java –

回答

1

您可以使用第三方 http://square.github.io/picasso/

Picasso.with(context).load("imageURL").into(image); 

加入依賴搖籃

compile 'com.squareup.picasso:picasso:2.5.2' 
+0

畢加索導致我凍結應用界面... –

0

來自在線來源?沿着這些線:image.setImageUri(Uri.parse("http://..."))

+1

這不是一個不錯的選擇,因爲下載圖像將在主應用程序線程上完成,凍結UI。 – CommonsWare

1

你需要從網上獲取該圖像。我建議你使用Picasso。它添加到您的項目把此行到依賴性在搖籃文件:

compile 'com.squareup.picasso:picasso:2.5.2' 

而且隨着顯示:

Picasso.with(this).load("URL").into(imageView); 
+0

畢加索導致我凍結應用界面... –

+0

圖像真的很大? – MiguelCatalan

+0

全高清或高清,但只有600kb –

0

大家都提到了如何使用Piccaso,我想提的問題更專注於如何將ImageView動態添加到Layout,您可以確定地檢查github上的代碼,它已經過測試。

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ImageViewDynamicallyActivity_linear_layout); 
// this would be the linear layout to which you can add the `ImageView` 

ImageView imageView = new ImageView(this); 

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); 
imageView.setLayoutParams(layoutParams); 
// you can make a lot of changes to the `imageview` attributes here 
linearLayout.addView(imageView); 

Picasso.with(getApplicationContext()).load("http://image.3bmeteo.com/images/newarticles/w_663/immagine-nasa-visible-infrared-imaging-radiometer-suite-viirs-3bmeteo-66721.jpg").into(imageView); 

我假設你會添加Picasso依賴於你的build.gradle文件的應用模塊。

compile 'com.squareup.picasso:picasso:2.5.2' 
+0

畢加索導致我凍結應用界面... –

+0

@MarcoGhigo,我不認爲使用畢加索會凍結UI,我認爲需要時間從URI加載圖像 –