/** * 从网络上异步加载单个图片 *  *   */public class LoadOneImageFromNet extends Activity {Button button;ImageView iView;String imageUrl = "http://image.club.china.com/twhb/7137652/2012/11/17/1353147018580.jpg";@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);this.setContentView(R.layout.loadoneimagefromnet);button = (Button) this.findViewById(R.id.oneImageButton);iView = (ImageView) this.findViewById(R.id.oneImage);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {executeAsyncTask();}});}/** * 启动并执行异步加载任务 */public void executeAsyncTask() {BitMapWorkTask bitMapWorkTask = new BitMapWorkTask(iView);iView.setImageResource(R.drawable.ic_launcher);// 设置加载等待图片bitMapWorkTask.execute(imageUrl);Log.i("info", "图片加载完成!");}/** * 计算图片缩小比例 *  * @param options * @param reqWidth * @param reqHeight * @return */public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {final int height = options.outHeight;final int width = options.outWidth;int inSampleSize = 1;if (height > reqHeight || width > reqWidth) {final int halfHeight = height / 2;final int halfWidth = width / 2;while ((halfHeight / inSampleSize) > reqHeight&& (halfWidth / inSampleSize) > reqWidth) {inSampleSize *= 2;}}return inSampleSize;}/** * 从网络加载图片 *  * @param uri * @return */public Bitmap decodeStreamBitmap(String uri) {Bitmap bitmap = null;HttpGet get = new HttpGet(uri);try {HttpClient client = new DefaultHttpClient();HttpResponse response = client.execute(get);HttpEntity entity = response.getEntity();InputStream inputStream = entity.getContent();BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeStream(inputStream, null, options);int inSampleSize = calculateInSampleSize(options, 160, 130);options.inSampleSize = inSampleSize;options.inJustDecodeBounds = false;response = client.execute(get);entity = response.getEntity();inputStream = entity.getContent();bitmap = BitmapFactory.decodeStream(inputStream, null, options);} catch (Exception e) {e.printStackTrace();}return bitmap;}/** * 异步加载数据类 *  *    */class BitMapWorkTask extends AsyncTask<String, Void, Bitmap> {// 定义一个对ImageView的弱引用,目的是异步线程不会干扰GC对ImageView对象的回收WeakReference<ImageView> imageReference;public BitMapWorkTask(ImageView imageView) {imageReference = new WeakReference<ImageView>(imageView);}@Overrideprotected Bitmap doInBackground(String... params) {return decodeStreamBitmap(params[0]);}@Overrideprotected void onPostExecute(Bitmap result) {// TODO Auto-generated method stubif (result != null && imageReference != null) {ImageView imageView = imageReference.get();if (imageView != null) {imageView.setImageBitmap(result);}}}}}

<?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="match_parent"    android:orientation="vertical" >    <Button        android:id="@+id/oneImageButton"        android:layout_width="fill_parent"        android:layout_height="50dp" android:text="load image from Internet"/>    <ImageView        android:id="@+id/oneImage"        android:layout_width="match_parent"        android:layout_height="match_parent"        android:adjustViewBounds="true" /></LinearLayout>

效果图(点击按钮加载图片):



Android 异步加载一张网络图片

更多相关文章

  1. Android中判断app是否连接网络
  2. android 使图片显示 圆角
  3. Android Http网络开发神兵利器
  4. android ImageUtils 图片处理工具类
  5. Android 获取SDCard上图片和视频的缩略图
  6. android 网络图片与网页读取

随机推荐

  1. Android根据联系人姓名首字符顺序读取通
  2. 【 Android(安卓)10 系统启动 】系列 --
  3. android 浮层简单实现、activity设置Them
  4. Android(安卓)ui utils-简单实用的Androi
  5. Android(安卓)Studio 运行项目报错:org.ob
  6. Android属性汇总
  7. Activity与Fragment通过接口回调进行通信
  8. 关于Android的nodpi,xhdpi,hdpi,mdpi,ldp
  9. Android(安卓)logcat详细用法
  10. android菜单详解六:快捷键和菜单intent