通过异步任务来实现一步图片加载
1.布局
修改布局文件,一个按钮和一个显示异步任务加载结果的ImageView控件
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="143dp"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="39dp"
android:text="点击获取图片" />
2
修改src/com.example.huoqutupian/MainActivity.java文件
public class MainActivity extends Activity {
//网络图片地址
public static final String URL ="<a target=_blank href="http://pic.nipic.com/2007-11-09/2007119121849495_2.jpg">http://pic.nipic.com/2007-11-09/2007119121849495_2.jpg";
private</a> Button mBtnPicTask;
private ImageView mIvPic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findView();//得到布局中的控件
setListtener();//绑定控件事件
}
private void findView(){
mBtnPicTask=(Button) findViewById(R.id.btn_picTask);//绑定控件
mIvPic=(inageView)findViewById(R.id.huoqutupian);
}
private void setListener(){
mBtnPicTask.setOnClickListener(new OnClickListener(){//添加事件
@Override
public void onClick(View arg0){
AsyncpicTask picTask=new ASYncPicTask();//定义异步任务 开始异步任务
PicTask.execute(mIvPic,URL);
}
}};}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
得到布局文件,设置监听器,当用户单击按钮的时候构造异步任务AsyncPicTask,并且传入相应的URL和ImageView对象,然后执行此异步任务。
3.定义一个异步任务类
新建src/com.example.huoqutupian/MainActivity.java文件
public class AsyncTask<Object,Integer,Bitmap>{//定义异步任务类
private ImageView iv_pic;//定义imageview属性
@Override//定义后台执行方法
protected Bitmap doInBackground(Object...params);{
//TODO Auto-generated method stub
Bitmap bitmap=null;
iv_pic=(ImageView)patams[0];
try{
URL url=new URL((string)patams[1]);
HttpsURLConnection conn=(HttpURLConnection)url.openConnection();
//调用URL对象openConnection()方法来创建URLConnection对象
conn.setDoInput(true);//设置该URLConnection的doInput请求头子段的值
conn.connect();//建立实际链接
InputStream inputStream=conn.getInputStream();//得到链接的字节流
bitmap=BitmapFactory.decodeStream(inputStream);//解析图片
inputStream.close();//关闭字节流对象
}catch(MalformedURLExcrption e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
@Override//得到图片数据,更新UI显示图片
protected void onPostExecute(Bitmap result){
if(result!=null){
iv_pic.setImageBitmap(result)
}
}
}