AndroidImageSlider quickly integrates cool automatic carousel ads in three minutes

Respect the original, please indicate: From zsml2016 ( http://blog.csdn.net/qq_29269233 ) Power byzsml2016 infringement must be investigated!

I. Introduction:

Due to various reasons, I didn't have time to write a blog, write a summary, and share it. Today, I will take the time to teach you how to quickly integrate automatic carousel ads! That's right! That's right, AndroidImageSlide, that's AndroidImageSlide, that's AndroidImageSlide!


2. A brief introduction to AndroidImageSlide:

AndroidImageSlide is a third-party open source framework that can realize the function of automatic advertisement rotation. It is very convenient to use, and it is very durable. The great gods know that you can also use ViewPager to achieve it, but it is more troublesome! AndroidImageSlide is much simpler than ViewPager, so you don't need to make wheels anymore!

3. AndroidImageSlide download address:

https://github.com/daimajia/AndroidImageSlider


Fourth, AndroidImageSlide quick integration:

1. Gradle add 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'

2. Add permissions such as networking in AndroidManifest:

<!-- 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" />

3. Add the xml layout file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="chaoandroid.zsml.com.androidimageslider.MainActivity">
    <com.daimajia.slider.library.SliderLayout
android:id="@+id/slider"
android                        
                :layout_width="match_parent"
        android:layout_height="200dp"
        />

</RelativeLayout>

4、activity代码实现:

public class MainActivity extends Activity {

    private SliderLayout sliderShow;

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

        sliderShow = (SliderLayout) findViewById(R.id.slider);

        imageSlider();


    }


    /**
     * AndroidImageslider
     */
    private void imageSlider() {

        TextSliderView textSliderView1 = new TextSliderView(this);
        textSliderView1
                .description("银河护卫队2")
                .image("http://i5qiniu.mtime.cn/mg/2016/12/04/143436.93575096.jpg");

        TextSliderView textSliderView2 = new TextSliderView(this);
        textSliderView2
                .description("记忆大师")
                .image("http://img5.mtime.cn/mg/2016/11/21/144656.57866103.jpg");

        TextSliderView textSliderView3 = new TextSliderView(this);
        textSliderView3
                .description("星际特工")
                .image("http://img5.mtime.cn/mg/2016/11/11/143347.84705153.jpg");

        TextSliderView textSliderView4 = new TextSliderView(this);
        textSliderView4
                .description("《长城》全新预告片")
                .image("http://img5.mtime.cn/mg/2016/10/09/112424.73820873.jpg");



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

        //图片点击监听
        textSliderView1.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
            @Override
            public void onSliderClick(BaseSliderView slider) {
                Toast.makeText(MainActivity.this,"电影:银河护卫队2",Toast.LENGTH_SHORT).show();
            }
        });

        textSliderView2.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
            @Override
            public void onSliderClick(BaseSliderView slider) {
                Toast.makeText(MainActivity.this,"电影:记忆大师",Toast.LENGTH_SHORT).show();
            }
        });

        textSliderView3.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
            @Override
            public void onSliderClick(BaseSliderView slider) {
                Toast.makeText(MainActivity.this,"电影:星际特工",Toast.LENGTH_SHORT).show();
            }
        });

        textSliderView4.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
            @Override
            public void onSliderClick(BaseSliderView slider) {
                Toast.makeText(MainActivity.this,"电影:长城",Toast.LENGTH_SHORT).show();
            }
        });



        //其他设置
        sliderShow.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);//使用默认指示器,在底部显示
        sliderShow.setDuration(2000);//停留时间

                //设置AndroidImageslider监听
                sliderShow.addOnPageChangeListener(new ViewPagerEx.OnPageChangeListener() {
                    @Override
                    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                    }

                    @Override
                    public void onPageSelected(int position) {

                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {

                    }
                });

    }
}

代码里我已经加上注释了,其中广告图片是网上获取的,当然也可以改为访问本地资源文件的,这样就不用网上获取,看你的需求吧!到这里就成功快速地集成广告自动轮播了,我设置了2秒,哈哈哈!先看看效果吧:



到这里已经可以满足大多数的需求了,而且还添加了广告的点击监听,轮播监听!

其中在这里我用了默认的指示器显示在底部,其实还可以使用自定义的指示器,大家可以自己去查看官方文档,我就不演示了,





更多精彩内容请关注:luoweichao.top




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325563591&siteId=291194637