在AS中的使用:
1.在build文件中进行配置
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.权限
<!-- 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.布局:<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" />4.功能实现:
package cniao5.com.cniao5shop.fragment; import android.content.Intent; import android.content.SharedPreferences; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import com.daimajia.slider.library.Animations.DescriptionAnimation; import com.daimajia.slider.library.Indicators.PagerIndicator; import com.daimajia.slider.library.SliderLayout; import com.daimajia.slider.library.SliderTypes.BaseSliderView; import com.daimajia.slider.library.SliderTypes.TextSliderView; import com.daimajia.slider.library.Tricks.ViewPagerEx; import cniao5.com.cniao5shop.NextActivity; import cniao5.com.cniao5shop.R; /** * Created by Ivan on 15/9/25. */ public class HomeFragment extends Fragment { private SliderLayout mSliderLayout; private PagerIndicator indicator; @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_home,container,false); //容器 mSliderLayout = (SliderLayout) view.findViewById(R.id.slider); //指示器,那些小点 indicator= (PagerIndicator) view.findViewById(R.id.custom_indicator);initSlider(); return view; } private void initSlider(){ //SliderView有两种DefaultSliderView,TextSliderView用法一致,但是DefaultSliderView没有显示文字的功能 //显示图片和文字 TextSliderView textSliderView = new TextSliderView(this.getActivity()); textSliderView.image("http://m.360buyimg.com/mobilecms/s300x98_jfs/t2416/102/20949846/13425/a3027ebc/55e6d1b9Ne6fd6d8f.jpg"); textSliderView.description("新品推荐"); textSliderView.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() { @Override public void onSliderClick(BaseSliderView baseSliderView) { Toast.makeText(HomeFragment.this.getActivity(), "新品推荐", Toast.LENGTH_LONG).show(); } }); TextSliderView textSliderView2 = new TextSliderView(this.getActivity()); textSliderView2.image("http://m.360buyimg.com/mobilecms/s300x98_jfs/t1507/64/486775407/55927/d72d78cb/558d2fbaNb3c2f349.jpg"); textSliderView2.description("时尚男装"); textSliderView2.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() { @Override public void onSliderClick(BaseSliderView baseSliderView) { Toast.makeText(HomeFragment.this.getActivity(), "时尚男装", Toast.LENGTH_LONG).show(); } }); TextSliderView textSliderView3 = new TextSliderView(this.getActivity()); textSliderView3.image("http://m.360buyimg.com/mobilecms/s300x98_jfs/t1363/77/1381395719/60705/ce91ad5c/55dd271aN49efd216.jpg"); textSliderView3.description("家电秒杀"); textSliderView3.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() { @Override public void onSliderClick(BaseSliderView baseSliderView) { Toast.makeText(HomeFragment.this.getActivity(), "家电秒杀", Toast.LENGTH_LONG).show(); } }); //添加到容器中 mSliderLayout.addSlider(textSliderView); mSliderLayout.addSlider(textSliderView2); mSliderLayout.addSlider(textSliderView3); //使用默认的指示器 // mSliderLayout.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom); mSliderLayout.setCustomIndicator(indicator); //阴影显示的动画效果 mSliderLayout.setCustomAnimation(new DescriptionAnimation()); //图片的转场效果 // mSliderLayout.setPresetTransformer(SliderLayout.Transformer.RotateUp); mSliderLayout.setDuration(3000); mSliderLayout.addOnPageChangeListener(new ViewPagerEx.OnPageChangeListener() { @Override public void onPageScrolled(int i, float v, int i1) { Log.d(TAG,"onPageScrolled"); } @Override public void onPageSelected(int i) { Log.d(TAG,"onPageSelected"); } @Override public void onPageScrollStateChanged(int i) { Log.d(TAG,"onPageScrollStateChanged"); } }); } @Override public void onDestroy() { super.onDestroy(); mSliderLayout.stopAutoCycle(); } }