package baway.com.yuejunfei1503a20170424; import android.app.Application; import android.os.Environment; import android.util.Log; import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache; import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator; import com.nostra13.universalimageloader.cache.memory.impl.LruMemoryCache; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import com.nostra13.universalimageloader.utils.StorageUtils; import java.io.File; /** * 类的作用: * 作者:岳俊飞 * 时间:2017/4/24
权限中添加name---类名 和导入image_loader jar包
添加写入 读取权限 */ public class MyImageLoader extends Application { @Override public void onCreate() { super.onCreate(); try { File cacheDir = StorageUtils.getOwnCacheDirectory(this, Environment.getExternalStorageDirectory().getPath()); ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(this) .threadPoolSize(3)//配置线程数量 .memoryCache(new LruMemoryCache(2 * 1024 * 1024))//内存缓存图片 2M .diskCache(new UnlimitedDiskCache(cacheDir))//配饰sdcard缓存路径 .diskCacheSize(50 * 1024 * 1024)//sdcard上缓存50M的图片 .diskCacheFileCount(100)//缓存文件的数量 100个 .diskCacheFileNameGenerator(new Md5FileNameGenerator()) .build(); //配置缓存选项 ImageLoader.getInstance().init(configuration); } catch (Exception e) { e.printStackTrace(); } } }
package baway.com.yuejunfei1503a20170424; import android.content.Context; import android.net.ConnectivityManager; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.ImageView; import android.widget.TextView; import com.nostra13.universalimageloader.core.DisplayImageOptions; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import com.nostra13.universalimageloader.core.assist.ImageScaleType; import java.util.List; /** * 类的作用: * 作者:岳俊飞 * 时间:2017/4/24 */ public class Mydapter extends BaseAdapter { private Context context; private List<Bean.SubjectsBean> list; //配置图片的缓存选项 private DisplayImageOptions options; public Mydapter(Context context, List<Bean.SubjectsBean> list){ this.context=context; this.list=list; //配置是否对图片进行缓存 options = new DisplayImageOptions.Builder() .cacheInMemory(true)//是否内存缓存 .cacheOnDisk(true)//是否sdcard缓存 .build();//构建图片缓存的选项 } public void addMore(List<Bean.SubjectsBean> list2,boolean isplay){ for (Bean.SubjectsBean be:list2){ if (isplay){ list.add(0,be); }else { list.add(be); } } } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.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 = convertView.inflate(context, R.layout.item, null); viewHolder.tvDesc = (TextView) convertView.findViewById(R.id.tvDes); viewHolder.tvDesc2= (TextView) convertView.findViewById(R.id.tvDes2); viewHolder.image= (ImageView) convertView.findViewById(R.id.imagev); viewHolder.image2= (ImageView) convertView.findViewById(R.id.imagev2); viewHolder.image3= (ImageView) convertView.findViewById(R.id.imagev3); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } viewHolder.tvDesc.setText(list.get(position).getOriginal_title()); viewHolder.tvDesc2.setText(list.get(position).getTitle()); ImageLoader.getInstance().displayImage(list.get(position).getCasts().get(0).getAvatars().getMedium(), viewHolder.image,options); ImageLoader.getInstance().displayImage(list.get(position).getCasts().get(1).getAvatars().getSmall(), viewHolder.image2,options); ImageLoader.getInstance().displayImage(list.get(position).getCasts().get(2).getAvatars().getLarge(), viewHolder.image3,options); return convertView; } //定义V static class ViewHolder { TextView tvDesc; TextView tvDesc2; ImageView image; ImageView image2; ImageView image3; } }
package baway.com.yuejunfei1503a20170424; import android.os.AsyncTask; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; import com.google.gson.Gson; import com.limxing.xlistview.view.XListView; import com.nostra13.universalimageloader.core.ImageLoader; import com.nostra13.universalimageloader.core.ImageLoaderConfiguration; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener { private XListView xlv; private Mydapter dapter; private int pageIndex = 1; private boolean isplay=true; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); xlv = (XListView) findViewById(R.id.xlv); imageView = (ImageView) findViewById(R.id.imagev); xlv.setPullLoadEnable(true); xlv.setRefreshTime("12:23:14"); xlv.setXListViewListener(this); getHuoqu("https://api.douban.com/v2/movie/in_theaters?apikey=0b2bdeda43b5688921839c8ecb20399b&city=北京&start="+pageIndex+"&count=2"); } private void getHuoqu(String s) { new AsyncTask<String, Void, String>() { @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson = new Gson(); Bean bean = gson.fromJson(s, Bean.class); if (bean==null){ return; } if (dapter==null){ dapter=new Mydapter(MainActivity.this,bean.getSubjects()); }else { dapter.addMore(bean.getSubjects(),isplay); dapter.notifyDataSetChanged(); } xlv.setAdapter(dapter); } @Override protected String doInBackground(String... params) { try { String str=params[0]; URL url = new URL(str); HttpURLConnection connection= (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setReadTimeout(5000); connection.setConnectTimeout(5000); int code = connection.getResponseCode(); if (code==200){ InputStream inputStream = connection.getInputStream(); String upec = Utils.upec(inputStream); System.out.println("haha"+upec); //将网络数据缓存到sdcard / SQLite SharedPerfrenced File file = new File(Environment.getExternalStorageDirectory(),"menuinfo.txt"); if (!file.exists()){ file.createNewFile(); } OutputStream os = new FileOutputStream(file); os.write(str.getBytes()); //关闭流 inputStream.close(); os.close(); return upec; } } catch (Exception e) { e.printStackTrace(); } return null; } }.execute(s); } @Override public void onRefresh() { isplay=true; pageIndex++; getHuoqu("https://api.douban.com/v2/movie/in_theaters?apikey=0b2bdeda43b5688921839c8ecb20399b&city=北京&start="+pageIndex+"&count=2"); xlv.stopRefresh(true); } @Override public void onLoadMore() { isplay=false; pageIndex++; getHuoqu("https://api.douban.com/v2/movie/in_theaters?apikey=0b2bdeda43b5688921839c8ecb20399b&city=北京&start="+pageIndex+"&count=2"); xlv.stopLoadMore(); } }