解析接口中的图片,并且轮播

控件中:

xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.com.lunbotu.MainActivity">

   <android.support.v4.view.ViewPager
       android:layout_width="match_parent"
       android:layout_height="200dp"
       android:id="@+id/vp">android.support.v4.view.ViewPager>

RelativeLayout>
MainActivity:
package com.example.com.lunbotu;

import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.example.com.lunbotu.Bean.ReqBean;
import com.example.com.lunbotu.Util.Mytask;
import com.example.com.lunbotu.Util.NetStateUtil;
import com.example.com.lunbotu.adapter.MyAdapter;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private ViewPager vp;
    private ArrayList list;
    private DisplayImageOptions options;

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

        //使用内存缓存
//使用磁盘缓存
//设置正在下载的图片
//url为空或请求的资源不存在时
//下载失败时显示的图片
//设置图片色彩模式 1px=2字节
//设置图片的缩放模式
//设置圆角 30代表半径 自定义
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)//使用内存缓存
                .cacheOnDisk(true)//使用磁盘缓存
                .showImageOnLoading(R.mipmap.ic_launcher)//设置正在下载的图片
                .showImageForEmptyUri(R.mipmap.ic_launcher)//url为空或请求的资源不存在时
                .showImageOnFail(R.mipmap.ic_launcher)//下载失败时显示的图片
                .bitmapConfig(Bitmap.Config.RGB_565)//设置图片色彩模式 1px=2字节
                .imageScaleType(ImageScaleType.EXACTLY)//设置图片的缩放模式
                //.displayer(new RoundedBitmapDisplayer(100))//设置圆角 30代表半径 自定义
                .build();

        //判断是否联网
if (NetStateUtil.isConn(this)){

    Mytask mytask = new Mytask(new Mytask.Icallbacks() {

        @Override
        public void updateUiByjson(String jsonstr) {
            Gson gson = new Gson();
            ReqBean reqBean = gson.fromJson(jsonstr, ReqBean.class);
            List newslist = reqBean.getNewslist();

            list = new ArrayList<>();
            for(int i=0;ilist.add(title);
            }
            vp.setCurrentItem(5*10);//设置默认显示的页面
            vp.setAdapter(new Myvpadapter());
            //MyAdapter myAdapter = new MyAdapter(MainActivity.this, newslist);

        }
    });
    mytask.execute("http://api.tianapi.com/social/?key=48a7d7193e11bd2dd4a683b6e2f90a4f&num=10");
}else {
    NetStateUtil.showNoNetWorkDlg(this);
}

    }


    class Myvpadapter extends PagerAdapter{

        @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 img   =  new ImageView(MainActivity.this);
            //Log.d("--", "instantiateItem: "+list.get(position));
            //Log.d("--", "instantiateItem: "+position);
            //ImageLoader.getInstance().displayImage(list.get(position%list.size()),img,options);
            ImageLoader.getInstance().displayImage(list.get(position%list.size()),img,options);
            container.addView(img);
            return img;
        }
//删除的方法
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {


            container.removeView((View) object);
        }
    }
}
注:要导入gson包和ImageLoader包,,然后导入Mapplicition文件,,还要导入工具类和NetStateUtil.java
然后xml配置文件为:
 
  
<uses-permission android:name="android.permission.INTERNET">uses-permission>
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">uses-permission>
适配器为:
 
  
package com.example.com.lunbotu.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.example.com.lunbotu.Bean.ReqBean;

import com.example.com.lunbotu.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.List;

/**
 * Created by 联想 on 2018/1/15.
 */

public class MyAdapter extends BaseAdapter {
    Context context;
    List newslist;
    private DisplayImageOptions options;
    public MyAdapter(Context context, List newslist) {
        this.context = context;
        this.newslist = newslist;


        this.context = context;
        this.newslist = newslist;
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)//使用内存缓存
                .cacheOnDisk(true)//使用磁盘缓存
                .showImageOnLoading(R.mipmap.ic_launcher)//设置正在下载的图片
                .showImageForEmptyUri(R.mipmap.ic_launcher)//url为空或请求的资源不存在时
                .showImageOnFail(R.mipmap.ic_launcher)//下载失败时显示的图片
                .bitmapConfig(Bitmap.Config.RGB_565)//设置图片色彩模式 1px=2字节
                .imageScaleType(ImageScaleType.EXACTLY)//设置图片的缩放模式
                .displayer(new RoundedBitmapDisplayer(100))//设置圆角 30代表半径 自定义
                .build();

    }

    @Override
    public int getCount() {
        return newslist.size();
    }

    @Override
    public Object getItem(int i) {
        return newslist.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.view,null);
            view=View.inflate(context,R.layout.view,null );
            holder=new ViewHolder();
            holder.tw=view.findViewById(R.id.tw);
            holder.iv=view.findViewById(R.id.iv);
            view.setTag(holder);
        }else{
            holder= (ViewHolder) view.getTag();
        }
        holder.tw.setText(newslist.get(i).getTitle());
        ImageLoader.getInstance().displayImage(newslist.get(i).getPicUrl(),holder.iv,options);
        return view;
    }

    private class ViewHolder{

        TextView tw;
        ImageView iv;
    }
}
 
  
 
  
 
  
 
  

你可能感兴趣的:(代码)