国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android實(shí)現(xiàn)網(wǎng)絡(luò)圖片瀏覽器

2019-10-23 18:28:39
字體:
供稿:網(wǎng)友

本文實(shí)例為大家分享了Android網(wǎng)絡(luò)圖片瀏覽器的制作過程,供大家參考,具體內(nèi)容如下

一、創(chuàng)建一個(gè)“網(wǎng)絡(luò)圖片瀏覽器的應(yīng)用程序”,并設(shè)計(jì)用戶交互界面,“網(wǎng)絡(luò)圖片瀏覽器”對應(yīng)的布局文件(activity_main.xml)代碼如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:orientation="vertical" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.bz0209.myapplication.MainActivity"> <ImageView android:layout_weight="1000" android:id="@+id/iv" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <EditText android:singleLine="true" android:id="@+id/et_path" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="http://b.hiphotos.baidu.com/image/w%3D310/sign=a439f5b24510b912bfc1f0fff3fdfcb5/83025aafa40f4bfb92c52c5d014f78f0f73618a5.jpg" android:hint="請輸入圖片路徑" /> <Button android:onClick="click" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="瀏覽" /></LinearLayout>

效果圖如下:

Android網(wǎng)絡(luò)圖片瀏覽器,Android圖片瀏覽器,Android網(wǎng)絡(luò)圖片瀏覽

二、編寫界面交互代碼(MainActivity),當(dāng)界面創(chuàng)建好后,需要在MainActivity里面編寫與界面交互的代碼。用于實(shí)現(xiàn)請求指定地址的網(wǎng)絡(luò)圖片,并將服務(wù)器返回的圖片展現(xiàn)在界面上。具體代碼如下:

package com.example.bz0209.myapplication;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;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; //主線創(chuàng)建消息處理器 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();  } } }; @Override 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,"圖片路徑不能為空",0).show(); } else {  //子線程請求網(wǎng)絡(luò),Android 4.0以后訪問網(wǎng)絡(luò)不能放在子線程中  new Thread(){  private HttpURLConnection conn;  private Bitmap bitmap;  public void run(){   //鏈接服務(wù)器get請求,獲取圖片   try{   //創(chuàng)建URL對象   URL url =new URL(path);   conn=(HttpURLConnection) url.openConnection();   conn.setRequestMethod("GET");   conn.setConnectTimeout(5000);   conn.setRequestProperty("User-Agent","Mozilla/4.0 (compatib;MSIE 6.0;Window NT 5.1;"+"SV1;" +    ".NET4.0C;.NET4.0E;NET CLK 2.0.50727;"+".NET CLR 3.0..4506.2152;.NET CLR 3.5.30729; Shuame )");   int code=conn.getResponseCode();   if(code==200){    InputStream is=conn.getInputStream();    bitmap= BitmapFactory.decodeStream(is);    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;   }  }  }.start(); } }}

三、添加權(quán)限:由于網(wǎng)絡(luò)圖片瀏覽器需要請求網(wǎng)絡(luò),因此需要在清單文件中配置相應(yīng)的權(quán)限,具體操作如下圖所示:

Android網(wǎng)絡(luò)圖片瀏覽器,Android圖片瀏覽器,Android網(wǎng)絡(luò)圖片瀏覽

四、運(yùn)行瀏覽圖片 ,結(jié)果圖如下:

Android網(wǎng)絡(luò)圖片瀏覽器,Android圖片瀏覽器,Android網(wǎng)絡(luò)圖片瀏覽

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持VEVB武林網(wǎng)。


注:相關(guān)教程知識閱讀請移步到Android開發(fā)頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 梁山县| 喀喇沁旗| 招远市| 灵宝市| 霍州市| 阿拉善盟| 云和县| 庐江县| 新巴尔虎左旗| 十堰市| 裕民县| 龙川县| 万山特区| 井研县| 潼关县| 和顺县| 巴林左旗| 文化| 都匀市| 高雄县| 温泉县| 榆中县| 南丰县| 西乌珠穆沁旗| 湛江市| 阿坝县| 平山县| 临海市| 合川市| 肥城市| 称多县| 西城区| 安图县| 武夷山市| 洱源县| 江津市| 石景山区| 章丘市| 辰溪县| 西乌| 海城市|