中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

孝感市| 沿河| 达尔| 镇江市| 浮梁县| 罗平县| 苍溪县| 黑河市| 遵义县| 大城县| 峡江县| 保亭| 林西县| 常德市| 丹寨县| 平泉县| 汤阴县| 象州县| 琼结县| 甘谷县| 定日县| 广元市| 扎囊县| 巴中市| 开阳县| 漳平市| 普兰店市| 郧西县| 墨脱县| 岳阳市| 苍山县| 迁西县| 壤塘县| 横峰县| 明星| 会理县| 准格尔旗| 天祝| 舟曲县| 龙泉市| 阿拉尔市|