mainActivity.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"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="200dp"
- android:orientation="vertical"
- tools:context="com.example.jinmuyan.bannerzdy.MainActivity">
- <com.example.jinmuyan.bannerzdy.LoopPicture
- android:id="@+id/loopPicture"
- android:layout_width="match_parent"
- android:layout_height="250dp"/>
- </LinearLayout>
mainActivity代码
- public class MainActivity extends AppCompatActivity {
- private LoopPicture loopPicture;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- loopPicture = (LoopPicture)this.findViewById(R.id.loopPicture);
- String[] imagesRes = new String[] {
- "http://pic.sc.chinaz.com/files/pic/pic9/201604/apic20400.jpg",
- "http://pics.sc.chinaz.com/files/pic/pic9/201602/apic19022.jpg",
- "http://pics.sc.chinaz.com/files/pic/pic9/201603/fpic430.jpg",
- "http://pics.sc.chinaz.com/files/pic/pic9/201605/apic20631.jpg"};
- loopPicture.setImageUrl(imagesRes, true);
- /* int[] imagesRes = { R.drawable.a, R.drawable.b, R.drawable.c,
- R.drawable.d, R.drawable.e };
- loopPicture.setImageRes(imagesRes, true);*/
- /* String[] titles = {"111111111111111111","2222222222222222",
- "3333333333333","4444444444444","5555555555"};
- loopPicture.setImageTitle(titles);*/
- }
- }
loop_pictures.xml
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- <android.support.v4.view.ViewPager
- android:id="@+id/vp"
- android:layout_width="match_parent"
- android:layout_height="match_parent"/>
- <TextView
- android:id="@+id/tv_title"
- android:textSize="20sp"
- android:textColor="@android:color/white"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentBottom="true"
- android:padding="20dp"/>
- <LinearLayout
- android:id="@+id/ll_dot"
- android:orientation="horizontal"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_alignParentBottom="true"
- android:padding="8dp"/>
- </RelativeLayout>
LoopPicture代码
- public class LoopPicture extends FrameLayout {
- private Context context;
- private DisplayImageOptions options;
- private ImageLoader mImageLoader;
- private String[] imageTitle; //展示图片的标题
- private List<ImageView> images; //展示的图片
- private List<ImageView> imagesDots; //展示图片位置的小圆点
- private int delayTime; //轮播图延迟换图的时间
- private boolean isAutoPlay;
- private int currentItem;
- public Handler handler = new Handler();
- private ViewPager viewPager;
- private int imageLength;
- private LinearLayout dotLayout;
- private int titleLength;
- private TextView titleTV;
- private boolean hasTitle;
- public LoopPicture(Context context) {
- this(context, null);
- }
- public LoopPicture(Context context, AttributeSet attrs) {
- this(context, attrs, 0);
- }
- public LoopPicture(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- this.context = context;
- //使用第三方库universal-imageloader 加载图片缓存
- initImageLoader(context);
- //初始化数据
- initData();
- }
- /**
- * 使用第三方库universal-imageloader 加载图片缓存
- */
- private void initImageLoader(Context context) {
- ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
- context).threadPriority(Thread.NORM_PRIORITY - 2)
- .denyCacheImageMultipleSizesInMemory()
- .diskCacheFileNameGenerator(new Md5FileNameGenerator())
- .tasksProcessingOrder(QueueProcessingType.LIFO)
- .writeDebugLogs().build();
- ImageLoader.getInstance().init(config);
- mImageLoader = ImageLoader.getInstance();
- }
- /**
- * 初始化轮播器中的数据
- */
- private void initData() {
- images = new ArrayList<ImageView>();
- imagesDots = new ArrayList<ImageView>();
- delayTime = 2000;
- }
- /**
- * 提供接口:
- * 使用轮播器时设置图片(图片id)
- */
- public void setImageRes(int[] imagesRes, boolean hasTitle) {
- this.hasTitle = hasTitle;
- initView();
- initImgFromRes(imagesRes);
- startLoopPicture();
- }
- /**
- * 提供接口:
- * 使用轮播器时设置图片(图片URl)
- */
- public void setImageUrl(String[] imageUrl, boolean hasTitle){
- this.hasTitle = hasTitle;
- initView();
- initImgFromUrl(imageUrl);
- startLoopPicture();
- }
- /**
- * 提供接口:
- * 使用轮播器时设置图片的标题
- */
- public void setImageTitle(String[] imageTitle){
- this.imageTitle = imageTitle;
- }
- /**
- * 初始化轮播器布局
- */
- private void initView() {
- images.clear();
- View view = LayoutInflater.from(context).inflate(R.layout.loop_pictures, this, true);
- viewPager = (ViewPager)view.findViewById(R.id.vp);
- dotLayout = (LinearLayout) view.findViewById(R.id.ll_dot);
- titleTV = (TextView) view.findViewById(R.id.tv_title);
- dotLayout.removeAllViews();
- }
- /**
- * 初始化轮播器的图片(图片URL)
- * 使用第三方类库universal-imageloader 加载图片缓存
- */
- private void initImgFromUrl(String[] imageUrl) {
- imageLength = imageUrl.length;
- for(int i = 0;i<imageLength;i++){
- ImageView dotView = new ImageView(context);
- LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,
- LinearLayout.LayoutParams.WRAP_CONTENT);
- params.leftMargin = 5;
- params.rightMargin = 5;
- dotView.setImageResource(R.drawable.dot_blur);
- dotLayout.addView(dotView, params);
- imagesDots.add(dotView);
- }
- imagesDots.get(0).setImageResource(R.drawable.dot_focus);
- //universal-imageloader 加载图片缓存
- for(int i = 0; i <= imageLength + 1; i++){
- ImageView imageView = new ImageView(context);
- imageView.setScaleType(ImageView.ScaleType.FIT_XY);
- imageView.setBackgroundResource(R.drawable.loading);
- if(i == 0){
- mImageLoader.displayImage(imageUrl[imageLength - 1], imageView);
- } else if(i == imageLength + 1){
- mImageLoader.displayImage(imageUrl[0], imageView);
- } else {
- mImageLoader.displayImage(imageUrl[i - 1], imageView);
- }
- images.add(imageView);
- }
- }
- /**
- * 初始化轮播器的图片(图片id)
- */
- private void initImgFromRes(int[] imagesRes) {
- imageLength = imagesRes.length;
- for(int i = 0; i < imageLength; i++){
- ImageView dotView = new ImageView(context);
- LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,
- LinearLayout.LayoutParams.WRAP_CONTENT);
- params.leftMargin = 5;
- params.rightMargin = 5;
- dotView.setImageResource(R.drawable.dot_blur);
- dotLayout.addView(dotView, params);
- imagesDots.add(dotView);
- }
- //设置第一张轮播图的小圆点为选中状态
- imagesDots.get(0).setImageResource(R.drawable.dot_focus);
- for(int i = 0;i<= imageLength+1 ; i++){
- ImageView imageView = new ImageView(context);
- imageView.setScaleType(ImageView.ScaleType.FIT_XY);
- imageView.setBackgroundResource(R.drawable.loading);
- if(i == 0){
- imageView.setImageResource(imagesRes[imageLength - 1]);
- } else if (i == imageLength + 1){
- imageView.setImageResource(imagesRes[0]);
- } else {
- imageView.setImageResource(imagesRes[i - 1]);
- }
- images.add(imageView);
- }
- }
- /**
- * 设置ViewPager,开启轮播器
- */
- private void startLoopPicture(){
- viewPager.setAdapter(new LoopPicturesAdapter());
- viewPager.setFocusable(true);
- viewPager.setCurrentItem(1);
- currentItem = 1;
- viewPager.addOnPageChangeListener((ViewPager.OnPageChangeListener) new LoopPicturesListener());
- //开启轮播器
- isAutoPlay = true;
- handler.postDelayed(task, 2000);
- }
- private final Runnable task = new Runnable() {
- @Override
- public void run() {
- if (isAutoPlay) {
- currentItem = currentItem % (imageLength + 1) + 1;
- if (currentItem == 1) {
- viewPager.setCurrentItem(currentItem, false);
- handler.post(task);
- } else {
- viewPager.setCurrentItem(currentItem);
- handler.postDelayed(task, 3000);
- }
- } else {
- handler.postDelayed(task, 5000);
- }
- }
- };
- class LoopPicturesAdapter extends PagerAdapter {
- @Override
- public int getCount() {
- return images.size();
- }
- @Override
- public boolean isViewFromObject(View view, Object object) {
- return view == object;
- }
- @Override
- public Object instantiateItem(ViewGroup container, int position) {
- View view = images.get(position);
- container.addView(view);
- return view;
- }
- @Override
- public void destroyItem(ViewGroup container, int position, Object object) {
- container.removeView((View)object);
- }
- }
- class LoopPicturesListener implements ViewPager.OnPageChangeListener {
- @Override
- public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
- }
- @Override
- public void onPageSelected(int position) {
- for(int i = 0; i<imagesDots.size();i++){
- if(i == position - 1){
- imagesDots.get(i).setImageResource(R.drawable.dot_focus);
- if(hasTitle) {
- // titleTV.setText(imageTitle[i]);
- }
- } else {
- imagesDots.get(i).setImageResource(R.drawable.dot_blur);
- }
- }
- }
- @Override
- public void onPageScrollStateChanged(int state) {
- switch (state){
- case 1:
- isAutoPlay = false;
- break;
- case 2:
- isAutoPlay = true;
- break;
- case 0:
- if (viewPager.getCurrentItem() == 0) {
- viewPager.setCurrentItem(imageLength, false);
- } else if (viewPager.getCurrentItem() == imageLength + 1) {
- viewPager.setCurrentItem(1, false);
- }
- currentItem = viewPager.getCurrentItem();
- isAutoPlay = true;
- break;
- }
- }
- }
- }