导入依赖库
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' }AndroidMainfest权限
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>布局
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent"> <com.daimajia.slider.library.SliderLayout android:id="@+id/slider" android:layout_width="match_parent" android:layout_height="200dp" /> </RelativeLayout>MainActivity代码
public class MainActivity extends AppCompatActivity { @BindView(R.id.slider) SliderLayout slider; @BindView(R.id.activity_main) RelativeLayout activityMain; private String[] mImages = {"http://a.hiphotos.baidu.com/image/pic/item/0bd162d9f2d3572ce98282e18e13632762d0c3af.jpg", "http://d.hiphotos.baidu.com/image/pic/item/1b4c510fd9f9d72aebede7a1d62a2834359bbb85.jpg", "http://h.hiphotos.baidu.com/image/pic/item/91ef76c6a7efce1be2f4f15cad51f3deb58f654c.jpg", "http://h.hiphotos.baidu.com/image/w%3D230/sign=3e9ec55457fbb2fb342b5f117f4b2043/e850352ac65c1038343303cbb0119313b07e896e.jpg", "http://e.hiphotos.baidu.com/image/pic/item/d53f8794a4c27d1e3625e52d18d5ad6edcc438dc.jpg"}; private String[] mTitles = {"1", "2", "3", "4", "5"}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); //添加图片控件 for (int i = 0; i < mImages.length; i++) { TextSliderView textSliderView = new TextSliderView(this);
//SliderView有两种DefaultSliderView,TextSliderView用法一致,但是DefaultSliderView没有显示文字的功能,指示器也没有背景
// DefaultSliderView defaultSliderView = new DefaultSliderView(this);
textSliderView.description(mTitles[i]);//设置标题 textSliderView.image(mImages[i]);//设置图片的网络地址// textSliderView.setScaleType(BaseSliderView.ScaleType.CenterCrop);//设置图片的缩放效果; //添加到布局中显示 slider.addSlider(textSliderView); } //设置指示器的位置 slider.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom); //设置图片的切换效果 slider.setPresetTransformer(SliderLayout.Transformer.Fade);// slider.setCustomAnimation(new DescriptionAnimation()); //添加textView动画特效 //设置切换时长2000 ,时长越小,切换速度越快 slider.setDuration(2000); } //性能优化。当页面显示时进行自动播放 @Override protected void onStart() { super.onStart(); slider.startAutoCycle(); }}