Android使用線程獲取網(wǎng)絡(luò)圖片的方法
本文為大家分享了Android使用線程獲取網(wǎng)絡(luò)圖片的具體代碼,供大家參考,具體內(nèi)容如下
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zdcrobot.handlermessage">
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.zdcrobot.handlermessage.MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="加載圖片"/>
<ImageView
android:id="@+id/image1"
android:layout_width="match_parent"
android:layout_height="500dp" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
MainActivity.class
package com.zdcrobot.handlermessage;
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.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private Button button;
private ImageView imageView;
private String imagPath = "http://pica.nipic.com/2007-11-09/200711912453162_2.jpg";
private final int IS_FINISH = 1;
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
Bitmap bitmap = (Bitmap)msg.obj;
imageView.setImageBitmap(bitmap);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button)findViewById(R.id.button1);
imageView = (ImageView)findViewById(R.id.image1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new MyClass()).start();
}
});
}
public class MyClass implements Runnable{
@Override
public void run() {
Bitmap bitmap = null;
try {
URL url = new URL(imagPath);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.connect();
InputStream inputStream = httpURLConnection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Message message = Message.obtain();
message.obj = bitmap;
message.what = IS_FINISH;
handler.sendMessage(message);
}
}
}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家學(xué)習(xí)Android軟件編程有所幫助。
相關(guān)文章
Android入門(mén)之計(jì)時(shí)器Chronometer的使用教程
Chronometer是一個(gè)簡(jiǎn)單的定時(shí)器,你可以給它一個(gè)開(kāi)始時(shí)間,并以此定時(shí)。本文將利用個(gè)簡(jiǎn)單的示例為大家講解一下它的使用,感興趣的小伙伴可以嘗試一下2022-11-11
Android 中SQLite技術(shù)實(shí)例詳解
這篇文章主要介紹了Android 中SQLite技術(shù)實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android編程使用HTTP協(xié)議與TCP協(xié)議實(shí)現(xiàn)上傳文件的方法
這篇文章主要介紹了Android編程使用HTTP協(xié)議與TCP協(xié)議實(shí)現(xiàn)上傳文件的方法,結(jié)合實(shí)例形式較為詳細(xì)的分析了Android使用HTTP協(xié)議與TCP協(xié)議的具體步驟與實(shí)現(xiàn)文件傳輸?shù)南嚓P(guān)技巧,需要的朋友可以參考下2016-01-01
Android HelloChart開(kāi)源庫(kù)圖表之折線圖的實(shí)例代碼
這篇文章主要介紹了Android HelloChart開(kāi)源庫(kù)圖表之折線圖的實(shí)例代碼,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Android利用ViewDragHelper輕松實(shí)現(xiàn)拼圖游戲的示例
本篇文章主要介紹了Android利用ViewDragHelper輕松實(shí)現(xiàn)拼圖游戲的示例,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-11-11
Android第三方文件選擇器aFileChooser使用方法詳解
這篇文章主要介紹了Android第三方文件選擇器aFileChooser的使用方法詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07
Diycode開(kāi)源項(xiàng)目實(shí)例搭建上拉加載和下拉刷新的Fragment
這篇文章主要介紹了Diycode開(kāi)源項(xiàng)目實(shí)例搭建上拉加載和下拉刷新的Fragment以及相關(guān)的代碼分享。2017-11-11
Android 實(shí)現(xiàn)控件懸浮效果實(shí)例代碼
本篇文章主要介紹了Android 實(shí)現(xiàn)控件懸浮效果實(shí)例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
Android自定義SeekBar滑動(dòng)顯示數(shù)字
這篇文章主要為大家詳細(xì)介紹了Android自定義SeekBar滑動(dòng)顯示數(shù)字,使用FrameLayout結(jié)合SeekBar滑動(dòng)時(shí),數(shù)值顯示,滑動(dòng)停止時(shí)顯示數(shù)字,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09

