温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

listView优化,是否滑动,第一行,最后一行判断,判断何时要加载图片

发布时间:2020-07-12 07:52:02 来源:网络 阅读:653 作者:Be立米 栏目:移动开发

listView.setOnScrollListener(new OnScrollListener() {


private boolean isFling;

private boolean isLastRow;

private boolean isTop;


@Override

public void onScrollStateChanged(AbsListView view, int scrollState) {

isFling=(scrollState == OnScrollListener.SCROLL_STATE_FLING);

if (!isLastRow&&!isTop && isFling) {

adapter.setLoading(false);//不加载图片

}else

adapter.setLoading(true);//加载图片

}


@Override

public void onScroll(AbsListView view, int firstVisibleItem,

int visibleItemCount, int totalItemCount) {

  //判断是否滚到第一行

           if (totalItemCount>0) {

if (firstVisibleItem == 0) {

isTop = true;

}else {

isTop = false;

}

}

           //判断是否滚到最后一行    

           if (firstVisibleItem + visibleItemCount == totalItemCount && totalItemCount > 0) {    

               isLastRow = true;    

           }else {

isLastRow = false;

           }  

}

});




//adapter部分代码

private boolean isLoading=true;


public void setLoading(boolean isLoading) {

this.isLoading = isLoading;

this.notifyDataSetChanged();

}

@Override

public View getView(int position, View convertView, ViewGroup parent) {

ListViewHolder viewHolder=null;

if (convertView==null)

{

viewHolder=new ListViewHolder();

convertView=View.inflate(context, R.layout.base_list_item, null);

viewHolder.tvTitle=(TextView) convertView.findViewById(R.id.tv_base_list_title);

viewHolder.tvTime=(TextView) convertView.findViewById(R.id.tv_base_list_time);

viewHolder.ivMsg=(ImageView) convertView.findViewById(R.id.iv_base_list_item);

convertView.setTag(viewHolder);

}else

{

viewHolder=(ListViewHolder) convertView.getTag();

}


Log.i("ListItemAdapter", "type:"+type);


InfoNews infoNews = ((InfoNews)list.get(position));

viewHolder.tvTitle.setText(infoNews.getTit());

String src = infoNews.getSrc();

if (src==null) {

src="";

}

viewHolder.tvTime.setText(infoNews.getAddDate()+"    "+src);

String path =infoNews.getImageUrl();

if(isLoading){

viewHolder.ivMsg.setTag(path);

Bitmap bm = loader.loadImage(path, 1);//异步加载图片

if (bm != null) {

viewHolder.ivMsg.setImageBitmap(bm);

} else {

viewHolder.ivMsg

.setImageResource(R.drawable.full_opacity);

}

} else{

viewHolder.ivMsg.setTag("abc");

viewHolder.ivMsg.setImageResource(R.drawable.full_opacity);

}

return convertView;

}

class ListViewHolder

{

TextView tvTitle;

TextView tvTime;

ImageView ivMsg;


}









import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.URL;


import android.graphics.Bitmap;

import android.graphics.Bitmap.CompressFormat;

import android.graphics.BitmapFactory;

import android.graphics.BitmapFactory.Options;

import android.util.Log;


public class BitmapUtils {

/* 下载图片的方法 */

public static Bitmap Downloadpic(String Url,int type) throws Exception{

try {

URL url = new URL(Url);

//URL url = new URL("https://cache.yisu.com/upload/information/20200311/46/202112.jpg");

HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

httpConn.setReadTimeout(20000);

httpConn.setConnectTimeout(20000);

InputStream input = httpConn.getInputStream();

Bitmap bit = BitmapFactory.decodeStream(input);

input.close();

FileOutputStream b = null;

//

String name = Url.substring(Url.lastIndexOf("/"));

String fileName = "/sdcard/dxt/p_w_picpath/"+name ;

File file = new File(fileName);

           save(bit, file,type);


return bit;

} catch (IOException e) {

// TODO Auto-generated catch block

throw new Exception(e.getMessage());

}

}

/**

*

* @param data

* @param width

* @param height

* @return

*/

public static Bitmap loadBitmap(byte[] data, int width, int height) {

Options opts = new Options();

opts.inJustDecodeBounds = true;

BitmapFactory.decodeByteArray(data, 0, data.length, opts);

int xScale = opts.outWidth / width;

int yScale = opts.outHeight / height;

opts.inSampleSize = xScale > yScale ? xScale : yScale;

opts.inJustDecodeBounds = false;

Bitmap bit = BitmapFactory.decodeByteArray(data, 0, data.length, opts);

return bit;

}


/**

*

* @param path

* @return

*/

public static Bitmap loadBitmap(String path) {

return BitmapFactory.decodeFile(path);

}


/**

*

* @param bm

* @param file

*/

public static void save(Bitmap bm, File file,int type) throws IOException {

Log.i("dxt", "save bm:"+file.getName());

if (!file.getParentFile().exists()) {

file.getParentFile().mkdirs();

}

if (!file.exists()) {

file.createNewFile();

}


FileOutputStream out = new FileOutputStream(file);

if (type ==1 ) {

bm.compress(CompressFormat.JPEG, 10, out);

}else {

bm.compress(CompressFormat.JPEG, 100, out);

}

out.close();

}

}










import java.io.File;

import java.io.IOException;

import java.lang.ref.SoftReference;

import java.util.ArrayList;

import java.util.HashMap;


import org.apache.http.HttpEntity;


import android.content.Context;

import android.graphics.Bitmap;

import android.os.Handler;

import android.os.Message;

import android.util.Log;


import com.ywtx.dxt.util.BitmapUtils;

import com.ywtx.dxt.util.HttpService;


public class AsyncImageLoader {

private Context context;

private boolean isLoop;

private ArrayList<ImageLoadTask> tasks;

private Thread workThread;

private Handler handler;

private HashMap<String, SoftReference<Bitmap>> caches;


public AsyncImageLoader(final Context context, final Callback callback) {

this.context = context;

this.isLoop = true;

this.tasks = new ArrayList<AsyncImageLoader.ImageLoadTask>();

this.caches = new HashMap<String, SoftReference<Bitmap>>();

this.handler = new Handler() {

public void handleMessage(android.os.Message msg) {

ImageLoadTask task = (ImageLoadTask) msg.obj;

callback.p_w_picpathLoaded(task.path, task.bitmap,task.type);

};

};

this.workThread = new Thread() {

@Override

public void run() {

while (isLoop) {

while (isLoop && !tasks.isEmpty()) {

try {

ImageLoadTask task = tasks.remove(0);

try {

Log.i("dxt", "runtask.path = "+task.path);

task.bitmap = BitmapUtils.Downloadpic(task.path,task.type);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

Message msg = Message.obtain(handler, 0, task);

msg.sendToTarget();


caches.put(task.path, new SoftReference<Bitmap>(task.bitmap));

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


if (!isLoop)

break;


synchronized (this) {

try {

this.wait();

} catch (InterruptedException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}


};

this.workThread.start();

}


public Bitmap loadImage(String path,int type) {

if (caches.containsKey(path)) {

Log.i("dxt", path+"缓存");

Bitmap bm = caches.get(path).get();

if (bm != null) {

return bm;

}

}

File dir = context.getCacheDir();

File file = new File(dir, path);

if (file.exists()) {

Log.i("dxt", path+"SD卡");

Bitmap bm = BitmapUtils.loadBitmap(file.getAbsolutePath());

if (bm != null) {

return bm;

}

}

ImageLoadTask task = new ImageLoadTask();

task.path = path;

task.type = type;

if (!tasks.contains(task)) {

Log.i("dxt", path+"download");

tasks.add(task);

synchronized (workThread) {

try {

workThread.notify();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

return null;

}


public void quit() {

isLoop = false;

synchronized (workThread) {

workThread.notify();

}

}


private class ImageLoadTask {

private String path;

private Bitmap bitmap;

private int type;


@Override

public boolean equals(Object o) {

ImageLoadTask task = (ImageLoadTask) o;

if (path==null||task==null||task.path==null) {

return false;

}

return path.equals(task.path);

}

}


public interface Callback {

void p_w_picpathLoaded(String path, Bitmap bitmap, int type);

}

}


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI