13 | * Created by zhangpan on 2019-08-14. 14 | * Description: 15 | *16 | */ 17 | public interface ApiService { 18 | 19 | @Headers("Cache-Control: public, max-age=" + 3600) 20 | @GET("banner/json") 21 | Observable
- > getBannerData();
22 |
23 | @Headers("Cache-Control: public, max-age=" + 3600)
24 | @GET("article/list/0/json")
25 | Observable
- 4 |
- 5 | 6 |
- 4 |
- 5 | 6 |
- 4 |
- 5 |
- 4 |
- 5 |
- 4 |
- 5 |
- 6 |
9 | * Created by zhangpan on 2019-08-14. 10 | * Description: 11 | *12 | */ 13 | public class BannerData { 14 | 15 | /** 16 | * desc : 17 | * id : 20 18 | * imagePath : https://www.wanandroid.com/blogimgs/90c6cc12-742e-4c9f-b318-b912f163b8d0.png 19 | * isVisible : 1 20 | * order : 2 21 | * title : flutter 中文社区 22 | * type : 1 23 | * url : https://flutter.cn/ 24 | */ 25 | 26 | public static final int TYPE_NEW = 10000; 27 | 28 | private String desc; 29 | private int id; 30 | private String imagePath; 31 | private int isVisible; 32 | private int order; 33 | private String title; 34 | private int type; 35 | private String url; 36 | private int drawable; 37 | private @DrawableRes final int placeHolder = R.drawable.placeholder; 38 | 39 | public int getDrawable() { 40 | return drawable; 41 | } 42 | 43 | public void setDrawable(int drawable) { 44 | this.drawable = drawable; 45 | } 46 | 47 | public String getDesc() { 48 | return desc; 49 | } 50 | 51 | public void setDesc(String desc) { 52 | this.desc = desc; 53 | } 54 | 55 | public int getId() { 56 | return id; 57 | } 58 | 59 | public void setId(int id) { 60 | this.id = id; 61 | } 62 | 63 | public String getImagePath() { 64 | return imagePath; 65 | } 66 | 67 | public void setImagePath(String imagePath) { 68 | this.imagePath = imagePath; 69 | } 70 | 71 | public int getIsVisible() { 72 | return isVisible; 73 | } 74 | 75 | public void setIsVisible(int isVisible) { 76 | this.isVisible = isVisible; 77 | } 78 | 79 | public int getOrder() { 80 | return order; 81 | } 82 | 83 | public void setOrder(int order) { 84 | this.order = order; 85 | } 86 | 87 | public String getTitle() { 88 | return title; 89 | } 90 | 91 | public void setTitle(String title) { 92 | this.title = title; 93 | } 94 | 95 | public int getType() { 96 | return type; 97 | } 98 | 99 | public void setType(int type) { 100 | this.type = type; 101 | } 102 | 103 | public String getUrl() { 104 | return url; 105 | } 106 | 107 | public void setUrl(String url) { 108 | this.url = url; 109 | } 110 | 111 | public @DrawableRes 112 | int getPlaceHolder() { 113 | return placeHolder; 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/zhpan/banner/net/Constants.java: -------------------------------------------------------------------------------- 1 | package com.example.zhpan.banner.net; 2 | 3 | /** 4 | *
5 | * Created by zhangpan on 2019-08-14. 6 | * Description: 7 | *8 | */ 9 | public interface Constants { 10 | String BASE_URL = "https://www.wanandroid.com/"; 11 | } 12 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/zhpan/banner/net/RetrofitGnerator.java: -------------------------------------------------------------------------------- 1 | package com.example.zhpan.banner.net; 2 | 3 | import com.example.zhpan.banner.net.common.IdeaApi; 4 | 5 | /** 6 | *
7 | * Created by zhangpan on 2019-08-14. 8 | * Description: 9 | *10 | */ 11 | public class RetrofitGnerator { 12 | private static ApiService mApiservice; 13 | 14 | public static ApiService getApiSerVice() { 15 | if (mApiservice == null) { 16 | mApiservice = IdeaApi.getApiService(ApiService.class, Constants.BASE_URL); 17 | } 18 | return mApiservice; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/example/zhpan/banner/net/RxUtil.java: -------------------------------------------------------------------------------- 1 | package com.example.zhpan.banner.net; 2 | 3 | import com.example.zhpan.banner.net.common.LoadingTransformerCreator; 4 | import com.trello.rxlifecycle2.android.ActivityEvent; 5 | import com.trello.rxlifecycle2.android.FragmentEvent; 6 | import com.trello.rxlifecycle2.components.support.RxAppCompatActivity; 7 | import com.trello.rxlifecycle2.components.support.RxFragment; 8 | 9 | import io.reactivex.Observable; 10 | import io.reactivex.ObservableTransformer; 11 | import io.reactivex.android.schedulers.AndroidSchedulers; 12 | import io.reactivex.schedulers.Schedulers; 13 | 14 | @SuppressWarnings("unused") 15 | public class RxUtil { 16 | 17 | /** 18 | * 统一线程处理 19 | * 20 | * @return ObservableTransformer 21 | */ 22 | public static
36 | * Because Gson is so flexible in the types it supports, this converter assumes that it can handle
37 | * all types. If you are mixing JSON serialization with something else (such as protocol buffers),
38 | * you must {@linkplain Retrofit.Builder#addConverterFactory(Converter.Factory) add this instance}
39 | * last to allow the other converters a chance to see their types.
40 | */
41 | public final class GsonConverterFactory extends Converter.Factory {
42 | /**
43 | * Create an instance using a default {@link Gson} instance for conversion. Encoding to JSON and
44 | * decoding from JSON (when no charset is specified by a header) will use UTF-8.
45 | */
46 | public static GsonConverterFactory create() {
47 | return create(new Gson());
48 | }
49 |
50 | /**
51 | * Create an instance using {@code gson} for conversion. Encoding to JSON and
52 | * decoding from JSON (when no charset is specified by a header) will use UTF-8.
53 | */
54 | public static GsonConverterFactory create(Gson gson) {
55 | return new GsonConverterFactory(gson);
56 | }
57 |
58 | private final Gson gson;
59 |
60 | private GsonConverterFactory(Gson gson) {
61 | if (gson == null) throw new NullPointerException("gson == null");
62 | this.gson = gson;
63 | }
64 |
65 | @Override
66 | public Converter
71 | * The default implementation attempts to reset all view properties. This is useful when toggling
72 | * transforms that do
73 | * not modify the same page properties. For instance changing from a transformation that applies
74 | * rotation to a
75 | * transformation that fades can inadvertently leave a fragment stuck with a rotation or with some
76 | * degree of applied
77 | * alpha.
78 | *
79 | * @param page Apply the transformation to this page
80 | * @param position Position of page relative to the current front-and-center position of the
81 | * pager. 0 is front and
82 | * center. 1 is one full page position to the right, and -1 is one page position to the left.
83 | */
84 | protected void onPreTransform(View page, float position) {
85 | final float width = page.getWidth();
86 |
87 | page.setRotationX(0);
88 | page.setRotationY(0);
89 | page.setRotation(0);
90 | page.setScaleX(1);
91 | page.setScaleY(1);
92 | page.setPivotX(0);
93 | page.setPivotY(0);
94 | page.setTranslationY(0);
95 | page.setTranslationX(isPagingEnabled() ? 0f : -width * position);
96 |
97 | if (hideOffscreenPages()) {
98 | page.setAlpha(position <= -1f || position >= 1f ? 0f : 1f);
99 | // page.setEnabled(false);
100 | } else {
101 | // page.setEnabled(true);
102 | page.setAlpha(1f);
103 | }
104 | }
105 |
106 | /**
107 | * Called each {@link #transformPage(View, float)} after {@link #onTransform(View, float)}.
108 | *
109 | * @param page Apply the transformation to this page
110 | * @param position Position of page relative to the current front-and-center position of the
111 | * pager. 0 is front and
112 | * center. 1 is one full page position to the right, and -1 is one page position to the left.
113 | */
114 | protected void onPostTransform(View page, float position) {
115 | }
116 |
117 | /**
118 | * Same as { Math#min(double, double)} without double casting, zero closest to infinity handling,
119 | * or NaN support.
120 | */
121 | protected static final float min(float val, float min) {
122 | return val < min ? min : val;
123 | }
124 | }
125 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/zhpan/banner/transform/DepthPageTransformer.kt:
--------------------------------------------------------------------------------
1 |
2 | package com.example.zhpan.banner.transform
3 |
4 | import android.view.View
5 | import androidx.viewpager2.widget.ViewPager2
6 |
7 | class DepthPageTransformer : ViewPager2.PageTransformer {
8 |
9 | override fun transformPage(page: View, position: Float) {
10 | page.also {
11 | if (kotlin.math.abs(position) >= 1f) {
12 | it.alpha = 0f
13 | return@transformPage
14 | }
15 | if (position > 0) {
16 | it.alpha = 1 - position
17 | val scale = 1f - position / 4f
18 | it.scaleX = scale
19 | it.scaleY = scale
20 | it.translationX = -it.width * position
21 | it.translationZ = -1f
22 | } else {
23 | it.alpha = 1f
24 | it.scaleX = 1f
25 | it.scaleY = 1f
26 | it.translationX = 0f
27 | it.translationZ = 0f
28 | }
29 | }
30 | }
31 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/example/zhpan/banner/transform/DepthScaleTransformer.kt:
--------------------------------------------------------------------------------
1 |
2 | package com.example.zhpan.banner.transform
3 |
4 | import android.view.View
5 | import androidx.viewpager2.widget.ViewPager2
6 |
7 | class DepthScaleTransformer : ViewPager2.PageTransformer {
8 |
9 | companion object {
10 | private const val MIN_SCALE = 0.9f
11 | private const val MIN_ALPHA = 0.7f
12 | }
13 |
14 | override fun transformPage(page: View, position: Float) {
15 | page.also {
16 | if (kotlin.math.abs(position) >= 1f) {
17 | it.alpha = 0f
18 | return@transformPage
19 | }
20 | val scale = (1 - kotlin.math.abs(position) / 2).coerceAtLeast(MIN_SCALE)
21 | it.scaleX = scale
22 | it.scaleY = scale
23 | it.alpha = (1 - kotlin.math.abs(position)).coerceAtLeast(MIN_ALPHA)
24 | it.translationX = (1 - scale) * it.width / 2 * if (position > 0) -1 else 1
25 | }
26 | }
27 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/example/zhpan/banner/transform/PageTransformerFactory.java:
--------------------------------------------------------------------------------
1 | package com.example.zhpan.banner.transform;
2 |
3 | import androidx.viewpager2.widget.ViewPager2;
4 |
5 | import com.zhpan.bannerview.transform.ScaleInTransformer;
6 |
7 | import static com.example.zhpan.banner.transform.TransformerStyle.ACCORDION;
8 | import static com.example.zhpan.banner.transform.TransformerStyle.DEPTH;
9 | import static com.example.zhpan.banner.transform.TransformerStyle.DEPTH_SCALE;
10 | import static com.example.zhpan.banner.transform.TransformerStyle.ROTATE;
11 | import static com.example.zhpan.banner.transform.TransformerStyle.ROTATE_UP;
12 | import static com.example.zhpan.banner.transform.TransformerStyle.SCALE_IN;
13 |
14 | public class PageTransformerFactory {
15 |
16 | public static ViewPager2.PageTransformer createPageTransformer(int transformerStyle) {
17 | ViewPager2.PageTransformer transformer = null;
18 | switch (transformerStyle) {
19 | case DEPTH:
20 | transformer = new DepthPageTransformer();
21 | break;
22 | case ROTATE:
23 | transformer = new RotateTransformer();
24 | break;
25 | case ROTATE_UP:
26 | transformer = new RotateUpTransformer();
27 | break;
28 | case DEPTH_SCALE:
29 | transformer = new DepthScaleTransformer();
30 | break;
31 | case ACCORDION:
32 | transformer = new AccordionTransformer();
33 | break;
34 | case SCALE_IN:
35 | transformer = new ScaleInTransformer(ScaleInTransformer.DEFAULT_MIN_SCALE, false);
36 | break;
37 | }
38 | return transformer;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/zhpan/banner/transform/RotateTransformer.kt:
--------------------------------------------------------------------------------
1 |
2 | package com.example.zhpan.banner.transform
3 |
4 | import android.view.View
5 | import androidx.viewpager2.widget.ViewPager2
6 | import kotlin.math.abs
7 |
8 | class RotateTransformer : ViewPager2.PageTransformer {
9 | override fun transformPage(page: View, position: Float) {
10 | page.also {
11 | if (abs(position) >= 1f) {
12 | it.alpha = 0f
13 | return@transformPage
14 | }
15 | val scale = (1 - abs(position)).coerceAtLeast(MIN_SCALE)
16 | it.scaleX = scale
17 | it.scaleY = scale
18 | it.alpha = 1f
19 | it.translationX = (1 - scale) * it.width / 2 * if (position > 0) -1 else 1
20 | it.translationZ = if (scale == MIN_SCALE) -1f else 0f
21 | it.rotation = -position * 45
22 | }
23 | }
24 |
25 | companion object{
26 | private const val MIN_SCALE = 0.5f
27 | }
28 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/example/zhpan/banner/transform/RotateUpTransformer.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.example.zhpan.banner.transform;
18 |
19 | import android.view.View;
20 |
21 | public class RotateUpTransformer extends BaseTransformer {
22 |
23 | private static final float ROT_MOD = -15f;
24 |
25 | @Override
26 | protected void onTransform(View view, float position) {
27 | final float width = view.getWidth();
28 | final float rotation = ROT_MOD * position;
29 |
30 | view.setPivotX(width * 0.5f);
31 | view.setPivotY(0f);
32 | view.setTranslationX(0f);
33 | view.setRotation(rotation);
34 | }
35 |
36 | @Override
37 | protected boolean isPagingEnabled() {
38 | return true;
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/zhpan/banner/transform/TransformerStyle.java:
--------------------------------------------------------------------------------
1 | package com.example.zhpan.banner.transform;
2 |
3 | public interface TransformerStyle {
4 | int NONE = 0;
5 | int DEPTH = 1;
6 | int DEPTH_SCALE = 2;
7 | int ACCORDION = 3;
8 | int ROTATE = 4;
9 | int ROTATE_UP=5;
10 | int SCALE_IN = 6;
11 | }
12 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/zhpan/banner/view/CornerImageView.kt:
--------------------------------------------------------------------------------
1 | package com.example.zhpan.banner.view
2 |
3 | import android.content.Context
4 | import android.util.AttributeSet
5 |
6 | import androidx.appcompat.widget.AppCompatImageView
7 | import com.zhpan.bannerview.provider.ViewStyleSetter
8 |
9 | class CornerImageView @JvmOverloads constructor(
10 | context: Context,
11 | attrs: AttributeSet? = null,
12 | defStyleAttr: Int = 0
13 | ) : AppCompatImageView(context, attrs, defStyleAttr) {
14 |
15 | fun setRoundCorner(radius: Int) {
16 | ViewStyleSetter.applyRoundCorner(this, radius.toFloat())
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/app/src/main/java/com/example/zhpan/banner/view/FigureIndicatorView.java:
--------------------------------------------------------------------------------
1 | package com.example.zhpan.banner.view;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.graphics.Color;
6 | import android.graphics.Paint;
7 | import android.util.AttributeSet;
8 |
9 | import androidx.annotation.ColorInt;
10 | import androidx.annotation.Nullable;
11 |
12 | import com.zhpan.bannerview.utils.BannerUtils;
13 | import com.zhpan.indicator.base.BaseIndicatorView;
14 |
15 | /**
16 | *
17 | * Created by zhangpan on 2019-10-18.
18 | * Description:
19 | *
20 | */
21 | public class FigureIndicatorView extends BaseIndicatorView {
22 |
23 | private int radius = BannerUtils.dp2px(20);
24 |
25 | private int backgroundColor = Color.parseColor("#88FF5252");
26 |
27 | private int textColor = Color.WHITE;
28 |
29 | private int textSize = BannerUtils.dp2px(13);
30 |
31 | private Paint mPaint;
32 |
33 | public FigureIndicatorView(Context context) {
34 | this(context, null);
35 | }
36 |
37 | public FigureIndicatorView(Context context, @Nullable AttributeSet attrs) {
38 | this(context, attrs, 0);
39 | }
40 |
41 | public FigureIndicatorView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
42 | super(context, attrs, defStyleAttr);
43 | mPaint = new Paint();
44 | }
45 |
46 | @Override
47 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
48 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
49 | setMeasuredDimension(2 * radius, 2 * radius);
50 | }
51 |
52 | @Override
53 | protected void onDraw(Canvas canvas) {
54 | super.onDraw(canvas);
55 | if (getPageSize() > 1) {
56 | mPaint.setColor(backgroundColor);
57 | canvas.drawCircle(getWidth() / 2f, getHeight() / 2f, radius, mPaint);
58 | mPaint.setColor(textColor);
59 | mPaint.setTextSize(textSize);
60 | String text = getCurrentPosition() + 1 + "/" + getPageSize();
61 | int textWidth = (int) mPaint.measureText(text);
62 | Paint.FontMetricsInt fontMetricsInt = mPaint.getFontMetricsInt();
63 | int baseline = (getMeasuredHeight() - fontMetricsInt.bottom + fontMetricsInt.top) / 2
64 | - fontMetricsInt.top;
65 | canvas.drawText(text, (getWidth() - textWidth) / 2f, baseline, mPaint);
66 | }
67 | }
68 |
69 | public void setRadius(int radius) {
70 | this.radius = radius;
71 | }
72 |
73 | @Override
74 | public void setBackgroundColor(@ColorInt int backgroundColor) {
75 | this.backgroundColor = backgroundColor;
76 | }
77 |
78 | public void setTextSize(int textSize) {
79 | this.textSize = textSize;
80 | }
81 |
82 | public void setTextColor(int textColor) {
83 | this.textColor = textColor;
84 | }
85 | }
86 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-anydpi/ic_back.xml:
--------------------------------------------------------------------------------
1 |
38 | * Created by zhpan on 2020/4/5.
39 | * Attention:Don't use {@link RecyclerView.ViewHolder#getAdapterPosition}
40 | * method to get position,this method will return a fake position.
41 | *
42 | */
43 | @SuppressWarnings("unused")
44 | public class BaseViewHolder
32 | * Created by zhangpan on 2019-10-18.
33 | * Description:指示器显示位置
34 | *
35 | */
36 | @IntDef({ CENTER, START, END })
37 | @Retention(RetentionPolicy.SOURCE)
38 | @Target(ElementType.PARAMETER)
39 | public @interface AIndicatorGravity {
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/bannerview/src/main/java/com/zhpan/bannerview/annotation/APageStyle.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2017 zhpanvip The BannerViewPager Open Source Project
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.zhpan.bannerview.annotation;
18 |
19 | import androidx.annotation.IntDef;
20 |
21 | import java.lang.annotation.ElementType;
22 | import java.lang.annotation.Retention;
23 | import java.lang.annotation.RetentionPolicy;
24 | import java.lang.annotation.Target;
25 |
26 | import static com.zhpan.bannerview.constants.PageStyle.MULTI_PAGE;
27 | import static com.zhpan.bannerview.constants.PageStyle.MULTI_PAGE_OVERLAP;
28 | import static com.zhpan.bannerview.constants.PageStyle.MULTI_PAGE_SCALE;
29 | import static com.zhpan.bannerview.constants.PageStyle.NORMAL;
30 |
31 | /**
32 | *
33 | * Created by zhangpan on 2019-11-06.
34 | * Description:
35 | *
36 | */
37 | @IntDef({ NORMAL, MULTI_PAGE, MULTI_PAGE_OVERLAP, MULTI_PAGE_SCALE })
38 | @Retention(RetentionPolicy.SOURCE)
39 | @Target(ElementType.PARAMETER)
40 | public @interface APageStyle {
41 | }
42 |
--------------------------------------------------------------------------------
/bannerview/src/main/java/com/zhpan/bannerview/annotation/Visibility.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2017 zhpanvip The BannerViewPager Open Source Project
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.zhpan.bannerview.annotation;
18 |
19 | import android.view.View;
20 |
21 | import androidx.annotation.IntDef;
22 |
23 | import java.lang.annotation.ElementType;
24 | import java.lang.annotation.Retention;
25 | import java.lang.annotation.RetentionPolicy;
26 | import java.lang.annotation.Target;
27 |
28 | /**
29 | *
30 | * Created by zhangpan on 2019-11-12.
31 | *
32 | */
33 | @IntDef({ View.VISIBLE, View.INVISIBLE, View.GONE })
34 | @Retention(RetentionPolicy.SOURCE)
35 | @Target(ElementType.PARAMETER)
36 | public @interface Visibility {
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/bannerview/src/main/java/com/zhpan/bannerview/constants/IndicatorGravity.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2017 zhpanvip The BannerViewPager Open Source Project
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.zhpan.bannerview.constants;
18 |
19 | /**
20 | *
21 | * Created by zhangpan on 2019-10-18.
22 | * Description:
23 | *
24 | */
25 | public interface IndicatorGravity {
26 | int CENTER = 0;
27 | int START = 1 << 1;
28 | int END = 1 << 2;
29 | }
30 |
--------------------------------------------------------------------------------
/bannerview/src/main/java/com/zhpan/bannerview/constants/PageStyle.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2017 zhpanvip The BannerViewPager Open Source Project
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.zhpan.bannerview.constants;
18 |
19 | /**
20 | *
21 | * Created by zhangpan on 2019-10-18.
22 | * Description:
23 | *
24 | */
25 | public interface PageStyle {
26 |
27 | int NORMAL = 0;
28 | /**
29 | * @deprecated please use {@link com.zhpan.bannerview.BannerViewPager#setRevealWidth(int)} instead.
30 | */
31 | @Deprecated
32 | int MULTI_PAGE = 1 << 1;
33 | /**
34 | * Requires Api Version >= 21
35 | */
36 | int MULTI_PAGE_OVERLAP = 1 << 2;
37 |
38 | int MULTI_PAGE_SCALE = 1 << 3;
39 | }
40 |
--------------------------------------------------------------------------------
/bannerview/src/main/java/com/zhpan/bannerview/manager/AttributeController.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2017 zhpanvip The BannerViewPager Open Source Project
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.zhpan.bannerview.manager;
18 |
19 | import android.content.Context;
20 | import android.content.res.TypedArray;
21 | import android.graphics.Color;
22 | import android.util.AttributeSet;
23 |
24 | import androidx.annotation.NonNull;
25 | import androidx.annotation.Nullable;
26 |
27 | import com.zhpan.bannerview.R;
28 | import com.zhpan.bannerview.utils.BannerUtils;
29 |
30 | import static com.zhpan.bannerview.manager.BannerOptions.DEFAULT_REVEAL_WIDTH;
31 |
32 | /**
33 | *
34 | * Created by zhpan on 2019/11/20.
35 | * Description:初始化xml的自定义属性
36 | *
37 | */
38 | public class AttributeController {
39 |
40 | private final BannerOptions mBannerOptions;
41 |
42 | public AttributeController(BannerOptions bannerOptions) {
43 | mBannerOptions = bannerOptions;
44 | }
45 |
46 | public void init(@NonNull Context context, @Nullable AttributeSet attrs) {
47 | if (attrs != null) {
48 | TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.BannerViewPager);
49 | initBannerAttrs(typedArray);
50 | initIndicatorAttrs(typedArray);
51 | typedArray.recycle();
52 | }
53 | }
54 |
55 | private void initIndicatorAttrs(TypedArray typedArray) {
56 | int indicatorCheckedColor =
57 | typedArray.getColor(R.styleable.BannerViewPager_bvp_indicator_checked_color,
58 | Color.parseColor("#8C18171C"));
59 | int indicatorNormalColor =
60 | typedArray.getColor(R.styleable.BannerViewPager_bvp_indicator_normal_color,
61 | Color.parseColor("#8C6C6D72"));
62 | int normalIndicatorWidth =
63 | (int) typedArray.getDimension(R.styleable.BannerViewPager_bvp_indicator_radius,
64 | BannerUtils.dp2px(8));
65 | int indicatorGravity = typedArray.getInt(R.styleable.BannerViewPager_bvp_indicator_gravity, 0);
66 | int indicatorStyle = typedArray.getInt(R.styleable.BannerViewPager_bvp_indicator_style, 0);
67 | int indicatorSlideMode =
68 | typedArray.getInt(R.styleable.BannerViewPager_bvp_indicator_slide_mode, 0);
69 | int indicatorVisibility =
70 | typedArray.getInt(R.styleable.BannerViewPager_bvp_indicator_visibility, 0);
71 | mBannerOptions.setIndicatorSliderColor(indicatorNormalColor, indicatorCheckedColor);
72 | mBannerOptions.setIndicatorSliderWidth(normalIndicatorWidth, normalIndicatorWidth);
73 | mBannerOptions.setIndicatorGravity(indicatorGravity);
74 | mBannerOptions.setIndicatorStyle(indicatorStyle);
75 | mBannerOptions.setIndicatorSlideMode(indicatorSlideMode);
76 | mBannerOptions.setIndicatorVisibility(indicatorVisibility);
77 | mBannerOptions.setIndicatorGap(normalIndicatorWidth);
78 | mBannerOptions.setIndicatorHeight(normalIndicatorWidth / 2);
79 | }
80 |
81 | private void initBannerAttrs(TypedArray typedArray) {
82 | int interval = typedArray.getInteger(R.styleable.BannerViewPager_bvp_interval, 3000);
83 | boolean isAutoPlay = typedArray.getBoolean(R.styleable.BannerViewPager_bvp_auto_play, true);
84 | boolean isCanLoop = typedArray.getBoolean(R.styleable.BannerViewPager_bvp_can_loop, true);
85 | int pageMargin = (int) typedArray.getDimension(R.styleable.BannerViewPager_bvp_page_margin, 0);
86 | int roundCorner =
87 | (int) typedArray.getDimension(R.styleable.BannerViewPager_bvp_round_corner, 0);
88 | int revealWidth = (int) typedArray.getDimension(R.styleable.BannerViewPager_bvp_reveal_width,
89 | DEFAULT_REVEAL_WIDTH);
90 | int pageStyle = typedArray.getInt(R.styleable.BannerViewPager_bvp_page_style, 0);
91 | int scrollDuration = typedArray.getInt(R.styleable.BannerViewPager_bvp_scroll_duration, 0);
92 | mBannerOptions.setInterval(interval);
93 | mBannerOptions.setAutoPlay(isAutoPlay);
94 | mBannerOptions.setCanLoop(isCanLoop);
95 | mBannerOptions.setPageMargin(pageMargin);
96 | mBannerOptions.setRoundRectRadius(roundCorner);
97 | mBannerOptions.setRightRevealWidth(revealWidth);
98 | mBannerOptions.setLeftRevealWidth(revealWidth);
99 | mBannerOptions.setPageStyle(pageStyle);
100 | mBannerOptions.setScrollDuration(scrollDuration);
101 | }
102 | }
103 |
--------------------------------------------------------------------------------
/bannerview/src/main/java/com/zhpan/bannerview/manager/BannerManager.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2017 zhpanvip The BannerViewPager Open Source Project
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.zhpan.bannerview.manager;
18 |
19 | import android.content.Context;
20 | import android.os.Build;
21 | import android.util.AttributeSet;
22 |
23 | import androidx.annotation.NonNull;
24 | import androidx.viewpager2.widget.CompositePageTransformer;
25 | import androidx.viewpager2.widget.MarginPageTransformer;
26 | import androidx.viewpager2.widget.ViewPager2;
27 |
28 | import com.zhpan.bannerview.transform.OverlapPageTransformer;
29 | import com.zhpan.bannerview.transform.ScaleInTransformer;
30 |
31 | /**
32 | *
33 | * Created by zhpan on 2019/11/20.
34 | * Description:
35 | *
36 | */
37 | public class BannerManager {
38 |
39 | private BannerOptions mBannerOptions;
40 |
41 | private final AttributeController mAttributeController;
42 |
43 | private final CompositePageTransformer mCompositePageTransformer;
44 |
45 | private MarginPageTransformer mMarginPageTransformer;
46 |
47 | private ViewPager2.PageTransformer mDefaultPageTransformer;
48 |
49 | public BannerManager() {
50 | mBannerOptions = new BannerOptions();
51 | mAttributeController = new AttributeController(mBannerOptions);
52 | mCompositePageTransformer = new CompositePageTransformer();
53 | }
54 |
55 | public BannerOptions getBannerOptions() {
56 | if (mBannerOptions == null) {
57 | mBannerOptions = new BannerOptions();
58 | }
59 | return mBannerOptions;
60 | }
61 |
62 | public void initAttrs(Context context, AttributeSet attrs) {
63 | mAttributeController.init(context, attrs);
64 | }
65 |
66 | public CompositePageTransformer getCompositePageTransformer() {
67 | return mCompositePageTransformer;
68 | }
69 |
70 | public void addTransformer(@NonNull ViewPager2.PageTransformer transformer) {
71 | mCompositePageTransformer.addTransformer(transformer);
72 | }
73 |
74 | public void removeTransformer(@NonNull ViewPager2.PageTransformer transformer) {
75 | mCompositePageTransformer.removeTransformer(transformer);
76 | }
77 |
78 | public void removeMarginPageTransformer() {
79 | if (mMarginPageTransformer != null) {
80 | mCompositePageTransformer.removeTransformer(mMarginPageTransformer);
81 | }
82 | }
83 |
84 | public void removeDefaultPageTransformer() {
85 | if (mDefaultPageTransformer != null) {
86 | mCompositePageTransformer.removeTransformer(mDefaultPageTransformer);
87 | }
88 | }
89 |
90 | public void setPageMargin(int pageMargin) {
91 | mBannerOptions.setPageMargin(pageMargin);
92 | }
93 |
94 | public void createMarginTransformer() {
95 | removeMarginPageTransformer();
96 | mMarginPageTransformer = new MarginPageTransformer(mBannerOptions.getPageMargin());
97 | mCompositePageTransformer.addTransformer(mMarginPageTransformer);
98 | }
99 |
100 | public void setMultiPageStyle(boolean overlap, float scale) {
101 | removeDefaultPageTransformer();
102 | if (overlap && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
103 | mDefaultPageTransformer = new OverlapPageTransformer(mBannerOptions
104 | .getOrientation(), scale, 0f, 1, 0);
105 | } else {
106 | mDefaultPageTransformer = new ScaleInTransformer(scale, mBannerOptions.isRtl());
107 | }
108 | mCompositePageTransformer.addTransformer(mDefaultPageTransformer);
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/bannerview/src/main/java/com/zhpan/bannerview/provider/ReflectLayoutManager.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2017 zhpanvip The BannerViewPager Open Source Project
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.zhpan.bannerview.provider;
18 |
19 | import androidx.recyclerview.widget.LinearLayoutManager;
20 | import androidx.recyclerview.widget.RecyclerView;
21 | import androidx.viewpager2.widget.ViewPager2;
22 |
23 | import java.lang.reflect.Field;
24 |
25 | /**
26 | * 通过反射修改页面滑动的时间
27 | * Thanks:https://github.com/zguop/banner/blob/master/pager2banner/src/main/java/com/to/aboomy/pager2banner/Banner.java
28 | */
29 | public class ReflectLayoutManager {
30 |
31 | public static void reflectLayoutManager(ViewPager2 viewPager2, int scrollDuration) {
32 | try {
33 | RecyclerView recyclerView = (RecyclerView) viewPager2.getChildAt(0);
34 | recyclerView.setOverScrollMode(RecyclerView.OVER_SCROLL_NEVER);
35 | LinearLayoutManager linearLayoutManager =
36 | (LinearLayoutManager) recyclerView.getLayoutManager();
37 | if (linearLayoutManager == null) {
38 | return;
39 | }
40 | ScrollDurationManger scrollDurationManger =
41 | new ScrollDurationManger(viewPager2, scrollDuration, linearLayoutManager);
42 | recyclerView.setLayoutManager(scrollDurationManger);
43 |
44 | Field mRecyclerField = RecyclerView.LayoutManager.class.getDeclaredField("mRecyclerView");
45 | mRecyclerField.setAccessible(true);
46 | mRecyclerField.set(linearLayoutManager, recyclerView);
47 | Field layoutMangerField = ViewPager2.class.getDeclaredField("mLayoutManager");
48 | layoutMangerField.setAccessible(true);
49 | layoutMangerField.set(viewPager2, scrollDurationManger);
50 |
51 | Field pageTransformerAdapterField =
52 | ViewPager2.class.getDeclaredField("mPageTransformerAdapter");
53 | pageTransformerAdapterField.setAccessible(true);
54 | Object mPageTransformerAdapter = pageTransformerAdapterField.get(viewPager2);
55 | if (mPageTransformerAdapter != null) {
56 | Class> aClass = mPageTransformerAdapter.getClass();
57 | Field layoutManager = aClass.getDeclaredField("mLayoutManager");
58 | layoutManager.setAccessible(true);
59 | layoutManager.set(mPageTransformerAdapter, scrollDurationManger);
60 | }
61 | Field scrollEventAdapterField = ViewPager2.class.getDeclaredField("mScrollEventAdapter");
62 | scrollEventAdapterField.setAccessible(true);
63 | Object mScrollEventAdapter = scrollEventAdapterField.get(viewPager2);
64 | if (mScrollEventAdapter != null) {
65 | Class> aClass = mScrollEventAdapter.getClass();
66 | Field layoutManager = aClass.getDeclaredField("mLayoutManager");
67 | layoutManager.setAccessible(true);
68 | layoutManager.set(mScrollEventAdapter, scrollDurationManger);
69 | }
70 | } catch (NoSuchFieldException | IllegalAccessException e) {
71 | e.printStackTrace();
72 | }
73 | }
74 | }
75 |
--------------------------------------------------------------------------------
/bannerview/src/main/java/com/zhpan/bannerview/provider/RoundViewOutlineProvider.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2017 zhpanvip The BannerViewPager Open Source Project
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.zhpan.bannerview.provider;
18 |
19 | import android.annotation.TargetApi;
20 | import android.graphics.Outline;
21 | import android.graphics.Rect;
22 | import android.os.Build;
23 | import android.view.View;
24 | import android.view.ViewOutlineProvider;
25 |
26 | /**
27 | *
28 | * Created by zhangpan on 2018/12/26.
29 | * Description:圆角效果
30 | *
31 | */
32 |
33 | @TargetApi(Build.VERSION_CODES.LOLLIPOP)
34 | public class RoundViewOutlineProvider extends ViewOutlineProvider {
35 | /**
36 | * 圆角弧度
37 | */
38 | private final float mRadius;
39 |
40 | public RoundViewOutlineProvider(float radius) {
41 | this.mRadius = radius;
42 | }
43 |
44 | @Override
45 | public void getOutline(View view, Outline outline) {
46 | // 绘制区域
47 | Rect selfRect = new Rect(0, 0, view.getWidth(), view.getHeight());
48 | outline.setRoundRect(selfRect, mRadius);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/bannerview/src/main/java/com/zhpan/bannerview/provider/ScrollDurationManger.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2017 zhpanvip The BannerViewPager Open Source Project
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.zhpan.bannerview.provider;
18 |
19 | import android.graphics.Rect;
20 | import android.os.Bundle;
21 | import android.view.View;
22 |
23 | import androidx.annotation.NonNull;
24 | import androidx.annotation.Nullable;
25 | import androidx.core.view.accessibility.AccessibilityNodeInfoCompat;
26 | import androidx.recyclerview.widget.LinearLayoutManager;
27 | import androidx.recyclerview.widget.LinearSmoothScroller;
28 | import androidx.recyclerview.widget.RecyclerView;
29 | import androidx.viewpager2.widget.ViewPager2;
30 |
31 | import com.zhpan.bannerview.utils.BannerUtils;
32 |
33 | import java.lang.reflect.InvocationTargetException;
34 | import java.lang.reflect.Method;
35 |
36 | /**
37 | * @author zhangpan
38 | * @date 2020/12/21
39 | */
40 | public class ScrollDurationManger extends LinearLayoutManager {
41 | private final LinearLayoutManager mParent;
42 | private final int scrollDuration;
43 |
44 | public ScrollDurationManger(ViewPager2 viewPager2, int scrollDuration,
45 | LinearLayoutManager linearLayoutManager) {
46 | super(viewPager2.getContext(), linearLayoutManager.getOrientation(), false);
47 | this.scrollDuration = scrollDuration;
48 | mParent = linearLayoutManager;
49 | }
50 |
51 | @Override
52 | public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state,
53 | int position) {
54 | LinearSmoothScroller linearSmoothScroller =
55 | new LinearSmoothScroller(recyclerView.getContext()) {
56 | @Override
57 | protected int calculateTimeForDeceleration(int dx) {
58 | return scrollDuration;
59 | }
60 | };
61 | linearSmoothScroller.setTargetPosition(position);
62 | startSmoothScroll(linearSmoothScroller);
63 | }
64 |
65 | @Override
66 | public boolean performAccessibilityAction(@NonNull RecyclerView.Recycler recycler,
67 | @NonNull RecyclerView.State state, int action, @Nullable Bundle args) {
68 | return mParent.performAccessibilityAction(recycler, state, action, args);
69 | }
70 |
71 | @Override
72 | public void onInitializeAccessibilityNodeInfo(@NonNull RecyclerView.Recycler recycler,
73 | @NonNull RecyclerView.State state, @NonNull AccessibilityNodeInfoCompat info) {
74 | mParent.onInitializeAccessibilityNodeInfo(recycler, state, info);
75 | }
76 |
77 | @Override
78 | protected void calculateExtraLayoutSpace(@NonNull RecyclerView.State state,
79 | @NonNull int[] extraLayoutSpace) {
80 | try {
81 | Method method = mParent.getClass()
82 | .getDeclaredMethod("calculateExtraLayoutSpace",
83 | state.getClass(), extraLayoutSpace.getClass());
84 | method.setAccessible(true);
85 | method.invoke(mParent, state, extraLayoutSpace);
86 | } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
87 | e.printStackTrace();
88 | BannerUtils.log(e.getMessage());
89 | }
90 | }
91 |
92 | @Override
93 | public boolean requestChildRectangleOnScreen(@NonNull RecyclerView parent,
94 | @NonNull View child, @NonNull Rect rect, boolean immediate,
95 | boolean focusedChildVisible) {
96 | return false; // users should use setCurrentItem instead
97 | }
98 | }
99 |
--------------------------------------------------------------------------------
/bannerview/src/main/java/com/zhpan/bannerview/provider/ViewStyleSetter.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2017 zhpanvip The BannerViewPager Open Source Project
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 | package com.zhpan.bannerview.provider;
17 |
18 | import android.os.Build;
19 | import androidx.annotation.RequiresApi;
20 | import android.view.View;
21 |
22 | /**
23 | *
24 | * Created by zhangpan on 2018/12/26.
25 | * Description:为View设置圆角,支持View及ViewGroup,适用Android 5.1及以上系统。
26 | *
27 | */
28 | public class ViewStyleSetter {
29 |
30 | /**
31 | * 为View设置圆角效果
32 | *
33 | * @param radius 圆角半径
34 | */
35 | @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
36 | public static void applyRoundCorner(View target, float radius) {
37 | if (target == null) {
38 | return;
39 | }
40 | target.setClipToOutline(true);// 用outline裁剪内容区域
41 | target.setOutlineProvider(new RoundViewOutlineProvider(radius));
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/bannerview/src/main/java/com/zhpan/bannerview/transform/OverlapPageTransformer.kt:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2017 zhpanvip The BannerViewPager Open Source Project
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 | package com.zhpan.bannerview.transform
17 |
18 | import android.os.Build
19 | import android.view.View
20 | import androidx.annotation.RequiresApi
21 | import androidx.viewpager2.widget.ViewPager2
22 | import com.zhpan.indicator.utils.IndicatorUtils
23 | import kotlin.math.abs
24 | import kotlin.math.max
25 |
26 | /**
27 | * @param: gives veiwpager2 orientation
28 | * Source from:https://github.com/KoderLabs/finite-cover-flow/blob/dev/lib/src/main/java/com/saeed/finiteflow/lib/OverlapSliderTransformer.kt
29 | */
30 | @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
31 | class OverlapPageTransformer(
32 | private val orientation: Int,
33 | private val minScale: Float = 0f,
34 | private val unSelectedItemRotation: Float = 0f,
35 | private val unSelectedItemAlpha: Float = 0f,
36 | private val itemGap: Float = 0f
37 | ) : ViewPager2.PageTransformer {
38 |
39 | init {
40 | require(minScale in 0f..1f) { "minScale value should be between 1.0 to 0.0" }
41 | require(
42 | unSelectedItemAlpha in 0f..1f
43 | ) { "unSelectedItemAlpha value should be between 1.0 to 0.0" }
44 | }
45 |
46 | private var scalingValue = 0.2f
47 |
48 | override fun transformPage(
49 | page: View,
50 | position: Float
51 | ) {
52 | page.apply {
53 | scalingValue = if (minScale >= 0.8) {
54 | 0.2f
55 | } else if (minScale >= 0.6) {
56 | 0.3f
57 | } else {
58 | 0.4f
59 | }
60 | elevation = -abs(position)
61 | val delta = max(1f - abs(position * (1 - 0.5f)), 0.5f)
62 |
63 | if (unSelectedItemRotation != 0f) {
64 | val rotation =
65 | (1 - delta) * if (position > 0) unSelectedItemRotation else -unSelectedItemRotation
66 |
67 | rotationY = rotation
68 | }
69 |
70 | val scaleDelta = abs(position * scalingValue)
71 | val scale = max(1f - scaleDelta, minScale)
72 |
73 | scaleX = scale
74 | scaleY = scale
75 |
76 | val dp2px = IndicatorUtils.dp2px((itemGap.toInt() / 2).toFloat())
77 | when (orientation) {
78 | ViewPager2.ORIENTATION_HORIZONTAL -> {
79 | translationX =
80 | position * dp2px +
81 | if (position > 0) {
82 | (-width * (1f - scale))
83 | } else {
84 | (width * (1f - scale))
85 | }
86 | }
87 | ViewPager2.ORIENTATION_VERTICAL -> {
88 | translationY = position * dp2px +
89 | if (position > 0) {
90 | (-width * (1f - scale))
91 | } else {
92 | (width * (1f - scale))
93 | }
94 | }
95 | else -> throw IllegalArgumentException(
96 | "Gives correct orientation value, ViewPager2.ORIENTATION_HORIZONTAL or ViewPager2.ORIENTATION_VERTICAL"
97 | )
98 | }
99 |
100 | if (unSelectedItemAlpha != 1f) {
101 | alpha = when {
102 | position >= -1f && position <= 1f -> { // (0, 1]
103 | // page move from right to center.
104 | 0.5f + ((1 - abs(position)) * 0.5f)
105 | }
106 | else -> {
107 | 0.5f / abs(position * position)
108 | }
109 | }
110 | }
111 | }
112 | }
113 | }
--------------------------------------------------------------------------------
/bannerview/src/main/java/com/zhpan/bannerview/transform/ScaleInTransformer.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2017 zhpanvip The BannerViewPager Open Source Project
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 | package com.zhpan.bannerview.transform;
17 |
18 | import android.view.View;
19 |
20 | import androidx.viewpager2.widget.ViewPager2;
21 |
22 | public class ScaleInTransformer implements ViewPager2.PageTransformer {
23 |
24 | private static final float DEFAULT_CENTER = 0.5f;
25 | public static final float DEFAULT_MIN_SCALE = 0.85f;
26 | private final float mMinScale;
27 |
28 | private final boolean mIsRtl;
29 |
30 | public ScaleInTransformer(float minScale, boolean isRtl) {
31 | mMinScale = minScale;
32 | mIsRtl = isRtl;
33 | }
34 |
35 | @Override
36 | public void transformPage(View view, float position) {
37 | if (mIsRtl) {
38 | position = -position;
39 | }
40 | int pageWidth = view.getWidth();
41 | int pageHeight = view.getHeight();
42 |
43 | view.setPivotY(pageHeight / 2f);
44 | view.setPivotX(pageWidth / 2f);
45 | if (position < -1) {
46 | // This page is way off-screen to the left.
47 | view.setScaleX(mMinScale);
48 | view.setScaleY(mMinScale);
49 | view.setPivotX(pageWidth);
50 | } else if (position <= 1) {
51 | // Modify the default slide transition to shrink the page as well
52 | if (position < 0) {
53 | float scaleFactor = (1 + position) * (1 - mMinScale) + mMinScale;
54 | view.setScaleX(scaleFactor);
55 | view.setScaleY(scaleFactor);
56 | view.setPivotX(pageWidth * (DEFAULT_CENTER + (DEFAULT_CENTER * -position)));
57 | } else {
58 | float scaleFactor = (1 - position) * (1 - mMinScale) + mMinScale;
59 | view.setScaleX(scaleFactor);
60 | view.setScaleY(scaleFactor);
61 | view.setPivotX(pageWidth * ((1 - position) * DEFAULT_CENTER));
62 | }
63 | } else {
64 | view.setPivotX(0);
65 | view.setScaleX(mMinScale);
66 | view.setScaleY(mMinScale);
67 | }
68 | }
69 | }
70 |
--------------------------------------------------------------------------------
/bannerview/src/main/java/com/zhpan/bannerview/utils/BannerUtils.java:
--------------------------------------------------------------------------------
1 | /*
2 | Copyright 2017 zhpanvip The BannerViewPager Open Source Project
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 | package com.zhpan.bannerview.utils;
17 |
18 | import android.content.res.Resources;
19 | import android.util.Log;
20 |
21 | import com.zhpan.bannerview.BaseBannerAdapter;
22 |
23 | import static com.zhpan.bannerview.BaseBannerAdapter.MAX_VALUE;
24 |
25 | /**
26 | *
27 | * Created by zhangpan on 2019-08-14.
28 | * Description:
29 | *
30 | */
31 | public class BannerUtils {
32 |
33 | private static boolean debugMode = false;
34 |
35 | private static final String TAG = "BVP";
36 |
37 | public static void setDebugMode(boolean isDebug) {
38 | debugMode = isDebug;
39 | }
40 |
41 | public static boolean isDebugMode() {
42 | return debugMode;
43 | }
44 |
45 | public static int dp2px(float dpValue) {
46 | return (int) (0.5F + dpValue * Resources.getSystem().getDisplayMetrics().density);
47 | }
48 |
49 | public static void log(String tag, String msg) {
50 | if (isDebugMode()) {
51 | Log.e(tag, msg);
52 | }
53 | }
54 |
55 | public static void log(String msg) {
56 | if (isDebugMode()) {
57 | log(TAG, msg);
58 | }
59 | }
60 |
61 | /**
62 | * 在循环模式下{@link com.zhpan.bannerview.BannerViewPager}会初始化一个item为
63 | * {@link BaseBannerAdapter#MAX_VALUE}的ViewPager2,并将当前position设置为ViewPager2
64 | * 的中间位置,因此,此时的position需要通过该方法进行转换为真实的position。
65 | *
66 | * @param position 当前position
67 | * @param pageSize 轮播图页面数
68 | * @return 真实的position
69 | */
70 | public static int getRealPosition(int position, int pageSize) {
71 | if (pageSize == 0) {
72 | return 0;
73 | }
74 | return (position + pageSize) % pageSize;
75 | }
76 |
77 | /**
78 | * @param pageSize 轮播图页面数
79 | * @return 轮播图初始位置
80 | */
81 | public static int getOriginalPosition(int pageSize) {
82 | return MAX_VALUE / 2 - ((MAX_VALUE / 2) % pageSize);
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/bannerview/src/main/res/layout/bvp_layout.xml:
--------------------------------------------------------------------------------
1 |
2 |