/** * 从网络上异步加载单个图片 *  *   */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>

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





更多相关文章

  1. Android(安卓)获取SDCard上图片和视频的缩略图
  2. Android(安卓)Http网络开发神兵利器
  3. ANDROID 使用 Service 在手机锁屏休眠状态下后台执行发送短信息
  4. android 选择图片(从手机照相机或手机图片)
  5. Android(安卓)ListView滑动加载
  6. android ImageUtils 图片处理工具类
  7. Android(安卓)使用系统摄像头拍相片与拍视频,并显示
  8. android 使图片显示 圆角
  9. android 线程睡几秒

随机推荐

  1. Android(安卓)监听WiFi的开关状态实现代
  2. android 设置APN
  3. [Android]android.util.AndroidRuntimeEx
  4. Android强制在主线程进行网络请求
  5. Android中如何获取应用版本号
  6. Android全局变量
  7. Android电池电量状态源码
  8. Android双击返回键退出程序
  9. Android之NetworkOnMainThreadException
  10. Android(安卓)Layout Tricks #3: Optimiz