本文主要是介绍interface回调获取网络数据listview展示,解决image显示错乱,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
工具类NetUtil
package com.example.loadimage;import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.util.LruCache;
import android.widget.ImageView;import com.google.gson.Gson;import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.LinkedHashMap;public class NetUtil {//单例private static NetUtil instance;//图片内存缓存private LruCache<String, Bitmap> cache;//HashMap<String, Bitmap> m;//无序的//LinkedHashMap<String, Bitmap>m;//有序的private File saveDir;//私有构造方法private NetUtil(Context context) {int size = (int)(Runtime.getRuntime().freeMemory() * 0.3);cache = new LruCache<>(size);//图片文件缓存目录saveDir = new File(context.getCacheDir(), "img_cache");//不存在,创建if(!saveDir.exists()) {saveDir.mkdirs();}}//提供方法,获取对象public static NetUtil getInstance(Context context) {if(instance == null) {instance = new NetUtil(context);}return instance;}/*public interface ImageCallbak2 {void onStartLoadImage(String url, ImageView imageView);void onLoadImageSuccess(String url, Bitmap bitmap, ImageView imageView);}public void displayBitmap3(final String urlStr, final ImageView imageView) {displayBitmap(urlStr, imageView, new ImageCallbak2() {@Overridepublic void onStartLoadImage(String url, ImageView imageView) {imageView.setTag(url);}@Overridepublic void onLoadImageSuccess(String url, Bitmap bitmap, ImageView imageView) {if(imageView.getTag().equals(url)) {imageView.setImageBitmap(bitmap);} else {Log.i("TEST", "错乱");}}});}public void displayBitmap(final String url, final ImageView imageView, final ImageCallbak2 callbak2) {callbak2.onStartLoadImage(url, imageView);new AsyncTask<String, Void, Bitmap>(){@Overrideprotected Bitmap doInBackground(String... strings) {return getBitmap(strings[0]);}@Overrideprotected void onPostExecute(Bitmap bitmap) {callbak2.onLoadImageSuccess(url, bitmap, imageView);}};}*///1 定义接口:合同public interface ImageCallback {void loadImageSuccess(String url, Bitmap bitmap);}//接口回调public void getBitmap(final String urlStr, final ImageCallback callback) {new AsyncTask<String, Void, Bitmap>(){@Overrideprotected Bitmap doInBackground(String... strings) {return getBitmap(strings[0]);}@Overrideprotected void onPostExecute(Bitmap bitmap) {callback.loadImageSuccess(urlStr, bitmap);}}.execute(urlStr);}//展示图片,没有错乱public void displayBitmap2(final String urlStr, final ImageView imageView) {//绑定url到imageviewimageView.setTag(urlStr);new AsyncTask<String, Void, Bitmap>(){@Overrideprotected Bitmap doInBackground(String... strings) {return getBitmap(strings[0]);}@Overrideprotected void onPostExecute(Bitmap bitmap) {if(imageView.getTag().equals(urlStr)) {imageView.setImageBitmap(bitmap);}else {Log.i("TEST", "错乱");}}}.execute(urlStr);}//请求一张图片,展示给ImageView//异步任务public void displayBitmap(String urlStr, final ImageView imageView) {new AsyncTask<String, Void, Bitmap>(){@Overrideprotected Bitmap doInBackground(String... strings) {return getBitmap(strings[0]);}@Overrideprotected void onPostExecute(Bitmap bitmap) {imageView.setImageBitmap(bitmap);}}.execute(urlStr);}//获取图片public Bitmap getBitmap(String urlStr) {Bitmap result = null;//从内存中要result = cache.get(url2Key(urlStr));//内存命中if(result != null) {Log.i("TEST", "内存中加载图片: " + urlStr);return result;}//从磁盘中要result = getBitmapForFile(urlStr);//文件命中if(result != null) {Log.i("TEST", "磁盘中加载图片: " + urlStr);//先存到内存缓存cache.put(url2Key(urlStr), result);return result;}//从网络中请Log.i("TEST", "网络中加载图片: " + urlStr);result = getBitmapForNetwork(urlStr);//写文件saveBitmap2File(urlStr, result);//存到内存缓存cache.put(url2Key(urlStr), result);return result;}//将url转换成key//url 做key: 太长;如果作为文件名,不允许/ ?private String url2Key(String url) {//MD5 摘要方法//MessageDigestreturn String.valueOf(url.hashCode());}//将图片存到文件private void saveBitmap2File(String urlStr, Bitmap bitmap) {//格式: png jpeg(jpg)//质量: 0 100FileOutputStream fos = null;try {fos = new FileOutputStream(new File(saveDir,url2Key(urlStr)));//图片质量压缩, 并输出到输出流bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);} catch (FileNotFoundException e) {e.printStackTrace();}finally {if(fos != null) {try {fos.close();} catch (IOException e) {e.printStackTrace();}}}}//从文件读取图片private Bitmap getBitmapForFile(String urlStr) {/////解码文件为bitmap图片return BitmapFactory.decodeFile(new File(saveDir, url2Key(urlStr)).getAbsolutePath());}//执行一个网络请求,返回一个Bitmap//这一步做:请求图片private Bitmap getBitmapForNetwork(String urlStr) {Bitmap result = null;try {URL url = new URL(urlStr);HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();urlConnection.setRequestMethod("GET");urlConnection.setConnectTimeout(5000);urlConnection.setReadTimeout(5000);int responseCode = urlConnection.getResponseCode();if(responseCode == 200) {result = BitmapFactory.decodeStream(urlConnection.getInputStream());}} catch (java.io.IOException e) {e.printStackTrace();}return result;}///普通get请求public interface Callbak<T>{void onSuccess(T t);}///执行一个网络请求,通过回调返回结果public void getRequest(final String urlStr, final Class clazz, final Callbak callbak) {new AsyncTask<String, Void, Object>(){@Overrideprotected Object doInBackground(String... strings) {return getRequest(urlStr, clazz);}@Overrideprotected void onPostExecute(Object o) {callbak.onSuccess(o);}}.execute(urlStr);}//执行一个网络请求,返回beanpublic <T> T getRequest(String urlStr, Class clazz) {return (T) new Gson().fromJson(getRequest(urlStr), clazz);}//执行一个网络请求,返回String结果public String getRequest(String urlStr) {String result = "";try {URL url = new URL(urlStr);HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();urlConnection.setRequestMethod("GET");urlConnection.setConnectTimeout(5000);urlConnection.setReadTimeout(5000);int responseCode = urlConnection.getResponseCode();if(responseCode == 200) {result = stream2String(urlConnection.getInputStream());}} catch (java.io.IOException e) {e.printStackTrace();}return result;}private String stream2String(InputStream is) throws IOException {StringBuilder sb = new StringBuilder();BufferedReader br = new BufferedReader(new InputStreamReader(is));for (String tmp = br.readLine(); tmp != null; tmp = br.readLine()) {sb.append(tmp);}return sb.toString();}
}
主线程MainActivity
单一性只做操作
package com.example.loadimage;import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;public class MainActivity extends AppCompatActivity {private ListView news;private MyBaseAdafter mAdapter;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);news = findViewById(R.id.news);mAdapter = new MyBaseAdafter(this);news.setAdapter(mAdapter);findViewById(R.id.left).setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//加载数据NetUtil.getInstance(MainActivity.this).getRequest("http://api.expoon.com/AppNews/getNewsList/type/1/p/1", Bean.class, new NetUtil.Callbak<Bean>() {@Overridepublic void onSuccess(Bean o) {mAdapter.setListData(o.getData());}});}});}
}
适配器 MyBaseAdafter
package com.example.loadimage;import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;import java.util.ArrayList;
import java.util.List;public class MyBaseAdafter extends BaseAdapter {private Context context;private List<Bean.DataBean>mlist;public MyBaseAdafter(Context context) {this.context = context;mlist = new ArrayList<>();}
public void setListData(List<Bean.DataBean> list){this.mlist = list;notifyDataSetChanged();
}@Overridepublic int getCount() {return mlist.size();}@Overridepublic Bean.DataBean getItem(int position) {return mlist.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic View getView(int position, View convertView, ViewGroup parent) {final ViewHoder viewHoder;if(convertView==null){convertView = LayoutInflater.from(context).inflate(R.layout.item,parent,false);viewHoder = new ViewHoder();viewHoder.textView1 = convertView.findViewById(R.id.textview1);viewHoder.textView2 = convertView.findViewById(R.id.textview2);viewHoder.imageView = convertView.findViewById(R.id.imageview);convertView.setTag(viewHoder);}else{viewHoder = (ViewHoder) convertView.getTag();}viewHoder.textView1.setText(mlist.get(position).getNews_title());viewHoder.textView2.setText(mlist.get(position).getNews_summary());//加载图片//错乱问题//NetUtil.getInstance().displayBitmap(getItem(position).getPic_url(), viewHoder.imageView);//NetUtil.getInstance().displayBitmap2(getItem(position).getPic_url(), viewHoder.imageView);viewHoder.imageView.setTag(getItem(position).getPic_url());NetUtil.getInstance(context).getBitmap(getItem(position).getPic_url(), new NetUtil.ImageCallback() {@Overridepublic void loadImageSuccess(String url, Bitmap bitmap) {if(viewHoder.imageView.getTag().equals(url)) {viewHoder.imageView.setImageBitmap(bitmap);}else {}}});return convertView;}class ViewHoder{private TextView textView1,textView2;private ImageView imageView;}
}
BitmapUtil
package com.example.loadimage;import android.graphics.Bitmap;
import android.graphics.BitmapFactory;public class BitmapUtil {public Bitmap scaleBitmap(int width, int height) {Bitmap result = null;return result;}
}
这篇关于interface回调获取网络数据listview展示,解决image显示错乱的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!