Android中ConvenientBanner的使用--获取网络图片 --(实现效果是自动轮播图片)

一.首先添加依赖

compile'com.bigkoo:convenientbanner:2.0.5'


                           compile'com.github.bumptech.glide:glide:3.6.0'

               



二.首先获取本地图片 

1.在MainActivity中:

public class MainActivity extends AppCompatActivity {
    private String[] images = {
            "http://img2.3lian.com/2014/f2/37/d/40.jpg",

            "http://img2.3lian.com/2014/f2/37/d/39.jpg",
            "http://www.8kmm.com/UploadFiles/2012/8/201208140920132659.jpg",
            "http://f.hiphotos.baidu.com/image/h%3D200/sign=1478eb74d5a20cf45990f9df460b4b0c/d058ccbf6c81800a5422e5fdb43533fa838b4779.jpg",
            "http://f.hiphotos.baidu.com/image/pic/item/09fa513d269759ee50f1971ab6fb43166c22dfba.jpg"
    };
    //轮播下面的小点(小圆点是本地的,自己导入的图片)
    private int[] indicator = {R.drawable.b1, R.drawable.b2};
    private ConvenientBanner convenientBanner;
    //图片加载地址的集合
    private List<String> bean;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        convenientBanner = (ConvenientBanner) findViewById(R.id.convenientBanner);

        bean = Arrays.asList(images);
        convenientBanner.setPointViewVisible(true)
                //设置小点
                .setPageIndicator(indicator);
        //允许手动轮播
        convenientBanner.setManualPageable(true);
        //设置自动轮播的时间
        convenientBanner.startTurning(2000);
        //设置点击事件
        //泛型为具体实现类ImageLoaderHolder
        convenientBanner.setPages(new CBViewHolderCreator<NetImageLoadHolder>() {
            @Override
            public NetImageLoadHolder createHolder() {
                return new NetImageLoadHolder();
            }
        }, bean);

        //设置每个pager的点击事件
        convenientBanner.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(MainActivity.this, "点击了" + convenientBanner.getCurrentItem(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    public class NetImageLoadHolder implements Holder<String> {
        private ImageView image_lv;

        //可以是一个布局也可以是一个Imageview
        @Override
        public ImageView createView(Context context) {
            image_lv = new ImageView(context);
            image_lv.setScaleType(ImageView.ScaleType.FIT_XY);

            return image_lv;

        }

        @Override
        public void UpdateUI(Context context, int position, String data) {
            //Glide框架
            Glide.with(context).load(data).into(image_lv);

        }

    }
}

2.MainActivity的布局文件:

<com.bigkoo.convenientbanner.ConvenientBanner
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/convenientBanner"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:canLoop="true"
/>


需要在xml清单文件里面配置网络权限:
<uses-permission android:name="android.permission.INTERNET"/>
=================================================================================
解析json串获得图片链接的代码

public class MainActivity extends AppCompatActivity {
    //轮播下面的小点(小圆点是本地的,自己导入的图片)
    private int[] indicator = {R.drawable.b1, R.drawable.b2};
    private ConvenientBanner convenientBanner;
    private List<String>  list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Jiexi();

        findViewById(R.id.tiao).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intent);

            }
        });


    }

    private void add() {
        convenientBanner = (ConvenientBanner) findViewById(R.id.convenientBanner);

        // bean = Arrays.asList(images);
        convenientBanner.setPointViewVisible(true)
                //设置小点
                .setPageIndicator(indicator);
        //允许手动轮播
        convenientBanner.setManualPageable(true);
        //设置自动轮播的时间
        convenientBanner.startTurning(2000);
        //设置点击事件
        //泛型为具体实现类ImageLoaderHolder
        convenientBanner.setPages(new CBViewHolderCreator<NetImageLoadHolder>() {
            @Override
            public NetImageLoadHolder createHolder() {
                return new NetImageLoadHolder();
            }
        }, list);

        //设置每个pager的点击事件
        convenientBanner.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                Toast.makeText(MainActivity.this, "点击了" + convenientBanner.getCurrentItem(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    private void Jiexi() {
        new AsyncTask<String, Integer, String>(){

            //一般基本最少写这俩种方法

            //doInBackground 方法
            @Override
            protected String doInBackground(String... params) {
//打开连接

                String str="";
                try {
                    URL url= new URL("http://apis.juhe.cn/cook/query?key=dd452181a9ca8ee5710d153538a4b8d6&menu=%E8%A5%BF%E7%BA%A2%E6%9F%BF&rn=10&pn=1");

                    HttpURLConnection conne=(HttpURLConnection) url.openConnection();
                    //设置时间

                    Log.d("zzz", "走没走!!!!");
                    conne.setReadTimeout(5000);
                    conne.setConnectTimeout(5000);
                    int responseCode = conne.getResponseCode();
                    //判断
                    if (responseCode==200) {

                        InputStream in = conne.getInputStream();
                        byte[] by=new byte[1024];
                        int len=0;

                        while ((len=in.read(by))!=-1) {

                            str+=new String(by,0,len);

                        }

                    }
                } catch (MalformedURLException e) {
// TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
// TODO Auto-generated catch block
                    e.printStackTrace();
                }


                return str;
            }

            //onPostExecute方法
            protected void onPostExecute(String result) {
                Log.d("zzz", result);
                Gson gson = new Gson();
                Prioer p = gson.fromJson(result, Prioer.class);
                List<Prioer.ResultBean.DataBean.StepsBean> data = p.getResult().getData().get(0).getSteps();


                //图片加载地址的集合
             list=new ArrayList<String>();
                for(int i=0;i<data.size();i++){

                    String img = data.get(i).getImg();

                    list.add(img);

                }
                add();
                super.onPostExecute(result);
            };

//这一步必须要写execute()
        }.execute();


    }

    public class NetImageLoadHolder implements Holder<String> {
        private ImageView image_lv;

        //可以是一个布局也可以是一个Imageview
        @Override
        public ImageView createView(Context context) {
            image_lv = new ImageView(context);
            image_lv.setScaleType(ImageView.ScaleType.FIT_XY);

            return image_lv;

        }

        @Override
        public void UpdateUI(Context context, int position, String data) {
            //Glide框架
            Glide.with(context).load(data).into(image_lv);

        }

    }
}



阅读更多
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40090482/article/details/77987392
文章标签: ConvenientBanner 网络
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭