---------------------------主页面
package fragment.bwie.com.lianxi; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.widget.GridView; import com.google.gson.Gson; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.util.List; import fragment.bwie.com.lianxi.adapter.GridAdapter; import fragment.bwie.com.lianxi.adapter.MyAdapter; import fragment.bwie.com.lianxi.bean.Bean; public class MainActivity extends AppCompatActivity { private ViewPager pager; private GridView gridView; private List<Bean.DataBean.EssayBean> list; private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 0) { int currentItem = pager.getCurrentItem(); pager.setCurrentItem(currentItem + 1); handler.sendEmptyMessageDelayed(0, 2000); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pager = (ViewPager) findViewById(R.id.pager); gridView = (GridView) findViewById(R.id.gridview); AsyncTask<Void, Void, String> asyncTask = new AsyncTask<Void, Void, String>() { @Override protected String doInBackground(Void... voids) { String path = "http://v3.wufazhuce.com:8000/api/reading/index/?version=3.5.0&platform=android"; try { URL url = new URL(path); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); urlConnection.setRequestMethod("GET"); urlConnection.setConnectTimeout(5000); urlConnection.setReadTimeout(5000); int responseCode = urlConnection.getResponseCode(); if (responseCode == 200) { InputStream inputStream = urlConnection.getInputStream(); String json = streamTostring(inputStream, "utf-8"); Log.i("----------------", json); return json; } } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson = new Gson(); Bean bean = gson.fromJson(s, Bean.class); list = bean.getData().getEssay(); MyAdapter myAdapter = new MyAdapter(list, MainActivity.this , handler); pager.setAdapter(myAdapter); pager.setCurrentItem(list.size() * 10000); //设置时间 handler.sendEmptyMessageDelayed(0, 2000); pager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { } @Override public void onPageSelected(int position) { } @Override public void onPageScrollStateChanged(int state) { } }); Datass(); } }; asyncTask.execute(); } private String streamTostring(InputStream inputStream, String s) { try { InputStreamReader inputStreamReader = new InputStreamReader(inputStream, s); BufferedReader bufferedReader = new BufferedReader(inputStreamReader); StringBuilder builder = new StringBuilder(); String string = null; while ((string = bufferedReader.readLine()) != null) { builder.append(string); } bufferedReader.close(); return builder.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } private void Datass() { GridAdapter gridAdapter = new GridAdapter(list, MainActivity.this); gridView.setAdapter(gridAdapter); } }
--------------------------------------------viewpager适配器
package fragment.bwie.com.lianxi.adapter; import android.content.Context; import android.graphics.Bitmap; import android.os.Handler; import android.support.v4.view.PagerAdapter; import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; 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 com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer; import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer; import java.util.List; import fragment.bwie.com.lianxi.R; import fragment.bwie.com.lianxi.bean.Bean; /** * Created by CZ on 2017/9/15. */ public class MyAdapter extends PagerAdapter { List<Bean.DataBean.EssayBean> image; Context context; Handler handler; public MyAdapter(List<Bean.DataBean.EssayBean> image, Context context, Handler handler) { this.image = image; this.context = context; this.handler = handler; ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context)); } @Override public int getCount() { return Integer.MAX_VALUE; } @Override public boolean isViewFromObject(View view, Object object) { return view == object; } @Override public Object instantiateItem(ViewGroup container, int position) { ImageView imageView = new ImageView(context); imageView.setScaleType(ImageView.ScaleType.FIT_XY);
//imageView.setImageResource(list.get(position));
ImageLoader.getInstance().displayImage(image.get(position % image.size()).getAuthor().get(0).getWeb_url(), imageView, getOption()); imageView.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent motionEvent) { boolean isTiaoZhuan = true; switch (motionEvent.getAction()) {//获取动作 case MotionEvent.ACTION_DOWN://按下 Log.i("--", "ACTION_DOWN"); handler.removeCallbacksAndMessages(null); break; case MotionEvent.ACTION_MOVE://移动 Log.i("--", "ACTION_MOVE"); isTiaoZhuan = false; handler.removeCallbacksAndMessages(null); break; case MotionEvent.ACTION_CANCEL://取消 Log.i("--", "ACTION_CANCEL"); isTiaoZhuan = false; handler.sendEmptyMessageDelayed(0, 2000); break; case MotionEvent.ACTION_UP://抬起 handler.sendEmptyMessageDelayed(0, 2000); break; } //自己处理触摸事件....如果当前位置返回了true,,,点击事件将不会执行,,,表示动作自己处理,不会传递 return true; } }); container.addView(imageView); return imageView; } private DisplayImageOptions getOption() { DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageOnLoading(R.mipmap.ic_launcher) //设置图片在下载期间显示的图片 .showImageForEmptyUri(R.mipmap.ic_launcher)//设置图的时片Uri为空或是错误候显示的图片 .showImageOnFail(R.mipmap.ic_launcher) //设置图片加载/解码过程中错误时候显示的图片 .cacheInMemory(true)//设置下载的图片是否缓存在内存中 .cacheOnDisk(true)//设置下载的图片是否缓存在SD卡中 .considerExifParams(true) //是否考虑JPEG图像EXIF参数(旋转,翻转) .imageScaleType(ImageScaleType.IN_SAMPLE_INT)//设置图片以如何的编码方式显示 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型 //.decodingOptions(BitmapFactory.Options decodingOptions)//设置图片的解码配置 .delayBeforeLoading(0)//int delayInMillis为你设置的下载前的延迟时间 //设置图片加入缓存前,对bitmap进行设置 //.preProcessor(BitmapProcessor preProcessor) .resetViewBeforeLoading(true)//设置图片在下载前是否重置,复位 .displayer(new RoundedBitmapDisplayer(20))//不推荐用!!!!是否设置为圆角,弧度为多少 .displayer(new FadeInBitmapDisplayer(100))//是否图片加载好后渐入的动画时间,可能会出现闪动 .build();//构建完成 return options; } @Override public void destroyItem(ViewGroup container, int position, Object object) { container.removeView((View) object); }}
----------------------------------------------gridview适配器图片优化要holder
package fragment.bwie.com.lianxi.adapter; import android.content.Context; import android.graphics.Bitmap; 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 com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer; import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer; import java.util.List; import fragment.bwie.com.lianxi.R; import fragment.bwie.com.lianxi.bean.Bean; /** * Created by CZ on 2017/9/15. */ public class GridAdapter extends BaseAdapter { List<Bean.DataBean.EssayBean> list; Context context; public GridAdapter(List<Bean.DataBean.EssayBean> list, Context context) { this.list = list; this.context = context; ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(context)); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return list.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(int i, View view, ViewGroup viewGroup) { ViewHolder holder; if (view == null) { view = View.inflate(context, R.layout.grid_item, null); holder = new ViewHolder(); holder.image = (ImageView) view.findViewById(R.id.image); holder.title = (TextView) view.findViewById(R.id.title); view.setTag(holder); } else { holder = (ViewHolder) view.getTag(); } holder.title.setText(list.get(i).getAuthor().get(0).getDesc()); ImageLoader.getInstance().displayImage(list.get(i).getAuthor().get(0).getWeb_url(), holder.image, getOption()); return view; } private DisplayImageOptions getOption() { DisplayImageOptions options = new DisplayImageOptions.Builder() .showImageOnLoading(R.mipmap.ic_launcher) //设置图片在下载期间显示的图片 .showImageForEmptyUri(R.mipmap.ic_launcher)//设置图的时片Uri为空或是错误候显示的图片 .showImageOnFail(R.mipmap.ic_launcher) //设置图片加载/解码过程中错误时候显示的图片 .cacheInMemory(true)//设置下载的图片是否缓存在内存中 .cacheOnDisk(true)//设置下载的图片是否缓存在SD卡中 .considerExifParams(true) //是否考虑JPEG图像EXIF参数(旋转,翻转) .imageScaleType(ImageScaleType.IN_SAMPLE_INT)//设置图片以如何的编码方式显示 .bitmapConfig(Bitmap.Config.RGB_565)//设置图片的解码类型 //.decodingOptions(BitmapFactory.Options decodingOptions)//设置图片的解码配置 .delayBeforeLoading(0)//int delayInMillis为你设置的下载前的延迟时间 //设置图片加入缓存前,对bitmap进行设置 //.preProcessor(BitmapProcessor preProcessor) .resetViewBeforeLoading(true)//设置图片在下载前是否重置,复位 .displayer(new RoundedBitmapDisplayer(20))//不推荐用!!!!是否设置为圆角,弧度为多少 .displayer(new FadeInBitmapDisplayer(100))//是否图片加载好后渐入的动画时间,可能会出现闪动 .build();//构建完成 return options; } public class ViewHolder { ImageView image; TextView title; } }
------------------------------------------------配置包
onstra直接输入