-
taskQueue.add(task);
-
// 唤醒任务下载队列
-
synchronized (runnable) {
-
runnable.notify();
-
}
-
}
-
}
-
// 缓存中没有图片则返回null
-
return null;
-
}
-
/**
-
*
-
* @param imageView
-
* @param resId 图片加载完成前显示的图片资源ID
-
* @return
-
*/
-
private ImageCallback getImageCallback(final ImageView imageView, final int resId){
-
return new ImageCallback() {
-
@Override
-
public void loadImage(String path, Bitmap bitmap) {
-
if(path.equals(imageView.getTag().toString())){
-
imageView.setImageBitmap(bitmap);
-
}else{
-
imageView.setImageResource(resId);
-
}
-
}
-
};
-
}
-
private Handler handler = new Handler(){
-
@Override
-
public void handleMessage(Message msg) {
-
// 子线程中返回的下载完成的任务
-
Task task = (Task)msg.obj;
-
// 调用callback对象的loadImage方法,并将图片路径和图片回传给adapter
-
task.callback.loadImage(task.path, task.bitmap);
-
}
-
};
-
private Runnable runnable = new Runnable() {
-
@Override
-
public void run() {
-
while(isRunning){
-
// 当队列中还有未处理的任务时,执行下载任务
-
while(taskQueue.size() > 0){
-
// 获取第一个任务,并将之从任务队列中删除
-
Task task = taskQueue.remove(0);
-
// 将下载的图片添加到缓存
-
task.bitmap = PicUtil.getbitmap(task.path);
-
caches.put(task.path, new SoftReference(task.bitmap));
-
if(handler != null){
-
// 创建消息对象,并将完成的任务添加到消息对象中
-
Message msg = handler.obtainMessage();
-
msg.obj = task;
-
// 发送消息回主线程
-
handler.sendMessage(msg);
-
}
-
}
-
//如果队列为空,则令线程等待
-
synchronized (this) {
-
try {
-
this.wait();
-
} catch (InterruptedException e) {
-
e.printStackTrace();
-
}
-
}
-
}
-
}
-
};
-
//回调接口
-
public interface ImageCallback{
-
void loadImage(String path, Bitmap bitmap);
-
}
-
class Task{
-
// 下载任务的下载路径
-
String path;
-
// 下载的图片
-
Bitmap bitmap;
-
// 回调对象
-
ImageCallback callback;
-
@Override
-
public boolean equals(Object o) {
-
Task task = (Task)o;
-
return task.path.equals(path);
-
}
-
}
-
}
最后附上PicUtil类的代码,之前忘了贴这个类的代码,不好意识了~~
[java] view plain copy
-
public class PicUtil {
-
private static final String TAG = “PicUtil”;
-
/**
-
* 根据一个网络连接(URL)获取bitmapDrawable图像
-
*
-
* @param imageUri
-
* @return
-
*/
-
public static BitmapDrawable getfriendicon(URL imageUri) {
-
BitmapDrawable icon = null;
-
try {
-
HttpURLConnection hp = (HttpURLConnection) imageUri
-
.openConnection();
-
icon = new BitmapDrawable(hp.getInputStream());// 将输入流转换成bitmap
-
hp.disconnect();// 关闭连接
-
} catch (Exception e) {
-
}
-
return icon;
-
}
-
/**
-
* 根据一个网络连接(String)获取bitmapDrawable图像
-
*
-
* @param imageUri
-
* @return
-
*/
-
public static BitmapDrawable getcontentPic(String imageUri) {
-
URL imgUrl = null;
-
try {
-
imgUrl = new URL(imageUri);
-
} catch (MalformedURLException e1) {
-
e1.printStackTrace();
-
}
-
BitmapDrawable icon = null;
-
try {
-
HttpURLConnection hp = (HttpURLConnection) imgUrl.openConnection();
-
icon = new BitmapDrawable(hp.getInputStream());// 将输入流转换成bitmap
-
hp.disconnect();// 关闭连接
-
} catch (Exception e) {
-
}
-
return icon;
-
}
-
/**
-
* 根据一个网络连接(URL)获取bitmap图像
-
*
-
* @param imageUri
-
* @return
-
*/
-
public static Bitmap getusericon(URL imageUri) {
-
// 显示网络上的图片
-
URL myFileUrl = imageUri;
-
Bitmap bitmap = null;
-
try {
-
HttpURLConnection conn = (HttpURLConnection) myFileUrl
-
.openConnection();
-
conn.setDoInput(true);
-
conn.connect();
-
InputStream is = conn.getInputStream();
-
bitmap = BitmapFactory.decodeStream(is);
-
is.close();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
return bitmap;
-
}
-
/**
-
* 根据一个网络连接(String)获取bitmap图像
-
*
-
* @param imageUri
-
* @return
-
* @throws MalformedURLException
-
*/
-
public static Bitmap getbitmap(String imageUri) {
-
// 显示网络上的图片
-
Bitmap bitmap = null;
-
try {
-
URL myFileUrl = new URL(imageUri);
-
HttpURLConnection conn = (HttpURLConnection) myFileUrl
-
.openConnection();
-
conn.setDoInput(true);
-
conn.connect();
-
InputStream is = conn.getInputStream();
-
bitmap = BitmapFactory.decodeStream(is);
-
is.close();
-
Log.i(TAG, “image download finished.” + imageUri);
-
} catch (IOException e) {
-
e.printStackTrace();
-
return null;
-
}
-
return bitmap;
-
}
-
/**
-
* 下载图片 同时写道本地缓存文件中
-
*
-
* @param context
-
* @param imageUri
-
* @return
-
* @throws MalformedURLException
-
*/
-
public static Bitmap getbitmapAndwrite(String imageUri) {
-
Bitmap bitmap = null;
-
try {
-
// 显示网络上的图片
-
URL myFileUrl = new URL(imageUri);
-
HttpURLConnection conn = (HttpURLConnection) myFileUrl
-
.openConnection();
-
conn.setDoInput(true);
-
conn.connect();
-
InputStream is = conn.getInputStream();
-
File cacheFile = FileUtil.getCacheFile(imageUri);
-
BufferedOutputStream bos = null;
-
bos = new BufferedOutputStream(new FileOutputStream(cacheFile));
-
Log.i(TAG, "write file to " + cacheFile.getCanonicalPath());
-
byte[] buf = new byte[1024];
-
int len = 0;
-
// 将网络上的图片存储到本地
-
while ((len = is.read(buf)) > 0) {
-
bos.write(buf, 0, len);
-
}
-
is.close();
-
bos.close();
-
// 从本地加载图片
-
bitmap = BitmapFactory.decodeFile(cacheFile.getCanonicalPath());
-
String name = MD5Util.MD5(imageUri);
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
return bitmap;
-
}
-
public static boolean downpic(String picName, Bitmap bitmap) {
-
boolean nowbol = false;
-
try {
-
File saveFile = new File(“/mnt/sdcard/download/weibopic/” + picName
-
+ “.png”);
-
if (!saveFile.exists()) {
-
saveFile.createNewFile();
-
}
-
FileOutputStream saveFileOutputStream;
-
saveFileOutputStream = new FileOutputStream(saveFile);
-
nowbol = bitmap.compress(Bitmap.CompressFormat.PNG, 100,
-
saveFileOutputStream);
-
saveFileOutputStream.close();
-
} catch (FileNotFoundException e) {
-
e.printStackTrace();
-
} catch (IOException e) {
-
e.printStackTrace();
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
return nowbol;
-
}
-
public static void writeTofiles(Context context, Bitmap bitmap,
-
String filename) {
-
BufferedOutputStream outputStream = null;
-
try {
-
outputStream = new BufferedOutputStream(context.openFileOutput(
-
filename, Context.MODE_PRIVATE));
-
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
-
} catch (FileNotFoundException e) {
-
e.printStackTrace();
-
}
-
}
-
/**
-
* 将文件写入缓存系统中
-
*
-
* @param filename
-
* @param is
-
* @return
-
*/
-
public static String writefile(Context context, String filename,
-
InputStream is) {
-
BufferedInputStream inputStream = null;
-
BufferedOutputStream outputStream = null;
-
try {
-
inputStream = new BufferedInputStream(is);
-
outputStream = new BufferedOutputStream(context.openFileOutput(
-
filename, Context.MODE_PRIVATE));
-
byte[] buffer = new byte[1024];
-
int length;
-
while ((length = inputStream.read(buffer)) != -1) {
-
outputStream.write(buffer, 0, length);
-
}
-
} catch (Exception e) {
-
} finally {
-
if (inputStream != null) {
-
try {
-
inputStream.close();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
if (outputStream != null) {
-
try {
-
outputStream.flush();
-
outputStream.close();
-
} catch (IOException e) {
-
e.printStackTrace();
-
}
-
}
-
}
-
return context.getFilesDir() + “/” + filename + “.jpg”;
-
}
-
// 放大缩小图片
-
public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
-
int width = bitmap.getWidth();
-
int height = bitmap.getHeight();
-
Matrix matrix = new Matrix();
-
float scaleWidht = ((float) w / width);
结尾
最后,针对上面谈的内容,给大家推荐一个Android资料,应该对大家有用。
首先是一个知识清单:(对于现在的Android及移动互联网来说,我们需要掌握的技术)
泛型原理丶反射原理丶Java虚拟机原理丶线程池原理丶
注解原理丶注解原理丶序列化
Activity知识体系(Activity的生命周期丶Activity的任务栈丶Activity的启动模式丶View源码丶Fragment内核相关丶service原理等)
代码框架结构优化(数据结构丶排序算法丶设计模式)
APP性能优化(用户体验优化丶适配丶代码调优)
热修复丶热升级丶Hook技术丶IOC架构设计
NDK(c编程丶C++丶JNI丶LINUX)
如何提高开发效率?
MVC丶MVP丶MVVM
微信小程序
Hybrid
Flutter
接下来是资料清单:(敲黑板!!!)
1.数据结构和算法
2.设计模式
3.全套体系化高级架构视频;七大主流技术模块,视频+源码+笔记
4.面试专题资料包(怎么能少了一份全面的面试题总结呢~)
不论遇到什么困难,都不应该成为我们放弃的理由!共勉~
如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
793642)]
接下来是资料清单:(敲黑板!!!)
1.数据结构和算法
[外链图片转存中…(img-yY7ApHKE-1714206793642)]
2.设计模式
[外链图片转存中…(img-efmKdNy8-1714206793643)]
3.全套体系化高级架构视频;七大主流技术模块,视频+源码+笔记
[外链图片转存中…(img-XFse64hZ-1714206793643)]
4.面试专题资料包(怎么能少了一份全面的面试题总结呢~)
[外链图片转存中…(img-vIgXoePi-1714206793643)]
不论遇到什么困难,都不应该成为我们放弃的理由!共勉~
如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。
[外链图片转存中…(img-a6JSDhpa-1714206793643)]
网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!