滚动优化
通过对ListView滚动状态的判断来决定何时进行异步加载图片,进而有效的解决ListView滚动卡顿的问题。
1.修改NewsAdapter类
通过实现AbsListView.OnScrollListener接口并编写onScrollStateChanged、onScroll两个方法来决定何时加载图片。
并且优化首次启动的时候就要开始预加载图片。
MyApplication/app/src/main/java/com/example/myapplication/NewsAdapter.java
package com.example.myapplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import java.util.List;
public class NewsAdapter extends BaseAdapter implements AbsListView.OnScrollListener {
private List<NewBean> mList;
private LayoutInflater mInflater;
private ImageLoader mImageLoader;
private int mstart,mend;
public static String[] URLS;
//是否第一次启动
private boolean mFirst;
public NewsAdapter(Context context, List<NewBean> data, ListView listView){
//构造方法
mList = data;
mInflater = LayoutInflater.from(context);
mImageLoader = new ImageLoader(listView);
URLS = new String[data.size()];
for (int i = 0;i < data.size();i++){
URLS[i] = data.get(i).getPicUrl();
}
//绑定监听事件
listView.setOnScrollListener(this);
//初始化值
mFirst = true;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if(convertView == null){
viewHolder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item_layout, null);
viewHolder.newPic = convertView.findViewById(R.id.new_pic);
viewHolder.newTitle = convertView.findViewById(R.id.new_title);
viewHolder.newTime = convertView.findViewById(R.id.new_time);
viewHolder.newDesc = convertView.findViewById(R.id.new_desc);
viewHolder.newContent = convertView.findViewById(R.id.new_content);
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.newPic.setImageResource(R.mipmap.ic_launcher);
String url = mList.get(position).getPicUrl();
viewHolder.newPic.setTag(url);
//new ImageLoader().showImagerByThread(viewHolder.newPic, mList.get(position).getPicUrl());
//new ImageLoader().showImageByAsyncTask(viewHolder.newPic, mList.get(position).getPicUrl());
mImageLoader.showImageByAsyncTask(viewHolder.newPic, mList.get(position).getPicUrl());
viewHolder.newTitle.setText(mList.get(position).getTitle());
viewHolder.newTime.setText(mList.get(position).getTime());
viewHolder.newDesc.setText(mList.get(position).getDesc());
viewHolder.newContent.setText(mList.get(position).getContentUrl());
return convertView;
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//滑动状态的时候调用
if(scrollState == SCROLL_STATE_IDLE){
//停止滚动的时候
mImageLoader.loadImages(mstart,mend);
} else{
//停止任务
mImageLoader.cancelAllTasks();
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
//滑不滑动都会调用
mstart = firstVisibleItem;
mend = firstVisibleItem + visibleItemCount;
//第一次显示调用
if(mFirst == true && visibleItemCount > 0){
mImageLoader.loadImages(mstart,mend);
mFirst = false;
}
}
class ViewHolder{
public TextView newTitle, newTime, newDesc, newContent;
public ImageView newPic;
}
}
2.编写ImageLoader类
编辑加载的loadImages方法和停止加载的cancelAllTasks方法。 MyApplication/app/src/main/java/com/example/myapplication/ImageLoader.java
package com.example.myapplication;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.LruCache;
import android.widget.ImageView;
import android.widget.ListView;
import javax.net.ssl.HttpsURLConnection;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
public class ImageLoader {
//创建Cache
private LruCache<String, Bitmap> mCaches;
private ListView mListView;
private Set<NewsAsyncTask> mTask;
public ImageLoader(ListView listView){
//构造方法
mListView = listView;
mTask = new HashSet<>();
//获取最大可用内存
int maxMemory = (int) Runtime.getRuntime().maxMemory();
//缓存为内存的四分之一
int cacheSize = maxMemory / 4;
mCaches = new LruCache<String, Bitmap>(cacheSize){
@Override
protected int sizeOf(String key, Bitmap value) {
//每次加入内存缓存的时候进行调用,把value的实际大小放进去
return value.getByteCount();
}
};
}
/**
* 增加到缓存
* @param url
* @param bitmap
*/
public void addBitmapToCache(String url, Bitmap bitmap){
//判断缓存中是否有这个值
if(getBitmapFromCache(url) == null){
mCaches.put(url, bitmap);
}
}
/**
* 从缓存中读取数据
* @param url
* @return
*/
public Bitmap getBitmapFromCache(String url){
//获取缓存的对象
return mCaches.get(url);
}
/**
* 根据url获取图片的Bitmap对象
* @param urlString
* @return
*/
public Bitmap getBitmapFromURL(String urlString){
Bitmap bitmap;
InputStream is = null;
try {
URL url = new URL(urlString);
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
is = new BufferedInputStream(connection.getInputStream());
bitmap = BitmapFactory.decodeStream(is);
connection.disconnect();
return bitmap;
} catch (java.io.IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
public void showImageByAsyncTask(ImageView imageView,String url){
//判断缓存中是否有
Bitmap bitmap = getBitmapFromCache(url);
if(bitmap == null){
//缓存中没有就去下载
//new NewsAsyncTask(imageView, url).execute(url);
//没有就给默认的
imageView.setImageResource(R.mipmap.ic_launcher);
}else{
imageView.setImageBitmap(bitmap);
}
}
/**
* 停止运行的任务
*/
public void cancelAllTasks(){
if(mTask != null){
for (NewsAsyncTask task : mTask){
task.cancel(false);
}
}
}
public void loadImages(int start, int end){
for (int i = start; i < end; i++) {
String url = NewsAdapter.URLS[i];
//判断缓存中是否有
Bitmap bitmap = getBitmapFromCache(url);
if(bitmap == null){
//缓存中没有就去下载
//new NewsAsyncTask(imageView, url).execute(url);
NewsAsyncTask task = new NewsAsyncTask(url);
task.execute(url);
mTask.add(task);
}else{
//imageView.setImageBitmap(bitmap);
ImageView imageView = mListView.findViewWithTag(url);
imageView.setImageBitmap(bitmap);
}
}
}
/**
* AsyncTask的方式把图片放入view
*/
private class NewsAsyncTask extends AsyncTask<String, Void, Bitmap>{
//private ImageView mImageView;
private String murl;
public NewsAsyncTask(String url){
//mImageView = imageView;
murl = url;
}
@Override
protected Bitmap doInBackground(String... strings) {
String url = strings[0];
//从网络中获取图片
Bitmap bitmap = getBitmapFromURL(url);
if(bitmap != null){
//增加到缓存
addBitmapToCache(url, bitmap);
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
//防止图片重复判断唯一性
// if(mImageView.getTag().equals(murl)) {
// mImageView.setImageBitmap(bitmap);
// }
ImageView imageView = mListView.findViewWithTag(murl);
imageView.setImageBitmap(bitmap);
if(imageView != null && bitmap != null){
imageView.setImageBitmap(bitmap);
}
mTask.remove(this);
}
}
}
3.修改MainActivity的调用NewsAdapter类
传参多传一个mListView。
MyApplication/app/src/main/java/com/example/myapplication/MainActivity.java
/**
* 实现网络的异步访问
*/
class NewsAsyncTask extends AsyncTask<String, Void, List<NewBean>>{
@Override
protected List<NewBean> doInBackground(String... strings) {
return getJsonData(strings[0]);
}
@Override
protected void onPostExecute(List<NewBean> newBeans) {
super.onPostExecute(newBeans);
NewsAdapter adapter = new NewsAdapter(MainActivity.this, newBeans, mListView);
mListView.setAdapter(adapter);
}
}
评论