Viewpager根据图片数量动态增加引导点

来源:互联网 发布:linux ant使用 编辑:程序博客网 时间:2024/06/07 15:47

直接代码:

public class MyPagerView extends RelativeLayout implements ViewPager.OnPageChangeListener {    private LinearLayout layout;    private PhotoViewPager viewPager;    private PagerAdapter adapter;    private int width;    private List<ImageView> mList;    private Context context;    private LinearLayout.LayoutParams maxLp, minLp;    private OnPagerItemClickListener listener;    private Timer timer;    private TimerTask timerTask;    private boolean isLunBo;    public void setListener(OnPagerItemClickListener listener) {        this.listener = listener;    }    public MyPagerView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    public MyPagerView(Context context, AttributeSet attrs) {        super(context, attrs);        LayoutInflater.from(context).inflate(R.layout.myviewpager, this, true);        this.context = context;        layout = (LinearLayout) findViewById(R.id.linlayout);        viewPager = (PhotoViewPager) findViewById(R.id.myViewPager);        WindowManager menager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);        DisplayMetrics metrics = new DisplayMetrics();        menager.getDefaultDisplay().getMetrics(metrics);        //获得一个基准尺寸        width = metrics.widthPixels / 50;        mList = new ArrayList<>();        //初始化大点和小点的layoutparams        maxLp = new LinearLayout.LayoutParams(width, width);        minLp = new LinearLayout.LayoutParams(width*2/3, width*2/3);    }    //初始选择哪个页面    public void setCurrentItem(int position){        viewPager.setCurrentItem(position);    }    //设置viewpager的背景色    public  void setViewPagerBackground(int resource){        viewPager.setBackgroundResource(resource);    }    //给viewpager设置适配器    public void setAdapter(final List<View> list) {        adapter = new PagerAdapter() {            @Override            public int getCount() {                return list.size();            }            @Override            public boolean isViewFromObject(View view, Object object) {                return view == object;            }            @Override            public Object instantiateItem(ViewGroup container, int position) {                container.addView(list.get(position));                return list.get(position);            }            @Override            public void destroyItem(ViewGroup container, int position, Object object) {                container.removeView(list.get(position));            }        };        viewPager.setAdapter(adapter);        if (mList.size()>0){            layout.removeAllViews();        }        viewPager.setCurrentItem(0);        mList.clear();        for (int i = 0; i < list.size(); i++) {            //为viewpager的item设置点击监听,返回点击item的position            View mView = list.get(i);            final int finalI = i;            mView.setOnClickListener(new OnClickListener() {                @Override                public void onClick(View v) {                    if (listener != null) {                        listener.onClick(finalI);                    }                }            });            //设置viewpager的size个数的圆点            View view = View.inflate(context, R.layout.pointlayout, null);            LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.imageLayout);            ImageView imageView = (ImageView) view.findViewById(R.id.imageView);            ViewGroup.LayoutParams layoutParams = linearLayout.getLayoutParams();            layoutParams.height = width;            layoutParams.width = width;            linearLayout.setLayoutParams(layoutParams);            imageView.setLayoutParams(minLp);            layout.addView(view);            mList.add(imageView);        }        if (mList.size() > 0) {            mList.get(0).setLayoutParams(maxLp);        }        viewPager.setOnPageChangeListener(this);        viewPager.setOnTouchListener(new OnTouchListener() {            @Override            public boolean onTouch(View v, MotionEvent event) {                switch (event.getAction()) {                    case MotionEvent.ACTION_DOWN:                        if (isLunBo) {                            stopLunBo();                        }                        break;                    case MotionEvent.ACTION_UP:                        if (isLunBo) {                            startLunBo();                        }                        break;                }                return false;            }        });    }    //是否设置轮播功能    public void setLunBo(boolean isLunBo) {        this.isLunBo = isLunBo;        if (this.isLunBo) {            startLunBo();        } else {            stopLunBo();        }    }    //初始轮播速度    private int speed = 2000;    //设置轮播的速度    public void setSpeed(int speed) {        this.speed = speed;    }    //开始轮播,使用计时器定时发送mess    private void startLunBo() {        if (timer != null || timerTask != null) {            timerTask.cancel();            timerTask = null;            timer.purge();            timer.cancel();            timer = null;        }        timer = new Timer(true);        timerTask = new TimerTask() {            @Override            public void run() {                int currentItem = viewPager.getCurrentItem() + 1;                if (currentItem == viewPager.getAdapter().getCount()) {                    currentItem = 0;                }                handler.sendEmptyMessage(currentItem);            }        };        timer.schedule(timerTask, 3000, speed);    }    private void stopLunBo() {        if (timer != null || timerTask != null) {            timerTask.cancel();            timerTask = null;            timer.purge();            timer.cancel();            timer = null;        }    }    //使用handler吧接收到的message的what值设置为选中页    Handler handler = new Handler() {        @Override        public void handleMessage(Message msg) {            super.handleMessage(msg);            viewPager.setCurrentItem(msg.what);        }    };    @Override    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {    }    @Override    public void onPageSelected(int position) {        //遍历集合,设置选中position 上的图片圆点为大点其他为小点        for (ImageView imageView :                mList) {            imageView.setLayoutParams(minLp);        }        mList.get(position).setLayoutParams(maxLp);    }    @Override    public void onPageScrollStateChanged(int state) {    }    public interface OnPagerItemClickListener {        void onClick(int position);    }}

布局文件:

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent">    <com.example.administrator.mybianliapp.MyView.PhotoViewPager        android:id="@+id/myViewPager"        android:layout_width="match_parent"        android:layout_height="match_parent" />    <LinearLayout        android:id="@+id/linlayout"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:gravity="center"        android:layout_marginBottom="20px"        android:orientation="horizontal"/></RelativeLayout>
PointLayout如下:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent"><LinearLayout    android:layout_margin="3px"    android:id="@+id/imageLayout"    android:orientation="horizontal"    android:layout_width="match_parent"    android:gravity="center"    android:layout_height="match_parent">    <ImageView        android:id="@+id/imageView"        android:background="@drawable/point"        android:layout_width="wrap_content"        android:layout_height="wrap_content" /></LinearLayout></LinearLayout>




解决PhotoView与Viewpager组合的时候缩放报错问题

public class PhotoViewPager extends ViewPager {    private boolean isLocked;    public PhotoViewPager(Context context) {        super(context);        isLocked = false;    }    public PhotoViewPager(Context context, AttributeSet attrs) {        super(context, attrs);        isLocked = false;    }    @Override    public boolean onInterceptTouchEvent(MotionEvent ev) {        if (!isLocked) {            try {                return super.onInterceptTouchEvent(ev);            } catch (Exception e){                e.printStackTrace();                return false;            }        }        return false;    }    @Override    public boolean onTouchEvent(MotionEvent ev) {        return !isLocked&&super.onTouchEvent(ev);    }}

全屏查看图片的实现

public class PhotoViewPager {    public static void show(Context context, List<String> list, final Window window, int position){        View view=View.inflate(context, R.layout.myphotoviewpager,null);        MyPagerView pagerView= (MyPagerView) view.findViewById(R.id.pagerView);        final List<View> pagerList=new ArrayList<>();        if (list.size()>0){            for (int i = 0; i < list.size(); i++) {                View v=View.inflate(context,R.layout.wuyeimagepageritem,null);                PhotoView photoView= (PhotoView) v.findViewById(R.id.photoView);                Glide.with(context).load(list.get(i)).into(photoView);                pagerList.add(v);            }            pagerView.setAdapter(pagerList);            pagerView.setCurrentItem(position);        }        final WindowManager.LayoutParams attributes = window.getAttributes();        attributes.alpha=0.0f;        window.setAttributes(attributes);        PopupWindow popupWindow=new PopupWindow(view,                ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT,true);        popupWindow.setBackgroundDrawable(new BitmapDrawable());        popupWindow.setOutsideTouchable(false);        popupWindow.showAtLocation(view, Gravity.CENTER,0,0);        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {            @Override            public void onDismiss() {                attributes.alpha=1.0f;                window.setAttributes(attributes);            }        });    }}
布局文件:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical" android:layout_width="match_parent"    android:layout_height="match_parent"><com.example.administrator.mybianliapp.MyView.MyPagerView    android:id="@+id/pagerView"    android:layout_width="match_parent"    android:layout_height="match_parent"/></LinearLayout>
这里使用第三方jar包Glide去加载图片,可以加载本地的也可以加载网络图片,还有用到 PhotoView控件






1 0
原创粉丝点击