Android實(shí)現(xiàn)網(wǎng)絡(luò)圖片瀏覽功能
我們?cè)谏暇W(wǎng)的過程中經(jīng)常看到各種圖片,那你知道它是如何實(shí)現(xiàn)的嗎?接下來就讓我們一塊探討一下。
網(wǎng)絡(luò)圖片的瀏覽可以分為倆部分,基本的頁(yè)面布局與界面交互,讓我們一步步的來編寫。
基本布局很簡(jiǎn)單,只需要有一個(gè)輸入圖片鏈接的EditText,一個(gè)瀏覽按鈕,一個(gè)ImageView就差不多了。下面是簡(jiǎn)單代碼。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <ImageView android:id="@+id/iv" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_weight="1" /> <EditText android:id="@+id/et_path" android:layout_width="fill_parent" android:layout_height="wrap_content" android:hint="請(qǐng)輸入圖片路徑" android:maxLines="1" /> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:onClick="click" android:text="瀏覽" /> </LinearLayout>
值得注意的是這里面的weight不是權(quán)重,而是渲染優(yōu)先級(jí),weight越大,優(yōu)先級(jí)越低。
最重要的自然是界面交互,輸入圖片的指定地址,便可以將服務(wù)器返回的圖片展示在界面上,具體如下
package cn.edu.bzu.imageviewdemo; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.View; import android.widget.EditText; import android.widget.ImageView; import android.widget.Toast; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class MainActivity extends AppCompatActivity { protected static final int CHANGE_UI = 1; protected static final int ERROR = 2; private EditText et_path; private ImageView iv; private Handler handler = new Handler(){ public void handleMessage(android.os.Message msg) { if(msg.what == CHANGE_UI){ Bitmap bitmap = (Bitmap) msg.obj; iv.setImageBitmap(bitmap); }else if(msg.what == ERROR){ Toast.makeText(MainActivity.this, "顯示圖片錯(cuò)誤", 0).show(); } }; }; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_path = (EditText) findViewById(R.id.et_path); iv = (ImageView) findViewById(R.id.iv); } public void click(View view) { final String path = et_path.getText().toString().trim(); if (TextUtils.isEmpty(path)) { Toast.makeText(this, "圖片路徑不能為空", Toast.LENGTH_SHORT).show(); } else { new Thread() { public void run() { try { URL url = new URL(path); //創(chuàng)建URL對(duì)象 HttpURLConnection conn = (HttpURLConnection) url .openConnection(); // 設(shè)置請(qǐng)求的方式 conn.setRequestMethod("GET"); //設(shè)置超時(shí)時(shí)間 conn.setConnectTimeout(5000); int code = conn.getResponseCode(); if (code == 200) { InputStream is = conn.getInputStream(); Bitmap bitmap = BitmapFactory.decodeStream(is); //iv.setImageBitmap(bitmap); Message msg = new Message(); msg.what = CHANGE_UI; msg.obj = bitmap; handler.sendMessage(msg); } else { Message msg = new Message(); msg.what = ERROR; handler.sendMessage(msg); } } catch (Exception e) { e.printStackTrace(); Message msg = new Message(); msg.what = ERROR; handler.sendMessage(msg); } }; }.start(); } } }
核心之處便是通過URL對(duì)象獲取HttpURLConnection,獲取服務(wù)器返回的輸入流
這便是簡(jiǎn)單的測(cè)試結(jié)果。有問題歡迎評(píng)論交流!
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android 網(wǎng)絡(luò)圖片查看顯示的實(shí)現(xiàn)方法
- Android讀取本地或網(wǎng)絡(luò)圖片并轉(zhuǎn)換為Bitmap
- Android 異步獲取網(wǎng)絡(luò)圖片并處理導(dǎo)致內(nèi)存溢出問題解決方法
- Android顯示網(wǎng)絡(luò)圖片實(shí)例
- Android 下載網(wǎng)絡(luò)圖片并顯示到本地
- 簡(jiǎn)單實(shí)現(xiàn)Android讀取網(wǎng)絡(luò)圖片到本地
- Android使用線程獲取網(wǎng)絡(luò)圖片的方法
- 在Android的應(yīng)用中實(shí)現(xiàn)網(wǎng)絡(luò)圖片異步加載的方法
- Android 讀取sdcard上的圖片實(shí)例(必看)
- Android sdcard實(shí)現(xiàn)圖片存儲(chǔ) 、聯(lián)網(wǎng)下載
- Android開發(fā)實(shí)現(xiàn)加載網(wǎng)絡(luò)圖片并下載至本地SdCard的方法
相關(guān)文章
Flutter交互并使用小工具管理其狀態(tài)widget的state詳解
這篇文章主要為大家介紹了Flutter交互并使用小工具管理其狀態(tài)widget的state詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12View中如何進(jìn)行手勢(shì)識(shí)別onFling動(dòng)作實(shí)現(xiàn)介紹
下面我們就以實(shí)現(xiàn)手勢(shì)識(shí)別的onFling動(dòng)作,在CwjView中我們從View類繼承,當(dāng)然大家可以從TextView等更高層的界面中實(shí)現(xiàn)觸控,感興趣的朋友可以了解下哈2013-06-06非常實(shí)用的側(cè)滑刪除控件SwipeLayout
這篇文章主要為大家詳細(xì)介紹了非常實(shí)用的側(cè)滑刪除控件SwipeLayout,類似于QQ側(cè)滑點(diǎn)擊刪除效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08Android 三級(jí)NestedScroll嵌套滾動(dòng)實(shí)踐
這篇文章主要介紹了Android 三級(jí)NestedScroll嵌套滾動(dòng)實(shí)踐,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02Android利用Badge組件實(shí)現(xiàn)未讀消息小紅點(diǎn)
在?App?的運(yùn)營(yíng)中,活躍度是一個(gè)重要的指標(biāo),日活/月活……為了提高活躍度,就發(fā)明了小紅點(diǎn)。這一篇,來介紹一個(gè)徽標(biāo)(Badge)組件,能夠快速搞定應(yīng)用內(nèi)的小紅點(diǎn),希望對(duì)大家有所幫助2023-01-01