当前位置:操作系统 > 安卓/Android >>

Android异步下载网络图片(其一)

项目中有时候需要获取网络上的图片,并下载下来到手机客户端显示。怎么做呢?

实现思路是:

 1:在UI线程中启动一个线程,让这个线程去下载图片。

 2:图片完成下载后发送一个消息去通知UI线程

 2:UI线程获取到消息后,更新UI。

 这里的UI线程就是主线程。

 这两个步骤涉及到一些知识点,即是:ProgressDialog,Handler,Thread/Runnable,URL,HttpURLConnection等等一系列东东的使用。

 现在让我们开始来实现这个功能吧!

 第一步:新建项目。

 第二步:设计好UI,如下所示

<?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"
>
<Button
android:id="@+id/btnFirst"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="异步下载方式一"
>
</Button>

<Button
android:id="@+id/btnSecond"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="异步下载方式二"
>
</Button>

<FrameLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="@+id/frameLayout"
>

<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:padding="2dp"
>
</ImageView>

<ProgressBar
android:id="@+id/progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center
补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,