91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

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

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

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

平远县| 和田县| 阿克苏市| 溧水县| 耒阳市| 泰安市| 福鼎市| 和静县| 长沙县| 肃宁县| 射洪县| 新昌县| 定结县| 开化县| 龙门县| 左权县| 射阳县| 都江堰市| 祁门县| 凤冈县| 西贡区| 大田县| 新龙县| 舟曲县| 南皮县| 博客| 包头市| 新疆| 塔河县| 靖宇县| 余江县| 无极县| 合阳县| 石渠县| 克什克腾旗| 许昌市| 明水县| 尖扎县| 昌乐县| 五河县| 芒康县|