childIndex = new SparseArray<>();
23 | /**
24 | * 能否滚动
25 | */
26 | private boolean mScrollable = true;
27 | /**
28 | * 能否仿魅族效果
29 | */
30 | private boolean mEnableMzEffects = false;
31 |
32 | public BannerViewPager(Context context) {
33 | super(context);
34 | }
35 |
36 | public BannerViewPager(Context context, AttributeSet attrs) {
37 | super(context, attrs);
38 | }
39 |
40 | @Override
41 | public boolean onTouchEvent(MotionEvent ev) {
42 | if (mScrollable)
43 | return super.onTouchEvent(ev);
44 | else
45 | return false;
46 | }
47 |
48 | @Override
49 | public boolean onInterceptTouchEvent(MotionEvent ev) {
50 | if (mScrollable)
51 | return super.onInterceptTouchEvent(ev);
52 | else
53 | return false;
54 |
55 | }
56 |
57 | /**
58 | * @param childCount
59 | * @param n
60 | * @return 第n个位置的child 的绘制索引
61 | *
62 | * 重写这个方法来控制字View的绘制顺序
63 | * 让中间View压着左右两边View,出现仿魅族效果
64 | */
65 | @Override
66 | protected int getChildDrawingOrder(int childCount, int n) {
67 | if (mEnableMzEffects) {
68 | if (n == 0 || childIndex.size() != childCount) {
69 | childCenterXAbs.clear();
70 | childIndex.clear();
71 | int viewCenterX = getViewCenterX(this);
72 | for (int i = 0; i < childCount; ++i) {
73 | int indexAbs = Math.abs(viewCenterX - getViewCenterX(getChildAt(i)));
74 | //两个距离相同,后来的那个做自增,从而保持abs不同
75 | if (childIndex.get(indexAbs) != null) {
76 | ++indexAbs;
77 | }
78 | childCenterXAbs.add(indexAbs);
79 | childIndex.append(indexAbs, i);
80 | }
81 | //1,0,2 0,1,2
82 | Collections.sort(childCenterXAbs);
83 | }
84 | //那个item距离中心点远一些,就先draw它。(最近的就是中间放大的item,最后draw)
85 | return childIndex.get(childCenterXAbs.get(childCount - 1 - n));
86 | } else {
87 | return super.getChildDrawingOrder(childCount, n);
88 | }
89 | }
90 |
91 | private int getViewCenterX(View view) {
92 | int[] array = new int[2];
93 | view.getLocationOnScreen(array);
94 | return array[0] + view.getWidth() / 2;
95 | }
96 |
97 | /**
98 | * 设置能否滚动
99 | *
100 | * @param scrollable
101 | */
102 | public void setScrollable(boolean scrollable) {
103 | mScrollable = scrollable;
104 | }
105 |
106 | /**
107 | * 仿魅族Banner
108 | * 控制绘制顺序,让中间View覆盖左右两View
109 | *
110 | * @param enableMzEffects
111 | */
112 | public void setEnableMzEffects(boolean enableMzEffects) {
113 | mEnableMzEffects = enableMzEffects;
114 | }
115 | }
116 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/banner/BannerMzActivity.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.banner;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.util.Log;
8 | import android.view.View;
9 | import android.widget.ImageView;
10 | import android.widget.Toast;
11 |
12 | import com.bumptech.glide.Glide;
13 | import com.bumptech.glide.load.engine.DiskCacheStrategy;
14 | import com.bumptech.glide.request.RequestOptions;
15 | import com.geek.banner.Banner;
16 | import com.geek.banner.loader.BannerEntry;
17 | import com.geek.banner.loader.ImageLoader;
18 | import com.test.banner.R;
19 | import com.test.banner.bean.BannerItem;
20 |
21 | import java.util.ArrayList;
22 | import java.util.List;
23 |
24 | import butterknife.BindView;
25 | import butterknife.ButterKnife;
26 |
27 | /**
28 | * @Author: HSL
29 | * @Time: 2019/8/23 11:54
30 | * @E-mail: xxx@163.com
31 | * @Description: 仿魅族Banner~
32 | */
33 | public class BannerMzActivity extends AppCompatActivity {
34 |
35 | @BindView(R.id.banner_mz)
36 | Banner bannerMz;
37 |
38 | private List mData = new ArrayList<>();
39 |
40 | public static void start(Context context) {
41 | Intent starter = new Intent(context, BannerMzActivity.class);
42 | context.startActivity(starter);
43 | }
44 |
45 | @Override
46 | protected void onCreate(Bundle savedInstanceState) {
47 | super.onCreate(savedInstanceState);
48 | setContentView(R.layout.activity_banner_mz);
49 | ButterKnife.bind(this);
50 | initData();
51 | initBannerMZ();
52 | }
53 |
54 | @Override
55 | protected void onResume() {
56 | super.onResume();
57 | bannerMz.startAutoPlay();
58 | }
59 |
60 | @Override
61 | protected void onPause() {
62 | super.onPause();
63 | bannerMz.stopAutoPlay();
64 | }
65 |
66 | private void initData() {
67 | mData.add(new BannerItem(R.drawable.banner_1, ""));
68 | mData.add(new BannerItem(R.drawable.banner_2, ""));
69 | mData.add(new BannerItem(R.drawable.banner_3, ""));
70 | mData.add(new BannerItem(R.drawable.banner_4, ""));
71 | mData.add(new BannerItem(R.drawable.banner_5, ""));
72 | }
73 |
74 | private void initBannerMZ() {
75 | // 1. 创建设置BannerLoader
76 | bannerMz.setBannerLoader(new ImageLoader() {
77 | @Override
78 | public void loadView(Context context, BannerEntry entry, int position, View imageView) {
79 | RequestOptions requestOptions = new RequestOptions()
80 | .placeholder(R.drawable.banner_default)
81 | .error(R.drawable.banner_default)
82 | .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
83 | Glide.with(context).load(entry.getBannerPath()).apply(requestOptions).into((ImageView) imageView);
84 | }
85 | });
86 |
87 | // 2. 设置页面点击事件
88 | bannerMz.setOnBannerClickListener(new Banner.OnBannerClickListener() {
89 | @Override
90 | public void onBannerClick(int position) {
91 | Toast.makeText(BannerMzActivity.this, "点击了:" + position, Toast.LENGTH_SHORT).show();
92 | }
93 | });
94 |
95 | // 3. 翻页事件
96 | bannerMz.setBannerPagerChangedListener(new Banner.OnBannerSimplePagerListener() {
97 | @Override
98 | public void onPageSelected(int position) {
99 | Log.d("hsl777", "onPageSelected: ==>" + position);
100 | }
101 | });
102 |
103 | // 4. 最重要一步,加载数据
104 | bannerMz.loadImagePaths(mData);
105 | }
106 | }
107 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_banner_indicator.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
22 |
23 |
35 |
36 |
47 |
48 |
49 |
64 |
65 |
78 |
79 |
91 |
92 |
93 |
103 |
104 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/geek/banner/transformer/simple/ABaseTransformer.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright 2014 Toxic Bakery
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.geek.banner.transformer.simple;
18 |
19 | import android.support.v4.view.ViewPager.PageTransformer;
20 | import android.view.View;
21 |
22 | public abstract class ABaseTransformer implements PageTransformer {
23 |
24 | /**
25 | * Called each {@link #transformPage(View, float)}.
26 | *
27 | * @param page
28 | * Apply the transformation to this page
29 | * @param position
30 | * Position of page relative to the current front-and-center position of the pager. 0 is front and
31 | * center. 1 is one full page position to the right, and -1 is one page position to the left.
32 | */
33 | protected abstract void onTransform(View page, float position);
34 |
35 | /**
36 | * Apply a property transformation to the given page. For most use cases, this method should not be overridden.
37 | * Instead use {@link #transformPage(View, float)} to perform typical transformations.
38 | *
39 | * @param page
40 | * Apply the transformation to this page
41 | * @param position
42 | * Position of page relative to the current front-and-center position of the pager. 0 is front and
43 | * center. 1 is one full page position to the right, and -1 is one page position to the left.
44 | */
45 | @Override
46 | public void transformPage(View page, float position) {
47 | onPreTransform(page, position);
48 | onTransform(page, position);
49 | onPostTransform(page, position);
50 | }
51 |
52 | /**
53 | * If the position offset of a fragment is less than negative one or greater than one, returning true will set the
54 | * fragment alpha to 0f. Otherwise fragment alpha is always defaulted to 1f.
55 | *
56 | * @return
57 | */
58 | protected boolean hideOffscreenPages() {
59 | return true;
60 | }
61 |
62 | /**
63 | * Indicates if the default animations of the view pager should be used.
64 | *
65 | * @return
66 | */
67 | protected boolean isPagingEnabled() {
68 | return false;
69 | }
70 |
71 | /**
72 | * Called each {@link #transformPage(View, float)} before {{@link #onTransform(View, float)}.
73 | *
74 | * The default implementation attempts to reset all view properties. This is useful when toggling transforms that do
75 | * not modify the same page properties. For instance changing from a transformation that applies rotation to a
76 | * transformation that fades can inadvertently leave a fragment stuck with a rotation or with some degree of applied
77 | * alpha.
78 | *
79 | * @param page
80 | * Apply the transformation to this page
81 | * @param position
82 | * Position of page relative to the current front-and-center position of the pager. 0 is front and
83 | * center. 1 is one full page position to the right, and -1 is one page position to the left.
84 | */
85 | protected void onPreTransform(View page, float position) {
86 | final float width = page.getWidth();
87 |
88 | page.setRotationX(0);
89 | page.setRotationY(0);
90 | page.setRotation(0);
91 | page.setScaleX(1);
92 | page.setScaleY(1);
93 | page.setPivotX(0);
94 | page.setPivotY(0);
95 | page.setTranslationY(0);
96 | page.setTranslationX(isPagingEnabled() ? 0f : -width * position);
97 |
98 | if (hideOffscreenPages()) {
99 | page.setAlpha(position <= -1f || position >= 1f ? 0f : 1f);
100 | // page.setEnabled(false);
101 | } else {
102 | // page.setEnabled(true);
103 | page.setAlpha(1f);
104 | }
105 | }
106 |
107 | /**
108 | * Called each {@link #transformPage(View, float)} after {@link #onTransform(View, float)}.
109 | *
110 | * @param page
111 | * Apply the transformation to this page
112 | * @param position
113 | * Position of page relative to the current front-and-center position of the pager. 0 is front and
114 | * center. 1 is one full page position to the right, and -1 is one page position to the left.
115 | */
116 | protected void onPostTransform(View page, float position) {
117 | }
118 |
119 | /**
120 | * Same as {@link Math#min(double, double)} without double casting, zero closest to infinity handling, or NaN support.
121 | *
122 | * @param val
123 | * @param min
124 | * @return
125 | */
126 | protected static final float min(float val, float min) {
127 | return val < min ? min : val;
128 | }
129 |
130 | }
131 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_banner_data.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
13 |
14 |
18 |
19 |
31 |
32 |
44 |
45 |
46 |
61 |
62 |
74 |
75 |
76 |
90 |
91 |
103 |
104 |
105 |
106 |
107 |
108 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/banner/BaseUseActivity.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.banner;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.util.Log;
8 | import android.view.View;
9 | import android.widget.ImageView;
10 | import android.widget.Toast;
11 |
12 | import com.bumptech.glide.Glide;
13 | import com.bumptech.glide.load.engine.DiskCacheStrategy;
14 | import com.bumptech.glide.request.RequestOptions;
15 | import com.geek.banner.Banner;
16 | import com.geek.banner.loader.BannerEntry;
17 | import com.geek.banner.loader.ImageLoader;
18 | import com.test.banner.R;
19 | import com.test.banner.bean.BannerItem;
20 |
21 | import java.util.ArrayList;
22 | import java.util.List;
23 |
24 | import butterknife.BindView;
25 | import butterknife.ButterKnife;
26 |
27 | /**
28 | * @Author: HSL
29 | * @Time: 2019/8/5 15:38
30 | * @E-mail: xxx@163.com
31 | * @Description: 基本使用~
32 | */
33 | public class BaseUseActivity extends AppCompatActivity {
34 |
35 | @BindView(R.id.banner)
36 | Banner banner;
37 | @BindView(R.id.banner_2)
38 | Banner banner2;
39 |
40 | private List mData = new ArrayList<>();
41 |
42 | public static void start(Context context) {
43 | Intent starter = new Intent(context, BaseUseActivity.class);
44 | context.startActivity(starter);
45 | }
46 |
47 | @Override
48 | protected void onCreate(Bundle savedInstanceState) {
49 | super.onCreate(savedInstanceState);
50 | setContentView(R.layout.activity_base_use);
51 | ButterKnife.bind(this);
52 | initData();
53 | initBanner();
54 | initBanner2();
55 | }
56 |
57 | private void initData() {
58 | mData.add(new BannerItem(R.drawable.banner_1, ""));
59 | mData.add(new BannerItem(R.drawable.banner_2, ""));
60 | mData.add(new BannerItem(R.drawable.banner_3, ""));
61 | mData.add(new BannerItem(R.drawable.banner_4, ""));
62 | mData.add(new BannerItem(R.drawable.banner_5, ""));
63 | }
64 |
65 | private void initBanner() {
66 | // 1. 创建设置BannerLoader
67 | banner.setBannerLoader(new ImageLoader() {
68 | @Override
69 | public void loadView(Context context, BannerEntry entry, int position, View imageView) {
70 | RequestOptions requestOptions = new RequestOptions()
71 | .placeholder(R.drawable.banner_default)
72 | .error(R.drawable.banner_default)
73 | .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
74 | Glide.with(context).load(entry.getBannerPath()).apply(requestOptions).into((ImageView) imageView);
75 | }
76 | });
77 |
78 | // 2 设置页面点击事件
79 | banner.setOnBannerClickListener(new Banner.OnBannerClickListener() {
80 | @Override
81 | public void onBannerClick(int position) {
82 | Toast.makeText(BaseUseActivity.this, "点击了:" + position, Toast.LENGTH_SHORT).show();
83 | }
84 | });
85 |
86 | // 3 翻页事件
87 | banner.setBannerPagerChangedListener(new Banner.OnBannerSimplePagerListener() {
88 | @Override
89 | public void onPageSelected(int position) {
90 | Log.d("hsl777", "onPageSelected: ==>" + position);
91 | }
92 | });
93 |
94 | // 4 最重要一步,加载数据
95 | banner.loadImagePaths(mData);
96 | }
97 |
98 | private void initBanner2() {
99 | // 1. 创建设置BannerLoader
100 | banner2.setBannerLoader(new ImageLoader() {
101 | @Override
102 | public void loadView(Context context, BannerEntry entry, int position, View imageView) {
103 | RequestOptions requestOptions = new RequestOptions()
104 | .placeholder(R.drawable.banner_default)
105 | .error(R.drawable.banner_default)
106 | .diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
107 | Glide.with(context).load(entry.getBannerPath()).apply(requestOptions).into((ImageView) imageView);
108 | }
109 | });
110 |
111 | // 2. 设置页面点击事件
112 | banner2.setOnBannerClickListener(new Banner.OnBannerClickListener() {
113 | @Override
114 | public void onBannerClick(int position) {
115 | Toast.makeText(BaseUseActivity.this, "点击了:" + position, Toast.LENGTH_SHORT).show();
116 | }
117 | });
118 |
119 | // 3. 翻页事件
120 | banner2.setBannerPagerChangedListener(new Banner.OnBannerSimplePagerListener() {
121 | @Override
122 | public void onPageSelected(int position) {
123 | Log.d("hsl777", "onPageSelected: ==>" + position);
124 | }
125 | });
126 |
127 | // 4. 最重要一步,加载数据
128 | banner2.loadImagePaths(mData);
129 | }
130 |
131 |
132 | @Override
133 | protected void onResume() {
134 | super.onResume();
135 | banner.startAutoPlay();
136 | banner2.startAutoPlay();
137 | }
138 |
139 | @Override
140 | protected void onPause() {
141 | super.onPause();
142 | banner.stopAutoPlay();
143 | banner2.stopAutoPlay();
144 | }
145 | }
146 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
19 |
20 |
26 |
27 |
36 |
37 |
43 |
44 |
53 |
54 |
60 |
61 |
70 |
71 |
77 |
78 |
87 |
88 |
94 |
95 |
104 |
105 |
111 |
112 |
116 |
117 |
127 |
128 |
134 |
135 |
142 |
143 |
144 |
145 |
146 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/banner/BannerViewActivity.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.banner;
2 |
3 | import android.content.Context;
4 | import android.content.Intent;
5 | import android.os.Bundle;
6 | import android.support.v7.app.AppCompatActivity;
7 | import android.util.Log;
8 | import android.view.LayoutInflater;
9 | import android.view.View;
10 | import android.widget.ImageView;
11 | import android.widget.Toast;
12 |
13 | import com.bumptech.glide.Glide;
14 | import com.bumptech.glide.load.engine.DiskCacheStrategy;
15 | import com.bumptech.glide.request.RequestOptions;
16 | import com.geek.banner.Banner;
17 | import com.geek.banner.loader.BannerEntry;
18 | import com.geek.banner.loader.BannerLoader;
19 | import com.test.banner.R;
20 | import com.test.banner.bean.BannerItem;
21 | import com.test.banner.widget.BannerView;
22 |
23 | import java.util.ArrayList;
24 | import java.util.List;
25 |
26 | import butterknife.BindView;
27 | import butterknife.ButterKnife;
28 | import cn.iwgang.countdownview.CountdownView;
29 | import cn.jzvd.Jzvd;
30 |
31 | /**
32 | * @Author: HSL
33 | * @Time: 2019/8/7 13:44
34 | * @E-mail: xxx@163.com
35 | * @Description: 自定义BannerView~
36 | */
37 | public class BannerViewActivity extends AppCompatActivity {
38 |
39 | @BindView(R.id.banner)
40 | Banner banner;
41 |
42 | private List mData = new ArrayList<>();
43 |
44 | public static void start(Context context) {
45 | Intent starter = new Intent(context, BannerViewActivity.class);
46 | context.startActivity(starter);
47 | }
48 |
49 | @Override
50 | protected void onCreate(Bundle savedInstanceState) {
51 | super.onCreate(savedInstanceState);
52 | setContentView(R.layout.activity_banner_view);
53 | ButterKnife.bind(this);
54 | initData();
55 | initBanner();
56 | }
57 |
58 | private void initData() {
59 | mData.add(new BannerItem(R.drawable.banner_1, ""));
60 | mData.add(new BannerItem("", ""));
61 | mData.add(new BannerItem(R.drawable.banner_3, ""));
62 | mData.add(new BannerItem("", ""));
63 | mData.add(new BannerItem(R.drawable.banner_5, ""));
64 | }
65 |
66 | private void initBanner() {
67 | //First 创建BannerLoader
68 | /**
69 | * 加载器
70 | * 第一个泛型:图片的URL FILE res...等
71 | * 第二个泛型:banner显示的View,可以是任意View
72 | */
73 | BannerLoader