binbinyang--介绍一个GITHUB上的轮播图-Android Image Slider(com.daimajia.slider.library.SliderLayout)

本文介绍了一个在GitHub上发现的Android轮播图控件的使用方法,包括配置依赖、添加权限、设置布局参数等步骤,并通过示例代码展示了如何实现带有自定义指示器的轮播图。


最近在GITHUB------- https://github.com/daimajia/AndroidImageSlider

 上看到了这个 轮播图控件

com.daimajia.slider.library.SliderLayout 

效果图如下 

幻灯片切换动画效果,可直接从网络或本地文件中获取图片。点击图片开始/暂停,滑动等待几秒后重新开始轮播。



 


首先来到GITHUB  看到下图





Step 1--在Gradle中加入包
dependencies {
        compile "com.android.support:support-v4:+"
        compile 'com.squareup.picasso:picasso:2.3.2'
        compile 'com.nineoldandroids:library:2.4.0'
        compile 'com.daimajia.slider:library:1.1.5@aar'
}

Step2--加入权限
<!-- if you want to load images from the internet -->
<uses-permission android:name="android.permission.INTERNET" /> 

<!-- if you want to load images from a file OR from the internet -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Step3--在布局中加入
<com.daimajia.slider.library.SliderLayout
        android:id="@+id/slider"
        android:layout_width="match_parent"
        android:layout_height="200dp"
/>

关键参数
关键参数设置: 
custom:pager_animation=”Accordion” 切换动画效果 
custom:auto_cycle=”true” 自动播放 
custom:indicator_visibility=”visible” 显示指示器

下面这个图是我自己写的DEMO的轮播图。。由于录制GIF软件问题 动画效果,录制出来的图片,动画效果感觉没出来 不过不碍事

直接上代码

由于我这个地方用到了这个轮播图带的自定义指示器 所以这里也要介绍一下

custom:shape=”oval” 或 rect 圆形,矩形

 <com.daimajia.slider.library.Indicators.PagerIndicator
        android:id="@+id/custom_indicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        custom:selected_color="#FF5500"
        custom:unselected_color="#55333333"
        custom:shape="rect"
        custom:selected_padding_left="2dp"
        custom:selected_padding_right="2dp"
        custom:unselected_padding_left="2dp"
        custom:unselected_padding_right="2dp"
        custom:selected_width="16dp"
        custom:selected_height="3dp"
        custom:unselected_width="16dp"
        custom:unselected_height="3dp"
        android:layout_gravity="center"
        />
以及  可用 style设置基本参数 
默认提供三种 
AndroidImageSlider_Corner_Oval_Orange,AndroidImageSlider_Attractive_Rect_Blue,AndroidImageSlider_Magnifier_Oval_Black
<style name="AndroidImageSlider_Corner_Oval_Orange">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:gravity">center</item>
        <item name="android:background">@drawable/indicator_corner_bg</item>
        <item name="android:paddingLeft">2dp</item>
        <item name="android:paddingRight">2dp</item>
        <item name="shape">oval</item>
        <item name="padding_left">3dp</item>
        <item name="padding_right">3dp</item>
        <item name="padding_top">4dp</item>
        <item name="padding_bottom">4dp</item>

        <item name="selected_color">#f60</item>
        <item name="unselected_color">#ffffff</item>
        <item name="selected_width">6dp</item>
        <item name="selected_height">6dp</item>
        <item name="unselected_width">6dp</item>
        <item name="unselected_height">6dp</item>
    </style>


我的布局文件

<?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:custom="http://schemas.android.com/apk/res-auto"
    android:background="#fff"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <com.daimajia.slider.library.SliderLayout
        android:id="@+id/slider"
        android:layout_width="match_parent"
        android:layout_height="200dp"
        />
    <com.daimajia.slider.library.Indicators.PagerIndicator
        android:id="@+id/custom_indicator"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        custom:selected_color="#FF5500"
        custom:unselected_color="#55333333"
        custom:shape="rect"
        custom:selected_padding_left="2dp"
        custom:selected_padding_right="2dp"
        custom:unselected_padding_left="2dp"
        custom:unselected_padding_right="2dp"
        custom:selected_width="16dp"
        custom:selected_height="3dp"
        custom:unselected_width="16dp"
        custom:unselected_height="3dp"
        android:layout_gravity="center"
        />
</LinearLayout>

我的代码

/**
 * 首页
 */
public class HomeFragment extends Fragment {
    SliderLayout sliderShow;
    View view;
    PagerIndicator indicator;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_home, container, false);
        sliderShow = (SliderLayout) view.findViewById(R.id.slider);
        indicator= (PagerIndicator)  view.findViewById(R.id.custom_indicator);
        initSlider();
        return view;
    }


    private void initSlider() {
        TextSliderView textSliderView1 = new TextSliderView(this.getActivity());
        textSliderView1
                .description("IT生活")
                .image("http://7mno4h.com2.z0.glb.qiniucdn.com/5608cae6Nbb1a39f9.jpg");


        textSliderView1.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
            @Override
            public void onSliderClick(BaseSliderView slider) {
                Toast.makeText(getActivity(),"轮播图的第一个图的点击效果",1).show();
            }
        });




        TextSliderView textSliderView2 = new TextSliderView(this.getActivity());
        textSliderView2
                .description("金秋风暴")
                .image("http://7mno4h.com2.z0.glb.qiniucdn.com/5608b7cdN218fb48f.jpg");



        TextSliderView textSliderView3 = new TextSliderView(this.getActivity());
        textSliderView3
                .description("手机会场")
                .image("http://7mno4h.com2.z0.glb.qiniucdn.com/5608eb8cN9b9a0a39.jpg");




        TextSliderView textSliderView4 = new TextSliderView(this.getActivity());
        textSliderView4
                .description("音像会场")
                .image("http://7mno4h.com2.z0.glb.qiniucdn.com/5608f3b5Nc8d90151.jpg");



        sliderShow.addSlider(textSliderView1);
        sliderShow.addSlider(textSliderView2);
        sliderShow.addSlider(textSliderView3);
        sliderShow.addSlider(textSliderView4);


//        sliderShow.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);/ 设置默认指示器位置(默认指示器白色)
        sliderShow.setCustomIndicator(indicator); // 设置自定义指示器(位置自定义)
        sliderShow.setCustomAnimation(new DescriptionAnimation());   // 设置TextView自定义动画
        sliderShow.setPresetTransformer(SliderLayout.Transformer.RotateDown);//设置默认过渡效果(可在xml中设置)的动画
        sliderShow.setDuration(3000);//设置时间 这个时间 就是延迟时间 第一个图跟第2个图3秒间隔



    }

其实他的指示器效果挺多的。。都在GITHUB上面有。。有兴趣的话可以自己去试试




现在的APP Banner大多数千篇一律,前几天看到魅族手机上所有魅族自家APP上的Banner效果不错,于是就想着来仿着做一个类似的效果。因此就有了这个库。但是为了使用方便,这个库不仅仅只有仿魅族效果的BannerView 来使用,还可以当作普通的BannerView 来使用,还可以当作一个ViewPager 来使用。使用很方便,具体使用方法和API 请看后面的示例。 ---  左为魅族APP上的Banner效果,右是高仿效果。MZBannerView 有以下功能:1 . 仿魅族BannerView 效果。2 . 当普通Banner 使用3 . 当普通ViewPager 使用。4 . 当普通ViewPager使用(有魅族Banner效果)5 . 仿某视频网站Banner效果。Demo APKgif片有点模糊,可以扫描下方二维码下载APK体验相关博客ViewPager系列之 仿魅族应用的广告BannerView更新日志v1.1.1 : 增加按住Banner 停止轮播,松开开始自轮播的功能v1.1.0 : fix 在从网上获取数据后,banner 显示 造成 ANR 的bug(如果在onCreate()中设置资源显示则没问题)v1.1.2 : fix 更改数据之后,调用setPages重新刷新数据会crush的bugv2.0.0 :1,add: 添加仿魅族Banner效果,中间Page覆盖两边。 -- 2,fix 部分bug: 添加OnPageChangeListener 回调 pisition 不对的bug.DependencyAdd it in your root build.gradle at the end of repositories:allprojects {      repositories {           ...           maven { url 'https://jitpack.io' }      } }Step 2. Add the dependencydependencies {          compile 'com.github.pinguo-zhouwei:MZBannerView:v2.0.0' }自定义属性属性名属性意义取值open_mz_mode是否开启魅族模式true 为魅族Banner效果,false 则普通Banner效果canLoop是否轮播true 轮播,false 则为普通ViewPagerindicatorPaddingLeft设置指示器距离左侧的距离单位为 dp 的值indicatorPaddingRight设置指示器距离右侧的距离单位为 dp 的值indicatorAlign设置指示器的位置有三个取值:left 左边,center 剧中显示,right 右侧显示middle_page_cover设置中间Page是否覆盖(真正的魅族Banner效果)true 覆盖,false 无覆盖效果使用方法1 . xml 布局文件2 . activity中代码:mMZBanner = (MZBannerView) view.findViewById(R.id.banner);              // 设置数据         mMZBanner.setPages(list, new MZHolderCreator() {             @Override             public BannerViewHolder createViewHolder() {                 return new BannerViewHolder();             }         });  public static class BannerViewHolder implements MZViewHolder {         private ImageView mImageView;         @Override         public View createView(Context cont
评论 4
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值