Android使用控件ImageView加載圖片的方法
在 Android 加載圖片一般使用 ImageView,這里簡單記錄一下這個控件的使用方法。
最簡單就是在 xml 里直接使用 ImageView 標(biāo)簽:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/welcome" /> </LinearLayout>
如果不想在 xml 里,也可以在程序里面加載。比如:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); ImageView welcome = new ImageView(this); welcome.setImageResource(R.drawable.welcome); setContentView(welcome); }
構(gòu)建ImageView對象時傳遞了一個this參數(shù),表示與當(dāng)前上下文(context)關(guān)聯(lián)。這個Context由系統(tǒng)處理,它提供諸如資源解析、獲取訪問數(shù)據(jù)庫和偏好等服務(wù)。因?yàn)锳ctivity類繼承自Context,且因?yàn)槟愕腍elloWorld類是Activity的子類,它也是一個Context。因此,你可以傳遞this作為你的Context給ImageView引用。
Android ImageView如何加載網(wǎng)絡(luò)圖片資源,代碼也分享給大家:
package com.android.antking.imageview; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.ImageView; public class MainActivity extends Activity { //定義一個圖片顯示控件 private ImageView imageView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //圖片資源 String url = "/orignal/89429f6dhb99b4903ebcf&690"; //得到可用的圖片 Bitmap bitmap = getHttpBitmap(url); imageView = (ImageView)this.findViewById(R.id.imageViewId); //顯示 imageView.setImageBitmap(bitmap); } /** * 獲取網(wǎng)落圖片資源 * @param url * @return */ public static Bitmap getHttpBitmap(String url){ URL myFileURL; Bitmap bitmap=null; try{ myFileURL = new URL(url); //獲得連接 HttpURLConnection conn=(HttpURLConnection)myFileURL.openConnection(); //設(shè)置超時時間為6000毫秒,conn.setConnectionTiem(0);表示沒有時間限制 conn.setConnectTimeout(6000); //連接設(shè)置獲得數(shù)據(jù)流 conn.setDoInput(true); //不使用緩存 conn.setUseCaches(false); //這句可有可無,沒有影響 //conn.connect(); //得到數(shù)據(jù)流 InputStream is = conn.getInputStream(); //解析得到圖片 bitmap = BitmapFactory.decodeStream(is); //關(guān)閉數(shù)據(jù)流 is.close(); }catch(Exception e){ e.printStackTrace(); } return bitmap; } }
以上就是本文的全部內(nèi)容,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Android仿新浪微博發(fā)送菜單界面的實(shí)現(xiàn)
這篇文章主要介紹了Android仿新浪微博發(fā)送菜單界面的實(shí)現(xiàn),幫助大家更好的理解和學(xué)習(xí)使用Android開發(fā),感興趣的朋友可以了解下2021-04-04使用Flutter實(shí)現(xiàn)一個走馬燈布局的示例代碼
這篇文章主要介紹了使用 Flutter 實(shí)現(xiàn)一個走馬燈布局的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11AndroidStudio圖片壓縮工具ImgCompressPlugin使用實(shí)例
這篇文章主要為大家介紹了AndroidStudio圖片壓縮工具ImgCompressPlugin使用實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Android使用OKHttp包處理HTTP相關(guān)操作的基本用法講解
這篇文章主要介紹了Android使用OKHttp包處理HTTP相關(guān)操作的基本用法講解,包括操作如何利用OKHttp操作HTTP請求與處理緩存等內(nèi)容,需要的朋友可以參考下2016-07-07Android 實(shí)現(xiàn)通知消息水平播放、無限循環(huán)效果
Android 實(shí)現(xiàn)通知消息水平播放、無限循環(huán)效果非常常見,今天小編給大家分享實(shí)例代碼感興趣的朋友參考下吧2017-08-08源碼淺析Android中內(nèi)存泄漏檢測工具Leakcanary的使用
大名鼎鼎的 Leakcanary 想必作為 Android 開發(fā)都多多少少接觸過,新版本的 Leakcanary 也用 Kotlin 重寫了一遍,最近詳細(xì)查看了下源碼,就來和大家簡單分享一下2023-04-04