异步加载网络图片(多线程,和AsyncTask)

原创 2016年03月13日 21:27:41

转载请注明出处:http://blog.csdn.net/wei_chong_chong/article/details/50880293

下面实现一个异步加载网络图片的例子,加载的图片使用ListIView显示出来

异步加载图片有两种方式,多线程,和AsyncTask,其实大家都知道AsyncTask底层也是多线程来实现的


第一步。布局文件

主布局文件是一个ListVIew

<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"
    tools:context="com.example.android.MainActivity" >


    <ListView
        android:id="@+id/lv_news"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </ListView>


</RelativeLayout>
item的布局文件:

item_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="4dp" >


    <ImageView
        android:id="@+id/iv_newsimage"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:paddingLeft="2dp"
        android:src="@drawable/ic_launcher" />


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        android:paddingLeft="4dp" >


        <TextView
            android:id="@+id/tv_newstitle"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:text="TextView"
            android:textSize="15sp" />


        <TextView
            android:id="@+id/tv_newscontent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="3dp"
            android:maxLines="3"
            android:text="TextView"
            android:textSize="10dp" />
    </LinearLayout>


</LinearLayout>

创建一个NewsBean的类

public class NewsBean {
	public String newsIconUrl;
	public String newstitle;
	public String newscontent;

}

下面的操作在MainActivity中:

创建成员变量

private ListView mLisView;
private String URL = "http://www.imooc.com/api/teacher?type=4&num=30";


然后在MainActivity中创建内部类实现异步访问网络数据:


/**
	 * 实现网络的异步访问
	 * @author Administrator
	 *
	 */
	class MyAsyncTask extends AsyncTask<String, Void, List<NewsBean>>{

		@Override
		protected List<NewsBean> doInBackground(String... params) {
			// TODO Auto-generated method stub
			return getJsonData(params[0]);
		}
		@Override
		protected void onPostExecute(List<NewsBean> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			NewsAdapter adapter = new NewsAdapter(MainActivity.this, result);
			mLisView.setAdapter(adapter);
		}
	}
上面的代码在doInBackground方法中调用
getJsonData(params[0]);
方法,所以要创建一个getJsonData()的方法

/**
	 * 将url对应的JSON格式数据转化为我们所封装的NewsBean对象
	 * @param url
	 * @return
	 */
	private List<NewsBean> getJsonData(String url){
		List<NewsBean> newsBeanList = new ArrayList<NewsBean>();
		try {
			JSONObject jsonobject ;
			JSONArray jsonArray ;
			NewsBean newsBean;
			String jsonString = readStream(new URL(url).openStream());
			jsonobject = new JSONObject(jsonString);
			jsonArray = jsonobject.getJSONArray("data");
			for (int i = 0; i < jsonArray.length(); i++) {
				jsonobject = jsonArray.getJSONObject(i);
				newsBean = new NewsBean();
				newsBean.newsIconUrl = jsonobject.getString("picSmall");
				newsBean.newstitle = jsonobject.getString("name");
				newsBean.newscontent = jsonobject.getString("description");
				newsBeanList.add(newsBean);
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/**
		 * new URL(url).openStream()这句话功能与url.openConnection().getInputStream()相同
		 * 可根据URL直接联网获取网络数据,简单粗暴,返回值类型为InputStream
		 */
		return newsBeanList;
	}
在上面的getJsonData方法中我们有用到String jsonString = readStream(new URL(url).openStream());获取网络数据

创建readStream方法

/**
	 * 通过is解析网页返回的数据
	 * @param is
	 * @return
	 */
	private String readStream(InputStream is){
		InputStreamReader isr ;
		String result = "";
		String line = "";
		try {
			isr = new InputStreamReader(is,"utf-8");
			BufferedReader br = new BufferedReader(isr);
			while((line = br.readLine())!=null){
				result += line;
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}

在onCreate方法中

使用异步加载访问网络

protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mLisView = (ListView) findViewById(R.id.lv_news);
		new MyAsyncTask().execute(URL);
	}

在MainActivity中的内部类中
onPostExecute()

方法中将获取到的网络数据传递给NewsAdapter并在ListView中显示出来


创建一个NewsAdapter类

下面的改动在NewsAdapter中

创建两个成员变量:

private List<NewsBean> mList;
	private LayoutInflater mInflater;
创建构造函数:

	public NewsAdapter(Context context,List<NewsBean> data){
		mList = data;
		mInflater = LayoutInflater.from(context);
	}
getCount(),getItem(),getItemId()方法

@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}
<pre code_snippet_id="1608833" snippet_file_name="blog_20160313_4_9669111" name="code" class="java">@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}


getView方法:

@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		if(convertView == null){
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_layout, null);
			viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.iv_newsimage);
			viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_newstitle);
			viewHolder.tvContent = (TextView) convertView.findViewById(R.id.tv_newscontent);

			convertView.setTag(viewHolder);
		}else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.ivIcon.setImageResource(R.drawable.ic_launcher);
		String url = mList.get(position).newsIconUrl;
		viewHolder.ivIcon.setTag(url);//将imageView显示的图片和url进行一个绑定,避免ListView的缓存图片对正确图片带来的影响
		//使用多线程方式加载图片
		//new ImageLoader().showImageByThread(viewHolder.ivIcon, mList.get(position).newsIconUrl);
		//使用AsyncTask加载网络图片
		new ImageLoader().showImageByAsyncTask(viewHolder.ivIcon, mList.get(position).newsIconUrl);
		viewHolder.tvTitle.setText(mList.get(position).newstitle);
		viewHolder.tvContent.setText(mList.get(position).newscontent);
		return convertView;
	}
上面getView方法中,为了提高listview显示的效率,避免重绘,创建内部类:


class ViewHolder{
		public TextView tvTitle,tvContent;
		public ImageView ivIcon;

	}

下面创建ImageLoader类实现图片的异步加载

下面的改动在ImageLoader类中:‘

创建成员变量

private ImageView mImageView;
	private String mUrl;
方法一创建方法线程方法使用多线程加载网络图片
	public void showImageByThread(ImageView imageView,final String url){
		mImageView = imageView;
		mUrl = url;
		new Thread(){
			@Override
			public void run() {
				// TODO Auto-generated method stub
				Bitmap bitmap = getBitmapFromURL(url);
				Message message = Message.obtain();
				message.obj = bitmap;
				mHandler.sendMessage(message);
			}
		}.start();
	}

创建方法从网络中获取图片

public Bitmap getBitmapFromURL(String urlString){
		Bitmap bitmap;
		InputStream is = null;
		try {
			URL url = new URL(urlString);
			HttpURLConnection connection = (HttpURLConnection) url.openConnection();
			is = new BufferedInputStream(connection.getInputStream());
			bitmap = BitmapFactory.decodeStream(is);
			connection.disconnect();

			//Thread.sleep(1000);//模拟网速不好的情况,实际应用中要去掉
			return bitmap;
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				is.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return null;
	}

创建成员变量:在主线程中更新UI

private Handler mHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			if(mImageView.getTag().equals(mUrl) ){
				mImageView.setImageBitmap((Bitmap) msg.obj);
			}

		};
	};

方法二使用ASyncTask加载网络图片


/**
	 * 使用AsyncTask加载网络图片
	 */

	public  void showImageByAsyncTask(ImageView imageView ,String url){
		//从缓存中取出图片
		Bitmap bitmap = getBitmapFromCache(url);
		if (bitmap == null) {
			//如果缓存中没有,就使用异步从网络中下载
			new NewsAsyncTask(imageView,url).execute(url);
		}else {
			//如果缓存中有就直接从缓存中读取
			imageView.setImageBitmap(bitmap);
		}

	}
创建异步加载类ASyncTask

/**
	 * 使用AsyncTask加载网络图片
	 */

	public  void showImageByAsyncTask(ImageView imageView ,String url){
			new NewsAsyncTask(imageView,url).execute(url);
		

	}

	private class NewsAsyncTask extends AsyncTask<String, Void, Bitmap>{
		private ImageView mImageView;
		private String mUrl;

		public NewsAsyncTask(ImageView imageView,String url){
			mImageView = imageView;
			mUrl = url;
		}


		@Override
		protected Bitmap doInBackground(String... params) {
			String url = params[0];
			Bitmap bitmap = getBitmapFromURL(params[0]);//从网络获取图片
			return bitmap;
		}
		@Override
		protected void onPostExecute(Bitmap result) {
			// TODO Auto-generated method stub
			if (mImageView.getTag().equals(mUrl)) {
				mImageView.setImageBitmap(result);
			}

		}

	}

所有代码:

MainActivity:

package com.example.android;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;


public class MainActivity extends Activity {
	private ListView mLisView;
	private String URL = "http://www.imooc.com/api/teacher?type=4&num=30";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		mLisView = (ListView) findViewById(R.id.lv_news);
		new MyAsyncTask().execute(URL);
	}

	/**
	 * 将url对应的JSON格式数据转化为我们所封装的NewsBean对象
	 * @param url
	 * @return
	 */
	private List<NewsBean> getJsonData(String url){
		List<NewsBean> newsBeanList = new ArrayList<NewsBean>();
		try {
			JSONObject jsonobject ;
			JSONArray jsonArray ;
			NewsBean newsBean;
			String jsonString = readStream(new URL(url).openStream());
			jsonobject = new JSONObject(jsonString);
			jsonArray = jsonobject.getJSONArray("data");
			for (int i = 0; i < jsonArray.length(); i++) {
				jsonobject = jsonArray.getJSONObject(i);
				newsBean = new NewsBean();
				newsBean.newsIconUrl = jsonobject.getString("picSmall");
				newsBean.newstitle = jsonobject.getString("name");
				newsBean.newscontent = jsonobject.getString("description");
				newsBeanList.add(newsBean);
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/**
		 * new URL(url).openStream()这句话功能与url.openConnection().getInputStream()相同
		 * 可根据URL直接联网获取网络数据,简单粗暴,返回值类型为InputStream
		 */
		return newsBeanList;
	}
	/**
	 * 通过is解析网页返回的数据
	 * @param is
	 * @return
	 */
	private String readStream(InputStream is){
		InputStreamReader isr ;
		String result = "";
		String line = "";
		try {
			isr = new InputStreamReader(is,"utf-8");
			BufferedReader br = new BufferedReader(isr);
			while((line = br.readLine())!=null){
				result += line;
			}
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 实现网络的异步访问
	 * @author Administrator
	 *
	 */
	class MyAsyncTask extends AsyncTask<String, Void, List<NewsBean>>{

		@Override
		protected List<NewsBean> doInBackground(String... params) {
			// TODO Auto-generated method stub
			return getJsonData(params[0]);
		}
		@Override
		protected void onPostExecute(List<NewsBean> result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			NewsAdapter adapter = new NewsAdapter(MainActivity.this, result);
			mLisView.setAdapter(adapter);
		}
	}
}

NewsAdapter

<span style="font-family: Arial, Helvetica, sans-serif; font-size: 12px;">public class NewsAdapter extends BaseAdapter{</span>
	private List<NewsBean> mList;
	private LayoutInflater mInflater;

	public NewsAdapter(Context context,List<NewsBean> data){
		mList = data;
		mInflater = LayoutInflater.from(context);
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mList.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		if(convertView == null){
			viewHolder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.item_layout, null);
			viewHolder.ivIcon = (ImageView) convertView.findViewById(R.id.iv_newsimage);
			viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.tv_newstitle);
			viewHolder.tvContent = (TextView) convertView.findViewById(R.id.tv_newscontent);

			convertView.setTag(viewHolder);
		}else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		viewHolder.ivIcon.setImageResource(R.drawable.ic_launcher);
		String url = mList.get(position).newsIconUrl;
		viewHolder.ivIcon.setTag(url);//将imageView显示的图片和url进行一个绑定,避免ListView的缓存图片对正确图片带来的影响
		//使用多线程方式加载图片
		//new ImageLoader().showImageByThread(viewHolder.ivIcon, mList.get(position).newsIconUrl);
		//使用AsyncTask加载网络图片
		new ImageLoader().showImageByAsyncTask(viewHolder.ivIcon, mList.get(position).newsIconUrl);
		viewHolder.tvTitle.setText(mList.get(position).newstitle);
		viewHolder.tvContent.setText(mList.get(position).newscontent);
		return convertView;
	}
	class ViewHolder{
		public TextView tvTitle,tvContent;
		public ImageView ivIcon;

	}

}
图片加载类:

package com.example.android;


import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;


/**
 * ImageLoader专门用于处理图片加载的类
 * @author Administrator
 *
 */


public class ImageLoader {
<span style="white-space:pre">	</span>private ImageView mImageView;
<span style="white-space:pre">	</span>private String mUrl;


<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 使用多线程加载图片
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>private Handler mHandler = new Handler(){
<span style="white-space:pre">		</span>public void handleMessage(android.os.Message msg) {
<span style="white-space:pre">			</span>super.handleMessage(msg);
<span style="white-space:pre">			</span>if(mImageView.getTag().equals(mUrl) ){
<span style="white-space:pre">				</span>mImageView.setImageBitmap((Bitmap) msg.obj);
<span style="white-space:pre">			</span>}


<span style="white-space:pre">		</span>};
<span style="white-space:pre">	</span>};
<span style="white-space:pre">	</span>public void showImageByThread(ImageView imageView,final String url){
<span style="white-space:pre">		</span>mImageView = imageView;
<span style="white-space:pre">		</span>mUrl = url;
<span style="white-space:pre">		</span>new Thread(){
<span style="white-space:pre">			</span>@Override
<span style="white-space:pre">			</span>public void run() {
<span style="white-space:pre">				</span>// TODO Auto-generated method stub
<span style="white-space:pre">				</span>Bitmap bitmap = getBitmapFromURL(url);
<span style="white-space:pre">				</span>Message message = Message.obtain();
<span style="white-space:pre">				</span>message.obj = bitmap;
<span style="white-space:pre">				</span>mHandler.sendMessage(message);
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}.start();
<span style="white-space:pre">	</span>}




<span style="white-space:pre">	</span>public Bitmap getBitmapFromURL(String urlString){
<span style="white-space:pre">		</span>Bitmap bitmap;
<span style="white-space:pre">		</span>InputStream is = null;
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>URL url = new URL(urlString);
<span style="white-space:pre">			</span>HttpURLConnection connection = (HttpURLConnection) url.openConnection();
<span style="white-space:pre">			</span>is = new BufferedInputStream(connection.getInputStream());
<span style="white-space:pre">			</span>bitmap = BitmapFactory.decodeStream(is);
<span style="white-space:pre">			</span>connection.disconnect();


<span style="white-space:pre">			</span>//Thread.sleep(1000);//模拟网速不好的情况,实际应用中要去掉
<span style="white-space:pre">			</span>return bitmap;
<span style="white-space:pre">		</span>} catch (MalformedURLException e) {
<span style="white-space:pre">			</span>// TODO Auto-generated catch block
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>} catch (IOException e) {
<span style="white-space:pre">			</span>// TODO Auto-generated catch block
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>}finally{
<span style="white-space:pre">			</span>try {
<span style="white-space:pre">				</span>is.close();
<span style="white-space:pre">			</span>} catch (IOException e) {
<span style="white-space:pre">				</span>// TODO Auto-generated catch block
<span style="white-space:pre">				</span>e.printStackTrace();
<span style="white-space:pre">			</span>}
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>return null;
<span style="white-space:pre">	</span>}




<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 使用AsyncTask加载网络图片
<span style="white-space:pre">	</span> */


<span style="white-space:pre">	</span>public  void showImageByAsyncTask(ImageView imageView ,String url){
<span style="white-space:pre">		</span>new NewsAsyncTask(imageView,url).execute(url);
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>private class NewsAsyncTask extends AsyncTask<String, Void, Bitmap>{
<span style="white-space:pre">		</span>private ImageView mImageView;
<span style="white-space:pre">		</span>private String mUrl;


<span style="white-space:pre">		</span>public NewsAsyncTask(ImageView imageView,String url){
<span style="white-space:pre">			</span>mImageView = imageView;
<span style="white-space:pre">			</span>mUrl = url;
<span style="white-space:pre">		</span>}




<span style="white-space:pre">		</span>@Override
<span style="white-space:pre">		</span>protected Bitmap doInBackground(String... params) {
<span style="white-space:pre">			</span>// TODO Auto-generated method stub
<span style="white-space:pre">			</span>return getBitmapFromURL(mUrl);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>@Override
<span style="white-space:pre">		</span>protected void onPostExecute(Bitmap result) {
<span style="white-space:pre">			</span>// TODO Auto-generated method stub
<span style="white-space:pre">			</span>if (mImageView.getTag().equals(mUrl)) {
<span style="white-space:pre">				</span>mImageView.setImageBitmap(result);
<span style="white-space:pre">			</span>}


<span style="white-space:pre">		</span>}


<span style="white-space:pre">	</span>}
}






版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/wei_chong_chong/article/details/50880293

Vue项目使用swiper做轮播图时,异步调用数据时导致图片不能滑动的解决方法

因为swiper提前初始化了,那个时候还没有数据,当数据调出来时已经没用了,所以有以下几个解决方法: 1、在数据调用结束后再进行swiper初始化 api.advertisingDate({}, fu...
  • weixin_38304202
  • weixin_38304202
  • 2017-09-14 14:21:18
  • 3589

vue 动态加载图片src的解决办法

好久没更博了,最近也不知道在忙啥,反正就是感觉挺忙的,在群里看到陆陆续续有刚入vue小伙伴问vue动态加载图片总是404的状况,这篇就简单的说明为什么会出现以及解决办法有哪些。首先先说明下vue-cl...
  • Mr_YanYan
  • Mr_YanYan
  • 2017-12-12 16:25:13
  • 5731

vue进行图片的预加载watch用法

watch应用场景 我想信图片预加载大家肯定都有接触过,当图片量大的时候,为了保证页面图片都加载出来的时候,我们才把主页面给显示出来,再进行一些ajax请求,或者逻辑操作 那此时你用compute...
  • hh279768876
  • hh279768876
  • 2017-03-31 14:38:18
  • 7311

vue-lazyload图片懒加载实战

插件地址:vue-lazyload 以下做一个最简答的示例: img{ border: none; } ...
  • bboyjoe
  • bboyjoe
  • 2017-05-24 16:50:07
  • 20166

vue图片懒加载

vue图片懒加载
  • Bright2017
  • Bright2017
  • 2017-09-06 13:36:02
  • 1434

Android widget桌面小部件与RemoteViewsService

概述android 中桌面插件主要依赖于AppWidget框架。涉及类: AppWidgetProvider :BroadcastRecevier子类,用于接收更新,删除通知 AppWidgetPro...
  • wbwjx
  • wbwjx
  • 2016-03-24 07:33:28
  • 684

JS实现图片的异步加载

1.概述异步加载:也称为图片的预加载。利用js代码提前加载图片,用户需要时可以直接从本地缓存获取,但是会增加服务器前端的压力。这样做可以提高用户的体验,因为同步加载大图片的时候,图片会一层一层的显示处...
  • Szu_AKer
  • Szu_AKer
  • 2016-10-21 18:00:34
  • 6351

纯js 图片异步加载原理及实现

最近自己做了一个个人网站 茶余饭后网 http://www.cyfhw.top。 在做页面优化时,首先是 js 异步加载。其次就是 图片异步加载。 关于页面优化 可以看:雅虎网站页面性能优...
  • hch126163
  • hch126163
  • 2016-01-18 11:11:41
  • 3805

异步加载图片(一)

前言:本以为异步加载挺简单,因为网上代码多,但真想要做好,还真不那么简单,从看代码到弄懂再到自己写,实在是有太多的东西需要学了,用了两天的时间,终于弄出来了,因为用到回调函数,所以理解起来可能难度有点...
  • harvic880925
  • harvic880925
  • 2014-01-02 21:19:57
  • 20777

GCD的简单应用--异步加载图片

在这里,简单介绍下GCD的应用。 1.编程场景 :     在iPhone上做一个下载图片的功能,很简单,就是在屏幕上放置一个按钮,点击该按钮,显示一个转动的圆圈,表示正在下载,下载完成后,将图片...
  • qq_34417314
  • qq_34417314
  • 2016-09-06 14:05:36
  • 919
收藏助手
不良信息举报
您举报文章:异步加载网络图片(多线程,和AsyncTask)
举报原因:
原因补充:

(最多只允许输入30个字)