Andriod 讀取網(wǎng)絡(luò)圖片實(shí)例代碼解析
Android手機(jī)上,我們常用ImageView顯示圖片,我們本章獲取網(wǎng)絡(luò)圖片并顯示在ImageView中。
一、設(shè)計(jì)界面
1、布局文件
打開res/layout/activity_main.xml文件。
輸入以下代碼:
<?xml version="." encoding="utf-"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/imagephoto" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
二、程序文件
打開“src/com.genwoxue.networkphoto/MainActivity.java”文件。
然后輸入以下代碼:
package com.genwoxue.networkphoto; import java.io.IOException; 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.AsyncTask; import android.os.Bundle; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView imView; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imView = (ImageView) findViewById(R.id.imagephoto); String imageUrl = "http://img.baidu.com/img/image/ilogob.gif"; new NetworkPhoto().execute(imageUrl); } /* 四個(gè)步驟: * ()onPreExecute(),執(zhí)行預(yù)處理,它運(yùn)行于UI線程, * 可以為后臺(tái)任務(wù)做一些準(zhǔn)備工作,比如繪制一個(gè)進(jìn)度條控件。 * ()doInBackground(Params...),后臺(tái)進(jìn)程執(zhí)行的具體計(jì)算在這里實(shí)現(xiàn), * doInBackground(Params...)是AsyncTask的關(guān)鍵,此方法必須重載。 * 在這個(gè)方法內(nèi)可以使用 publishProgress(Progress...)改變當(dāng)前的進(jìn)度值。 * ()onProgressUpdate(Progress...),運(yùn)行于UI線程。如果 * 在doInBackground(Params...) 中使用了publishProgress(Progress...),就會(huì) * 觸發(fā)這個(gè)方法。在這里可以對(duì)進(jìn)度條控件根據(jù)進(jìn)度值做出具體的響應(yīng)。 * ()onPostExecute(Result),運(yùn)行于UI線程,可以對(duì)后臺(tái)任務(wù)的結(jié)果做出處理,結(jié)果 * 就是doInBackground(Params...)的返回值。此方法也要經(jīng)常重載,如果Result為 * null表明后臺(tái)任務(wù)沒(méi)有完成(被取消或者出現(xiàn)異常)。 * */ //本案例我們僅使用了()和() class NetworkPhoto extends AsyncTask<String, Integer, Bitmap> { public NetworkPhoto() { } //doInBackground(Params...),后臺(tái)進(jìn)程執(zhí)行的具體計(jì)算在這里實(shí)現(xiàn),是AsyncTask的關(guān)鍵,此方法必須重載。 @Override protected Bitmap doInBackground(String... urls) { URL url = null; Bitmap bitmap = null; HttpURLConnection conn=null; InputStream is=null; try { url = new URL(urls[]); } catch (MalformedURLException e) { e.printStackTrace(); } try { conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); conn.connect(); is = conn.getInputStream(); bitmap = BitmapFactory.decodeStream(is); is.close(); } catch (IOException e) { e.printStackTrace(); } finally { if(conn!=null){ conn.disconnect(); conn=null; } if(is!=null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } is=null; } } return bitmap; } //onPostExecute(Result),運(yùn)行于UI線程,可以對(duì)后臺(tái)任務(wù)的結(jié)果做出處理,結(jié)果 //就是doInBackground(Params...)的返回值。 @Override protected void onPostExecute(Bitmap bitmap) { // 返回結(jié)果bitmap顯示在ImageView控件 imView.setImageBitmap(bitmap); } } }
三、配置文件
打開“AndroidManifest.xml”文件。
然后輸入以下代碼:
<?xml version="." encoding="utf-"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.genwoxue.networkphoto" android:versionCode="" android:versionName="." > <uses-sdk android:minSdkVersion="" android:targetSdkVersion="" /> <uses-permission android:name="android.permission.INTERNET" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.genwoxue.networkphoto.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
注意:需要在AndroidManifest.xml文件中添加權(quán)限:
<uses-permission android:name="android.permission.INTERNET" />
相關(guān)文章
Android基礎(chǔ)知識(shí)之frame動(dòng)畫效果
Android基礎(chǔ)知識(shí)之tween動(dòng)畫效果,Android一共提供了兩種動(dòng)畫,這篇文章主要介紹了Android動(dòng)畫效果之frame動(dòng)畫,感興趣的小伙伴們可以參考一下2016-06-06詳解Android TableLayout中stretchColumns、shrinkColumns的用法
這篇文章主要介紹了Android TableLayout中stretchColumns、shrinkColumns用法的相關(guān)資料,需要的朋友可以參考下2017-03-03Android編程創(chuàng)建與解析xml的常用方法詳解
這篇文章主要介紹了Android編程創(chuàng)建與解析xml的常用方法,結(jié)合具體實(shí)例形式較為詳細(xì)的分析了Android操作xml文件的步驟、實(shí)現(xiàn)技巧與相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-05-05Android開發(fā)快速實(shí)現(xiàn)底部導(dǎo)航欄示例
這篇文章主要為大家介紹了Android開發(fā)快速實(shí)現(xiàn)底部導(dǎo)航欄的示例代碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-04-04android xml實(shí)現(xiàn)按鈕的圓角、陰影效果及按下變化效果的實(shí)現(xiàn)代碼
這篇文章主要介紹了android xml實(shí)現(xiàn)按鈕的圓角、陰影效果以及按下變化效果,通過(guò)五個(gè)xml文件實(shí)現(xiàn)按鈕的圓角陰影效果,代碼也很簡(jiǎn)單,需要的朋友可以參考下2021-05-05Android PopupWindow實(shí)現(xiàn)遮罩層效果
這篇文章主要為大家詳細(xì)介紹了Android PopupWindow實(shí)現(xiàn)遮罩層效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10