zoukankan      html  css  js  c++  java
  • 图片轮播

    https://github.com/youth5201314/banner

    
    
    compile 'com.youth.banner:banner:+'
    
    
    compile "com.github.bumptech.glide:glide:3.7.0"

    <?xml version="1.0" encoding="utf-8"?>
    <com.youth.banner.Banner xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/banner"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

    public class GlideImageLoader extends ImageLoader {
    @Override
    public void displayImage(Context context, Object path, ImageView imageView) {
    //具体方法内容自己去选择,次方法是为了减少banner过多的依赖第三方包,所以将这个权限开放给使用者去选择
    Glide.with(context.getApplicationContext())
    .load(path)
    .crossFade()
    .into(imageView);

    }


    }
    final View header = LayoutInflater.from(getActivity()).inflate(R.layout.header, (ViewGroup) getActivity().findViewById(android.R.id.content), false);
    mLRecyclerViewAdapter.addHeaderView(header);
    banner = (Banner) header.findViewById(R.id.banner);
    DisplayMetrics dm = getActivity().getResources().getDisplayMetrics();
    H = dm.widthPixels;
    banner.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, H / 3));
    banner.setBannerAnimation(AccordionTransformer.class);

    //简单使用
    banner.setImages(imgesUrl)
    .setImageLoader(new GlideImageLoader())
    .setOnBannerListener(new OnBannerListener() {
    @Override
    public void OnBannerClick(int position) {
    ToastUtil.makeToast("" + position);
    }
    })
    .start();
    lvLiveRoom.addHeaderView(banner);
     
    //更新图片数据
    banner.update(imgesUrl);
    //图片轮播开始
    banner.startAutoPlay();
    //轮播停止
    banner.stopAutoPlay();

    //界面显示

        @Override
       public void onStart() {
         super.onStart();
         banner.startAutoPlay();
        }

    //界面不显示
    @Override
    public void onStop() {
    super.onStop();
    banner.stopAutoPlay();
    }
    
    
    
    
    
    
    
    
    
    

    1.添加布局fragment_hall_head.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/transparent"
    android:orientation="vertical">
    <FrameLayout
    android:layout_width="match_parent"
    android:layout_height="300px">

    <android.support.v4.view.ViewPager
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="300px" />

    <LinearLayout
    android:id="@+id/ll_points"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|center_horizontal"
    android:layout_marginBottom="11dp"
    android:orientation="horizontal" />

    </FrameLayout>
    </LinearLayout>


    2.添加资源文件
    shape_xiao_yuan_quan_selected.xml
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <size
    android:width="8dp"
    android:height="8dp" />
    <stroke
    android:width="1px"
    android:color="@color/colorAccent" />
    <solid android:color="@color/colorAccent" />
    </shape>

    shape_xiao_yuan_quan_nomal.xml
    <?xml version="1.0" encoding="utf-8"?>
    <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval">
    <size
    android:width="8dp"
    android:height="8dp" />
    <stroke
    android:width="1px"
    android:color="@color/white" />
    <solid android:color="@color/white" />
    </shape>
    3.代码:
    初始化数据:
    View bannerHead;
    private LinearLayout llPoints; //图片里面的小圆点
    private ViewPager viewPager; //图片轮播
    List<String> imgesUrl;
    初始化图片:
     
    bannerHead = View.inflate(mContext, R.layout.fragment_hall_head, null);
    viewPager = (ViewPager) bannerHead.findViewById(R.id.viewPager);
    llPoints = (LinearLayout) bannerHead.findViewById(R.id.ll_points);
    imgesUrl = new ArrayList<>();
    imgesUrl.add("http://img3.fengniao.com/forum/attachpics/913/114/36502745.jpg");
    imgesUrl.add("http://imageprocess.yitos.net/images/public/20160910/99381473502384338.jpg");
    imgesUrl.add("http://imageprocess.yitos.net/images/public/20160910/77991473496077677.jpg");
    imgesUrl.add("http://imageprocess.yitos.net/images/public/20160906/1291473163104906.jpg");
    dealLunBo();
    初始化進行圖片的處理:
    /**
    * 处理轮播图
    */

    private void dealLunBo() {
    MyPagerAdapter myPagerAdapter = new MyPagerAdapter();
    viewPager.setAdapter(myPagerAdapter);
    viewPager.setCurrentItem(imgesUrl.size() * 10000);
    startRool();
    //初始化ViewPager轮播小圆圈
    llPoints.removeAllViews();
    for (int i = 0; i < imgesUrl.size(); i++) {
    ImageView point = new ImageView(getActivity());
    if (i == 0) {
    point.setImageResource(R.drawable.shape_xiao_yuan_quan_selected);
    } else {
    point.setImageResource(R.drawable.shape_xiao_yuan_quan_nomal);
    }
    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    if (i > 0) {
    params.leftMargin = UiUtils.dp2px(getContext(), 8);
    }
    llPoints.addView(point, params);
    }
    //ViewPager的轮播效果(小圆圈改变)
    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    @Override
    public void onPageSelected(int position) {
    position = position % imgesUrl.size();
    for (int i = 0; i < llPoints.getChildCount(); i++) {
    ImageView image = (ImageView) llPoints.getChildAt(i);
    if (i == position) {
    image.setImageResource(R.drawable.shape_xiao_yuan_quan_selected);
    } else {
    image.setImageResource(R.drawable.shape_xiao_yuan_quan_nomal);
    }
    }
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
    });

    }

    /**
    * 轮播图的adapter
    */

    class MyPagerAdapter extends PagerAdapter {
    @Override
    public int getCount() {
    return Integer.MAX_VALUE;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
    return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
    container.removeView((View) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
    ImageView imageView = new ImageView(getActivity());
    imageView.setScaleType(ImageView.ScaleType.FIT_XY);
    ImageLoader.getInstance().displayImage(imgesUrl.get(position % imgesUrl.size()), imageView);
    imageView.setOnTouchListener(new View.OnTouchListener() {

    public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
    handler.removeCallbacksAndMessages(null);
    break;
    case MotionEvent.ACTION_UP:
    handler.sendEmptyMessageDelayed(1, 2000);
    break;
    /**
    * 当保持按下操作,并从此控件移到外层控件时,就会触发ACTION_CANCEL事件时,
    * 当前的手势被中断,不会在接收它的记录
    * 将它当做ACTION_UP事件进行处理比较好
    */
    case MotionEvent.ACTION_CANCEL:
    handler.sendEmptyMessageDelayed(1, 2000);
    break;

    default:
    break;
    }
    // true处理这个事件
    return true;
    }
    });

    container.addView(imageView);
    return imageView;
    }
    }

    Handler handler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
    if (msg.what == 1) {
    int currentItem = viewPager.getCurrentItem();
    currentItem++;
    viewPager.setCurrentItem(currentItem);
    startRool();
    }
    }

    };

    private void startRool() {
    handler.sendEmptyMessageDelayed(1, 3000);
    }











  • 相关阅读:
    Visual Studio DSL 入门 11为状态机设计器添加规则
    不平静的2009,期待更不平静的2010
    ASP.NET MVC 2 正式发布
    [翻译] DSL和模型驱动开发的最佳实践(2/4)
    Visual Studio DSL 入门 9创建状态机的图形符号
    Visual Studio DSL 入门 6DSL的图形表示1
    智诚B2C1.31正式发
    一个程序员的创业尝试
    Visual Studio DSL 入门 13结合T4生成代码
    Visual Studio DSL 入门 10完善状态机案例
  • 原文地址:https://www.cnblogs.com/huihuizhang/p/6244839.html
Copyright © 2011-2022 走看看