图片下载工具类,可更新UI
2016-05-05 14:42
411 查看
package com.method.tool; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.os.Handler; import android.util.Log; import android.widget.ImageView; import android.widget.ImageView.ScaleType; /** * @describe:图片下载工具类,可更新UI。 */ public class LoadImageUtil { static Handler handler = new Handler(); private static Bitmap mBitmap; public static void loadImageFromNet(final String url, final ImageView imageview) { new Thread(new Runnable() { public void run() { URL u = null; HttpURLConnection conn = null; InputStream is = null; try { Log.i("TAG", "LoadImageUtil-URL=" + url); u = new URL(url); conn = (HttpURLConnection) u.openConnection(); is = conn.getInputStream(); final Bitmap bitmap = BitmapFactory.decodeStream(is); handler.post(new Runnable() { public void run() { /** * @describe:调整位图大小 * @attention:若在XML文件中设置此属性,会被代码中的setImageBitmap()方法覆盖,导致其失效。 */ imageview.setScaleType(ScaleType.FIT_XY); imageview.setImageBitmap(bitmap); // imageview.setImageDrawable(Handler_Bitmap.bitmap2Drawable(bitmap)); } }); } catch (Exception e) { e.printStackTrace(); } } }).start(); } public static Bitmap ratio(Bitmap image, float pixelW, float pixelH) { ByteArrayOutputStream os = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, os); if (os.toByteArray().length / 1024 > 1024) {// 判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出 os.reset();// 重置baos即清空baos image.compress(Bitmap.CompressFormat.JPEG, 50, os);// 这里压缩50%,把压缩后的数据存放到baos中 } ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray()); BitmapFactory.Options newOpts = new BitmapFactory.Options(); // 开始读入图片,此时把options.inJustDecodeBounds 设回true了 newOpts.inJustDecodeBounds = true; newOpts.inPreferredConfig = Config.RGB_565; Bitmap bitmap = BitmapFactory.decodeStream(is, null, newOpts); newOpts.inJustDecodeBounds = false; int w = newOpts.outWidth; int h = newOpts.outHeight; float hh = pixelH;// 设置高度为240f时,可以明显看到图片缩小了 float ww = pixelW;// 设置宽度为120f,可以明显看到图片缩小了 // 缩放比。由于是固定比例缩放,只用高或者宽其中一个数据进行计算即可 int be = 1;// be=1表示不缩放 if (w > h && w > ww) {// 如果宽度大的话根据宽度固定大小缩放 be = (int) (newOpts.outWidth / ww); } else if (w < h && h > hh) {// 如果高度高的话根据宽度固定大小缩放 be = (int) (newOpts.outHeight / hh); } if (be <= 0) be = 1; newOpts.inSampleSize = be;// 设置缩放比例 // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了 is = new ByteArrayInputStream(os.toByteArray()); bitmap = BitmapFactory.decodeStream(is, null, newOpts); // 压缩好比例大小后再进行质量压缩 // return compress(bitmap, maxSize); // 这里再进行质量压缩的意义不大,反而耗资源,删除 return bitmap; } }
相关文章推荐
- java-用HttpURLConnection发送Http请求.
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件