19 | * link https://xiaohaibin.github.io/
20 | * email: xhb_199409@163.com
21 | * github: https://github.com/xiaohaibin
22 | * description: XBanner轮播控件的工具类
23 | */
24 | public class XBannerUtils {
25 |
26 | /**
27 | * 设置背景选择器
28 | *
29 | * @param pressedDraw
30 | * @param normalDraw
31 | * @return
32 | */
33 | public static StateListDrawable getSelector(Drawable normalDraw, Drawable pressedDraw) {
34 | StateListDrawable stateListDrawable = new StateListDrawable();
35 | stateListDrawable.addState(new int[]{android.R.attr.state_enabled}, pressedDraw);
36 | stateListDrawable.addState(new int[]{}, normalDraw);
37 | return stateListDrawable;
38 | }
39 |
40 | public static Drawable getDrawable(Context context, @DrawableRes int resId) {
41 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
42 | return context.getDrawable(resId);
43 | } else {
44 | return context.getResources().getDrawable(resId);
45 | }
46 | }
47 |
48 | public static int dp2px(Context context, float dpValue) {
49 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, context.getResources().getDisplayMetrics());
50 | }
51 |
52 | public static int sp2px(Context context, float spValue) {
53 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spValue, context.getResources().getDisplayMetrics());
54 | }
55 |
56 | public static int getScreenWidth(Context context) {
57 | Resources resources = context.getResources();
58 | DisplayMetrics dm = resources.getDisplayMetrics();
59 | return dm.widthPixels;
60 | }
61 |
62 | public static void resetPageTransformer(List extends View> views) {
63 | if (views == null) {
64 | return;
65 | }
66 | for (View view : views) {
67 | view.setVisibility(View.VISIBLE);
68 | ViewCompat.setAlpha(view, 1);
69 | ViewCompat.setPivotX(view, view.getMeasuredWidth() * 0.5f);
70 | ViewCompat.setPivotY(view, view.getMeasuredHeight() * 0.5f);
71 | ViewCompat.setTranslationX(view, 0);
72 | ViewCompat.setTranslationY(view, 0);
73 | ViewCompat.setScaleX(view, 1);
74 | ViewCompat.setScaleY(view, 1);
75 | ViewCompat.setRotationX(view, 0);
76 | ViewCompat.setRotationY(view, 0);
77 | ViewCompat.setRotation(view, 0);
78 | }
79 | }
80 |
81 | public static int getRealPosition(int position,int dataSize) {
82 | return position % dataSize;
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/xbanner/src/main/java/com/stx/xhb/xbanner/XBannerViewPager.java:
--------------------------------------------------------------------------------
1 | package com.stx.xhb.xbanner;
2 |
3 | import android.content.Context;
4 | import android.support.v4.view.VelocityTrackerCompat;
5 | import android.support.v4.view.ViewCompat;
6 | import android.support.v4.view.ViewPager;
7 | import android.util.AttributeSet;
8 | import android.view.MotionEvent;
9 | import android.view.VelocityTracker;
10 |
11 | import java.lang.reflect.Field;
12 | import java.lang.reflect.Method;
13 |
14 |
15 | /**
16 | * author:jxnk25
17 | * email: xhb_199409@163.com
18 | * github: https://github.com/xiaohaibin
19 | * description: 继承ViewPager
20 | *
21 | */
22 | public class XBannerViewPager extends ViewPager {
23 |
24 | private boolean overlapStyle = false;
25 | private boolean mIsAllowUserScroll = true;
26 | private AutoPlayDelegate mAutoPlayDelegate;
27 |
28 | public XBannerViewPager(Context context) {
29 | super(context);
30 | }
31 |
32 | public XBannerViewPager(Context context, AttributeSet attrs) {
33 | super(context, attrs);
34 | }
35 |
36 | @Override
37 | public void setPageTransformer(boolean reverseDrawingOrder, ViewPager.PageTransformer transformer) {
38 | /**
39 | 继承ViewPager,重写setPageTransformer方法,移除版本限制,通过反射设置参数和方法
40 |
41 | getDeclaredMethod*()获取的是【类自身】声明的所有方法,包含public、protected和private方法。
42 | getMethod*()获取的是类的所有共有方法,这就包括自身的所有【public方法】,和从基类继承的、从接口实现的所有【public方法】。
43 |
44 | getDeclaredField获取的是【类自身】声明的所有字段,包含public、protected和private字段。
45 | getField获取的是类的所有共有字段,这就包括自身的所有【public字段】,和从基类继承的、从接口实现的所有【public字段】。
46 | */
47 | Class viewpagerClass = ViewPager.class;
48 |
49 | try {
50 | boolean hasTransformer = transformer != null;
51 |
52 | Field pageTransformerField = viewpagerClass.getDeclaredField("mPageTransformer");
53 | pageTransformerField.setAccessible(true);
54 | PageTransformer mPageTransformer = (PageTransformer) pageTransformerField.get(this);
55 |
56 | boolean needsPopulate = hasTransformer != (mPageTransformer != null);
57 | pageTransformerField.set(this, transformer);
58 |
59 | Method setChildrenDrawingOrderEnabledCompatMethod = viewpagerClass.getDeclaredMethod("setChildrenDrawingOrderEnabledCompat", boolean.class);
60 | setChildrenDrawingOrderEnabledCompatMethod.setAccessible(true);
61 | setChildrenDrawingOrderEnabledCompatMethod.invoke(this, hasTransformer);
62 |
63 | Field drawingOrderField = viewpagerClass.getDeclaredField("mDrawingOrder");
64 | drawingOrderField.setAccessible(true);
65 | if (hasTransformer) {
66 | drawingOrderField.setInt(this, reverseDrawingOrder ? 2 : 1);
67 | } else {
68 | drawingOrderField.setInt(this, 0);
69 | }
70 |
71 | if (needsPopulate) {
72 | Method populateMethod = viewpagerClass.getDeclaredMethod("populate");
73 | populateMethod.setAccessible(true);
74 | populateMethod.invoke(this);
75 | }
76 | } catch (Exception e) {
77 | }
78 | }
79 |
80 |
81 | /**
82 | * 设置ViewPager的滚动速度
83 | *
84 | * @param duration page切换的时间长度
85 | */
86 | public void setScrollDuration(int duration) {
87 | try {
88 | Field scrollerField = ViewPager.class.getDeclaredField("mScroller");
89 | scrollerField.setAccessible(true);
90 | scrollerField.set(this, new XBannerScroller(getContext(), duration));
91 | } catch (Exception e) {
92 | e.printStackTrace();
93 | }
94 | }
95 |
96 | /**
97 | * 切换到指定索引的页面,主要用于自动轮播
98 | *
99 | * @param position
100 | */
101 | public void setBannerCurrentItemInternal(int position, boolean smoothScroll) {
102 | Class viewpagerClass = ViewPager.class;
103 | try {
104 | Method setCurrentItemInternalMethod = viewpagerClass.getDeclaredMethod("setCurrentItemInternal", int.class, boolean.class, boolean.class);
105 | setCurrentItemInternalMethod.setAccessible(true);
106 | setCurrentItemInternalMethod.invoke(this, position, smoothScroll, true);
107 | ViewCompat.postInvalidateOnAnimation(this);
108 | } catch (Exception e) {
109 | }
110 | }
111 |
112 | /**
113 | * 设置是否允许用户手指滑动
114 | *
115 | * @param iSallowUserScroll
116 | */
117 | public void setIsAllowUserScroll(boolean iSallowUserScroll) {
118 | mIsAllowUserScroll = iSallowUserScroll;
119 | }
120 |
121 | @Override
122 | public boolean onInterceptTouchEvent(MotionEvent ev) {
123 | return mIsAllowUserScroll && super.onInterceptTouchEvent(ev);
124 | }
125 |
126 | @Override
127 | public boolean onTouchEvent(MotionEvent ev) {
128 | if (mIsAllowUserScroll) {
129 | if (mAutoPlayDelegate != null && (ev.getAction() == MotionEvent.ACTION_CANCEL || ev.getAction() == MotionEvent.ACTION_UP)) {
130 | mAutoPlayDelegate.handleAutoPlayActionUpOrCancel(getXVelocity());
131 | return false;
132 | } else {
133 | return super.onTouchEvent(ev);
134 | }
135 | } else {
136 | return false;
137 | }
138 | }
139 |
140 | @Override
141 | protected int getChildDrawingOrder(int childCount, int i) {
142 | if (overlapStyle) {
143 | if(i == childCount - 1) {
144 | return getCurrentItem();
145 | } else {
146 | return i >= getCurrentItem()? i + 1 : i;
147 | }
148 | } else {
149 | return super.getChildDrawingOrder(childCount, i);
150 | }
151 | }
152 |
153 | private float getXVelocity() {
154 | float xVelocity = 0;
155 | Class viewpagerClass = ViewPager.class;
156 | try {
157 | Field velocityTrackerField = viewpagerClass.getDeclaredField("mVelocityTracker");
158 | velocityTrackerField.setAccessible(true);
159 | VelocityTracker velocityTracker = (VelocityTracker) velocityTrackerField.get(this);
160 |
161 | Field activePointerIdField = viewpagerClass.getDeclaredField("mActivePointerId");
162 | activePointerIdField.setAccessible(true);
163 |
164 | Field maximumVelocityField = viewpagerClass.getDeclaredField("mMaximumVelocity");
165 | maximumVelocityField.setAccessible(true);
166 | int maximumVelocity = maximumVelocityField.getInt(this);
167 |
168 | velocityTracker.computeCurrentVelocity(1000, maximumVelocity);
169 | xVelocity = VelocityTrackerCompat.getXVelocity(velocityTracker, activePointerIdField.getInt(this));
170 | } catch (Exception ignored) {
171 | }
172 | return xVelocity;
173 | }
174 |
175 | public void setOverlapStyle(boolean overlapStyle) {
176 | this.overlapStyle = overlapStyle;
177 | }
178 |
179 | public void setAutoPlayDelegate(AutoPlayDelegate autoPlayDelegate) {
180 | mAutoPlayDelegate = autoPlayDelegate;
181 | }
182 |
183 | public interface AutoPlayDelegate {
184 | void handleAutoPlayActionUpOrCancel(float xVelocity);
185 | }
186 | }
--------------------------------------------------------------------------------
/xbanner/src/main/java/com/stx/xhb/xbanner/entity/BaseBannerInfo.java:
--------------------------------------------------------------------------------
1 | package com.stx.xhb.xbanner.entity;
2 |
3 | /**
4 | * author: xiaohaibin.
5 | * time: 2018/12/3
6 | * mail:xhb_199409@163.com
7 | * github:https://github.com/xiaohaibin
8 | * describe: BaseBannerInfo
9 | */
10 | public interface BaseBannerInfo {
11 | Object getXBannerUrl();
12 |
13 | String getXBannerTitle();
14 | }
15 |
--------------------------------------------------------------------------------
/xbanner/src/main/java/com/stx/xhb/xbanner/entity/LocalImageInfo.java:
--------------------------------------------------------------------------------
1 | package com.stx.xhb.xbanner.entity;
2 |
3 |
4 | import android.support.annotation.DrawableRes;
5 |
6 | /**
7 | * author: xiaohaibin.
8 | * time: 2018/12/3
9 | * mail:xhb_199409@163.com
10 | * github:https://github.com/xiaohaibin
11 | * describe: 本地资源图片
12 | */
13 | public class LocalImageInfo extends SimpleBannerInfo {
14 |
15 | @DrawableRes
16 | private int bannerRes;
17 |
18 | public LocalImageInfo(int bannerRes) {
19 | this.bannerRes = bannerRes;
20 | }
21 |
22 | @Override
23 | public Integer getXBannerUrl() {
24 | return bannerRes;
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/xbanner/src/main/java/com/stx/xhb/xbanner/entity/SimpleBannerInfo.java:
--------------------------------------------------------------------------------
1 | package com.stx.xhb.xbanner.entity;
2 |
3 | /**
4 | * author: xiaohaibin.
5 | * time: 2018/11/28
6 | * mail:xhb_199409@163.com
7 | * github:https://github.com/xiaohaibin
8 | * describe: SimpleBannerInfo
9 | */
10 | public abstract class SimpleBannerInfo implements BaseBannerInfo {
11 | @Override
12 | public String getXBannerTitle() {
13 | return null;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/xbanner/src/main/java/com/stx/xhb/xbanner/holder/HolderCreator.java:
--------------------------------------------------------------------------------
1 | package com.stx.xhb.xbanner.holder;
2 |
3 |
4 |
5 | public interface HolderCreator
8 | * link https://xiaohaibin.github.io/
9 | * email: xhb_199409@163.com
10 | * github: https://github.com/xiaohaibin
11 | * description:AccordionPageTransformer
12 | */
13 | public class AccordionPageTransformer extends BasePageTransformer {
14 |
15 | @Override
16 | public void handleInvisiblePage(View view, float position) {
17 | }
18 |
19 | @Override
20 | public void handleLeftPage(View view, float position) {
21 | view.setPivotX(view.getWidth());
22 | view.setScaleX(1.0f + position);
23 | }
24 |
25 | @Override
26 | public void handleRightPage(View view, float position) {
27 | view.setPivotX(0);
28 | view.setScaleX(1.0f - position);
29 | view.setAlpha(1);
30 | }
31 |
32 | }
--------------------------------------------------------------------------------
/xbanner/src/main/java/com/stx/xhb/xbanner/transformers/AlphaPageTransformer.java:
--------------------------------------------------------------------------------
1 | package com.stx.xhb.xbanner.transformers;
2 |
3 | import android.support.v4.view.ViewCompat;
4 | import android.view.View;
5 |
6 |
7 | /**
8 | * Created by jxnk25 on 2016/10/18.
9 | *
10 | * link https://xiaohaibin.github.io/
11 | * email: xhb_199409@163.com
12 | * github: https://github.com/xiaohaibin
13 | * description:AlphaPageTransformer
14 | */
15 | public class AlphaPageTransformer extends BasePageTransformer {
16 | private float mMinScale = 0.4f;
17 |
18 | public AlphaPageTransformer() {
19 | }
20 |
21 | public AlphaPageTransformer(float minScale) {
22 | setMinScale(minScale);
23 | }
24 |
25 | @Override
26 | public void handleInvisiblePage(View view, float position) {
27 | ViewCompat.setAlpha(view, 0);
28 | }
29 |
30 | @Override
31 | public void handleLeftPage(View view, float position) {
32 | view.setAlpha(mMinScale + (1 - mMinScale) * (1 + position));
33 | }
34 |
35 | @Override
36 | public void handleRightPage(View view, float position) {
37 | view.setAlpha(mMinScale + (1 - mMinScale) * (1 - position));
38 | }
39 |
40 | public void setMinScale(float minScale) {
41 | if (minScale >= 0.0f && minScale <= 1.0f) {
42 | mMinScale = minScale;
43 | }
44 | }
45 | }
--------------------------------------------------------------------------------
/xbanner/src/main/java/com/stx/xhb/xbanner/transformers/BasePageTransformer.java:
--------------------------------------------------------------------------------
1 | package com.stx.xhb.xbanner.transformers;
2 |
3 | import android.support.v4.view.ViewPager;
4 | import android.view.View;
5 |
6 |
7 | /**
8 | * Created by jxnk25 on 2016/10/18.
9 | *
10 | * link https://xiaohaibin.github.io/
11 | * email: xhb_199409@163.com
12 | * github: https://github.com/xiaohaibin
13 | * description:
14 | */
15 | public abstract class BasePageTransformer implements ViewPager.PageTransformer {
16 |
17 | @Override
18 | public void transformPage(View view, float position) {
19 | ViewPager viewPager;
20 | if (view.getParent() instanceof ViewPager) {
21 | viewPager = (ViewPager) view.getParent();
22 | } else {
23 | return;
24 | }
25 | position = getRealPosition(viewPager, view);
26 | if (position < -1.0f) {
27 | handleInvisiblePage(view, position);
28 | } else if (position <= 0.0f) {
29 | handleLeftPage(view, position);
30 | } else if (position <= 1.0f) {
31 | handleRightPage(view, position);
32 | } else {
33 | handleInvisiblePage(view, position);
34 | }
35 | }
36 |
37 | /**
38 | * 重新计算position
39 | *
40 | * @param viewPager
41 | * @param page
42 | * @return
43 | */
44 | private float getRealPosition(ViewPager viewPager, View page) {
45 | int width = viewPager.getMeasuredWidth() - viewPager.getPaddingLeft() - viewPager.getPaddingRight();
46 | return (float) (page.getLeft() - viewPager.getScrollX() - viewPager.getPaddingLeft()) / width;
47 | }
48 |
49 | public abstract void handleInvisiblePage(View view, float position);
50 |
51 | public abstract void handleLeftPage(View view, float position);
52 |
53 | public abstract void handleRightPage(View view, float position);
54 |
55 | public static BasePageTransformer getPageTransformer(Transformer effect) {
56 | switch (effect) {
57 | case Alpha:
58 | return new AlphaPageTransformer();
59 | case Rotate:
60 | return new RotatePageTransformer();
61 | case Cube:
62 | return new CubePageTransformer();
63 | case Flip:
64 | return new FlipPageTransformer();
65 | case Accordion:
66 | return new AccordionPageTransformer();
67 | case ZoomFade:
68 | return new ZoomFadePageTransformer();
69 | case ZoomCenter:
70 | return new ZoomCenterPageTransformer();
71 | case ZoomStack:
72 | return new ZoomStackPageTransformer();
73 | case Stack:
74 | return new StackPageTransformer();
75 | case Depth:
76 | return new DepthPageTransformer();
77 | case Zoom:
78 | return new ZoomPageTransformer();
79 | case Scale:
80 | return new ScalePageTransformer();
81 | case OverLap:
82 | return new OverLapPageTransformer();
83 | default:
84 | return new DefaultPageTransformer();
85 | }
86 | }
87 | }
--------------------------------------------------------------------------------
/xbanner/src/main/java/com/stx/xhb/xbanner/transformers/CubePageTransformer.java:
--------------------------------------------------------------------------------
1 | package com.stx.xhb.xbanner.transformers;
2 |
3 | import android.view.View;
4 |
5 | /**
6 | * Created by jxnk25 on 2016/10/18.
7 | *
8 | * link https://xiaohaibin.github.io/
9 | * email: xhb_199409@163.com
10 | * github: https://github.com/xiaohaibin
11 | * description:CubePageTransformer
12 | */
13 | public class CubePageTransformer extends BasePageTransformer {
14 | private float mMaxRotation = 90.0f;
15 |
16 | public CubePageTransformer() {
17 | }
18 |
19 | public CubePageTransformer(float maxRotation) {
20 | setMaxRotation(maxRotation);
21 | }
22 |
23 | @Override
24 | public void handleInvisiblePage(View view, float position) {
25 | view.setPivotX(view.getMeasuredWidth());
26 | view.setPivotY( view.getMeasuredHeight() * 0.5f);
27 | view.setRotationY(0);
28 | }
29 |
30 | @Override
31 | public void handleLeftPage(View view, float position) {
32 | view.setPivotX(view.getMeasuredWidth());
33 | view.setPivotY( view.getMeasuredHeight() * 0.5f);
34 | view.setRotationY(mMaxRotation * position);
35 | }
36 |
37 | @Override
38 | public void handleRightPage(View view, float position) {
39 | view.setPivotX( 0);
40 | view.setPivotY(view.getMeasuredHeight() * 0.5f);
41 | view.setRotationY( mMaxRotation * position);
42 | }
43 |
44 | public void setMaxRotation(float maxRotation) {
45 | if (maxRotation >= 0.0f && maxRotation <= 90.0f) {
46 | mMaxRotation = maxRotation;
47 | }
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/xbanner/src/main/java/com/stx/xhb/xbanner/transformers/DefaultPageTransformer.java:
--------------------------------------------------------------------------------
1 | package com.stx.xhb.xbanner.transformers;
2 |
3 | import android.view.View;
4 |
5 | /**
6 | * Created by jxnk25 on 2016/10/18.
7 | *
8 | * link https://xiaohaibin.github.io/
9 | * email: xhb_199409@163.com
10 | * github: https://github.com/xiaohaibin
11 | * description:DefaultPageTransformer
12 | */
13 | public class DefaultPageTransformer extends BasePageTransformer {
14 |
15 | @Override
16 | public void handleInvisiblePage(View view, float position) {
17 | }
18 |
19 | @Override
20 | public void handleLeftPage(View view, float position) {
21 | }
22 |
23 | @Override
24 | public void handleRightPage(View view, float position) {
25 | }
26 |
27 | }
--------------------------------------------------------------------------------
/xbanner/src/main/java/com/stx/xhb/xbanner/transformers/DepthPageTransformer.java:
--------------------------------------------------------------------------------
1 | package com.stx.xhb.xbanner.transformers;
2 |
3 | import android.support.v4.view.ViewCompat;
4 | import android.view.View;
5 |
6 |
7 | /**
8 | * Created by jxnk25 on 2016/10/18.
9 | *
10 | * link https://xiaohaibin.github.io/
11 | * email: xhb_199409@163.com
12 | * github: https://github.com/xiaohaibin
13 | * description:
14 | */
15 | public class DepthPageTransformer extends BasePageTransformer {
16 | private float mMinScale = 0.8f;
17 |
18 | public DepthPageTransformer() {
19 | }
20 |
21 | public DepthPageTransformer(float minScale) {
22 | setMinScale(minScale);
23 | }
24 |
25 | @Override
26 | public void handleInvisiblePage(View view, float position) {
27 | ViewCompat.setAlpha(view, 0);
28 | }
29 |
30 | @Override
31 | public void handleLeftPage(View view, float position) {
32 | view.setAlpha( 1);
33 | view.setTranslationX( 0);
34 | view.setScaleX( 1);
35 | view.setScaleY(1);
36 | }
37 |
38 | @Override
39 | public void handleRightPage(View view, float position) {
40 | view.setAlpha( 1 - position);
41 | view.setTranslationX( -view.getWidth() * position);
42 | float scale = mMinScale + (1 - mMinScale) * (1 - position);
43 | view.setScaleX(scale);
44 | view.setScaleY( scale);
45 | }
46 |
47 | public void setMinScale(float minScale) {
48 | if (minScale >= 0.6f && minScale <= 1.0f) {
49 | mMinScale = minScale;
50 | }
51 | }
52 |
53 | }
--------------------------------------------------------------------------------
/xbanner/src/main/java/com/stx/xhb/xbanner/transformers/FlipPageTransformer.java:
--------------------------------------------------------------------------------
1 | package com.stx.xhb.xbanner.transformers;
2 |
3 | import android.view.View;
4 |
5 | /**
6 | * Created by jxnk25 on 2016/10/18.
7 | *
8 | * link https://xiaohaibin.github.io/
9 | * email: xhb_199409@163.com
10 | * github: https://github.com/xiaohaibin
11 | * description:
12 | */
13 | public class FlipPageTransformer extends BasePageTransformer {
14 | private static final float ROTATION = 180.0f;
15 |
16 | @Override
17 | public void handleInvisiblePage(View view, float position) {
18 | }
19 |
20 | @Override
21 | public void handleLeftPage(View view, float position) {
22 | view.setTranslationX(-view.getWidth() * position);
23 | float rotation = (ROTATION * position);
24 | view.setRotationY(rotation);
25 |
26 | if (position > -0.5) {
27 | view.setVisibility(View.VISIBLE);
28 | } else {
29 | view.setVisibility(View.INVISIBLE);
30 | }
31 | }
32 |
33 | @Override
34 | public void handleRightPage(View view, float position) {
35 | view.setTranslationX(-view.getWidth() * position);
36 | float rotation = (ROTATION * position);
37 | view.setRotationY(rotation);
38 |
39 | if (position < 0.5) {
40 | view.setVisibility(View.VISIBLE);
41 | } else {
42 | view.setVisibility(View.INVISIBLE);
43 | }
44 | }
45 |
46 | }
--------------------------------------------------------------------------------
/xbanner/src/main/java/com/stx/xhb/xbanner/transformers/OverLapPageTransformer.java:
--------------------------------------------------------------------------------
1 | package com.stx.xhb.xbanner.transformers;
2 |
3 | import android.support.v4.view.ViewCompat;
4 | import android.view.View;
5 |
6 | /**
7 | * author: xiaohaibin.
8 | * time: 2018/10/9
9 | * mail:xhb_199409@163.com
10 | * github:https://github.com/xiaohaibin
11 | * describe: 适用于一屏显示多个模式
12 | */
13 | public class OverLapPageTransformer extends BasePageTransformer {
14 |
15 | private float scaleValue = 0.8F;
16 | private float alphaValue = 1f;
17 |
18 | public OverLapPageTransformer() {
19 | }
20 |
21 | public OverLapPageTransformer(float scaleValue, float alphaValue) {
22 | this.scaleValue = scaleValue;
23 | this.alphaValue = alphaValue;
24 | }
25 |
26 | @Override
27 | public void handleInvisiblePage(View view, float position) {
28 | view.setAlpha(1);
29 | view.setScaleX(scaleValue);
30 | view.setScaleY(scaleValue);
31 | }
32 |
33 | @Override
34 | public void handleLeftPage(View view, float position) {
35 | view.setAlpha(1 + position * (1 - alphaValue));
36 | float scale = Math.max(scaleValue, 1 - Math.abs(position));
37 | view.setScaleX(scale);
38 | view.setScaleY(scale);
39 | ViewCompat.setTranslationZ(view, position);
40 | }
41 |
42 | @Override
43 | public void handleRightPage(View view, float position) {
44 | view.setAlpha(1 - position * (1 - alphaValue));
45 | float scale = Math.max(scaleValue, 1 - Math.abs(position));
46 | view.setScaleX(scale);
47 | view.setScaleY(scale);
48 | ViewCompat.setTranslationZ(view, -position);
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/xbanner/src/main/java/com/stx/xhb/xbanner/transformers/RotatePageTransformer.java:
--------------------------------------------------------------------------------
1 | package com.stx.xhb.xbanner.transformers;
2 |
3 | import android.view.View;
4 |
5 | /**
6 | * Created by jxnk25 on 2016/10/18.
7 | *
8 | * link https://xiaohaibin.github.io/
9 | * email: xhb_199409@163.com
10 | * github: https://github.com/xiaohaibin
11 | * description:
12 | */
13 | public class RotatePageTransformer extends BasePageTransformer {
14 | private float mMaxRotation = 15.0f;
15 |
16 | public RotatePageTransformer() {
17 | }
18 |
19 | public RotatePageTransformer(float maxRotation) {
20 | setMaxRotation(maxRotation);
21 | }
22 |
23 | @Override
24 | public void handleInvisiblePage(View view, float position) {
25 | view.setPivotX(view.getMeasuredWidth() * 0.5f);
26 | view.setPivotY(view.getMeasuredHeight());
27 | view.setRotation( 0);
28 | }
29 |
30 | @Override
31 | public void handleLeftPage(View view, float position) {
32 | float rotation = (mMaxRotation * position);
33 | view.setPivotX( view.getMeasuredWidth() * 0.5f);
34 | view.setPivotY(view.getMeasuredHeight());
35 | view.setRotation( rotation);
36 | }
37 |
38 | @Override
39 | public void handleRightPage(View view, float position) {
40 | handleLeftPage(view, position);
41 | }
42 |
43 | public void setMaxRotation(float maxRotation) {
44 | if (maxRotation >= 0.0f && maxRotation <= 40.0f) {
45 | mMaxRotation = maxRotation;
46 | }
47 | }
48 | }
--------------------------------------------------------------------------------
/xbanner/src/main/java/com/stx/xhb/xbanner/transformers/ScalePageTransformer.java:
--------------------------------------------------------------------------------
1 | package com.stx.xhb.xbanner.transformers;
2 |
3 | import android.view.View;
4 |
5 | /**
6 | * author: xiaohaibin.
7 | * time: 2018/10/9
8 | * mail:xhb_199409@163.com
9 | * github:https://github.com/xiaohaibin
10 | * describe: 适用于一屏显示多个模式
11 | */
12 | public class ScalePageTransformer extends BasePageTransformer {
13 | private static final float MIN_SCALE = 0.8F;
14 |
15 | @Override
16 | public void handleInvisiblePage(View view, float position) {
17 | view.setScaleY(MIN_SCALE);
18 | }
19 |
20 | @Override
21 | public void handleLeftPage(View view, float position) {
22 | float scale = Math.max(MIN_SCALE, 1 - Math.abs(position));
23 | view.setScaleY(scale);
24 | }
25 |
26 | @Override
27 | public void handleRightPage(View view, float position) {
28 | float scale = Math.max(MIN_SCALE, 1 - Math.abs(position));
29 | view.setScaleY(scale);
30 | }
31 | }
32 |
--------------------------------------------------------------------------------
/xbanner/src/main/java/com/stx/xhb/xbanner/transformers/StackPageTransformer.java:
--------------------------------------------------------------------------------
1 | package com.stx.xhb.xbanner.transformers;
2 |
3 | import android.view.View;
4 |
5 | /**
6 | * Created by jxnk25 on 2016/10/18.
7 | *
8 | * link https://xiaohaibin.github.io/
9 | * email: xhb_199409@163.com
10 | * github: https://github.com/xiaohaibin
11 | * description:
12 | */
13 | public class StackPageTransformer extends BasePageTransformer {
14 |
15 | @Override
16 | public void handleInvisiblePage(View view, float position) {
17 | }
18 |
19 | @Override
20 | public void handleLeftPage(View view, float position) {
21 | }
22 |
23 | @Override
24 | public void handleRightPage(View view, float position) {
25 | view.setTranslationX(-view.getWidth() * position);
26 | }
27 |
28 | }
--------------------------------------------------------------------------------
/xbanner/src/main/java/com/stx/xhb/xbanner/transformers/Transformer.java:
--------------------------------------------------------------------------------
1 | package com.stx.xhb.xbanner.transformers;
2 |
3 | /**
4 | * Created by jxnk25 on 2016/10/18.
5 | * link https://xiaohaibin.github.io/
6 | * email: xhb_199409@163.com
7 | * github: https://github.com/xiaohaibin
8 | * description:
9 | */
10 | public enum Transformer {
11 | Default,
12 | Alpha,
13 | Rotate,
14 | Cube,
15 | Flip,
16 | Accordion,
17 | ZoomFade,
18 | ZoomCenter,
19 | ZoomStack,
20 | Stack,
21 | Depth,
22 | Zoom,
23 | Scale,
24 | OverLap
25 | }
26 |
--------------------------------------------------------------------------------
/xbanner/src/main/java/com/stx/xhb/xbanner/transformers/ZoomCenterPageTransformer.java:
--------------------------------------------------------------------------------
1 | package com.stx.xhb.xbanner.transformers;
2 |
3 | import android.support.v4.view.ViewCompat;
4 | import android.view.View;
5 |
6 |
7 | /**
8 | * Created by jxnk25 on 2016/10/18.
9 | *
10 | * link https://xiaohaibin.github.io/
11 | * email: xhb_199409@163.com
12 | * github: https://github.com/xiaohaibin
13 | * description:
14 | */
15 | public class ZoomCenterPageTransformer extends BasePageTransformer {
16 |
17 | @Override
18 | public void handleInvisiblePage(View view, float position) {
19 | }
20 |
21 | @Override
22 | public void handleLeftPage(View view, float position) {
23 | view.setTranslationX(-view.getWidth() * position);
24 | view.setPivotX(view.getWidth() * 0.5f);
25 | view.setPivotY(view.getHeight() * 0.5f);
26 | view.setScaleX(1 + position);
27 | view.setScaleY(1 + position);
28 |
29 | if (position < -0.95f) {
30 | view.setAlpha(0);
31 | } else {
32 | view.setAlpha(1);
33 | }
34 | }
35 |
36 | @Override
37 | public void handleRightPage(View view, float position) {
38 | view.setTranslationX(-view.getWidth() * position);
39 | view.setPivotX(view.getWidth() * 0.5f);
40 | view.setPivotY(view.getHeight() * 0.5f);
41 | view.setScaleX(1 - position);
42 | view.setScaleY(1 - position);
43 | if (position > 0.95f) {
44 | ViewCompat.setAlpha(view, 0);
45 | } else {
46 | ViewCompat.setAlpha(view, 1);
47 | }
48 | }
49 |
50 | }
--------------------------------------------------------------------------------
/xbanner/src/main/java/com/stx/xhb/xbanner/transformers/ZoomFadePageTransformer.java:
--------------------------------------------------------------------------------
1 | package com.stx.xhb.xbanner.transformers;
2 |
3 | import android.view.View;
4 |
5 | /**
6 | * Created by jxnk25 on 2016/10/18.
7 | *
8 | * link https://xiaohaibin.github.io/
9 | * email: xhb_199409@163.com
10 | * github: https://github.com/xiaohaibin
11 | * description:
12 | */
13 | public class ZoomFadePageTransformer extends BasePageTransformer {
14 |
15 | @Override
16 | public void handleInvisiblePage(View view, float position) {
17 | }
18 |
19 | @Override
20 | public void handleLeftPage(View view, float position) {
21 | view.setTranslationX(-view.getWidth() * position);
22 | view.setPivotX(view.getWidth() * 0.5f);
23 | view.setPivotY(view.getHeight() * 0.5f);
24 | view.setScaleX(1 + position);
25 | view.setScaleY(1 + position);
26 | view.setAlpha(1 + position);
27 | }
28 |
29 | @Override
30 | public void handleRightPage(View view, float position) {
31 | view.setTranslationX(-view.getWidth() * position);
32 | view.setPivotX(view.getWidth() * 0.5f);
33 | view.setPivotY(view.getHeight() * 0.5f);
34 | view.setScaleX(1 - position);
35 | view.setScaleY(1 - position);
36 | view.setAlpha(1 - position);
37 | }
38 |
39 | }
--------------------------------------------------------------------------------
/xbanner/src/main/java/com/stx/xhb/xbanner/transformers/ZoomPageTransformer.java:
--------------------------------------------------------------------------------
1 | package com.stx.xhb.xbanner.transformers;
2 |
3 | import android.support.v4.view.ViewCompat;
4 | import android.view.View;
5 |
6 |
7 | /**
8 | * Created by jxnk25 on 2016/10/18.
9 | *
10 | * link https://xiaohaibin.github.io/
11 | * email: xhb_199409@163.com
12 | * github: https://github.com/xiaohaibin
13 | * description:
14 | */
15 | public class ZoomPageTransformer extends BasePageTransformer {
16 | private float mMinScale = 0.85f;
17 | private float mMinAlpha = 0.65f;
18 |
19 | public ZoomPageTransformer() {
20 | }
21 |
22 | public ZoomPageTransformer(float minAlpha, float minScale) {
23 | setMinAlpha(minAlpha);
24 | setMinScale(minScale);
25 | }
26 |
27 | @Override
28 | public void handleInvisiblePage(View view, float position) {
29 | ViewCompat.setAlpha(view, 0);
30 | }
31 |
32 | @Override
33 | public void handleLeftPage(View view, float position) {
34 | float scale = Math.max(mMinScale, 1 + position);
35 | float vertMargin = view.getHeight() * (1 - scale) / 2;
36 | float horzMargin = view.getWidth() * (1 - scale) / 2;
37 | view.setTranslationX(horzMargin - vertMargin / 2);
38 | view.setScaleX(scale);
39 | view.setScaleY( scale);
40 | view.setAlpha( mMinAlpha + (scale - mMinScale) / (1 - mMinScale) * (1 - mMinAlpha));
41 | }
42 |
43 | @Override
44 | public void handleRightPage(View view, float position) {
45 | float scale = Math.max(mMinScale, 1 - position);
46 | float vertMargin = view.getHeight() * (1 - scale) / 2;
47 | float horzMargin = view.getWidth() * (1 - scale) / 2;
48 | view.setTranslationX( -horzMargin + vertMargin / 2);
49 | view.setScaleX( scale);
50 | view.setScaleY( scale);
51 | view.setAlpha( mMinAlpha + (scale - mMinScale) / (1 - mMinScale) * (1 - mMinAlpha));
52 | }
53 |
54 | public void setMinAlpha(float minAlpha) {
55 | if (minAlpha >= 0.6f && minAlpha <= 1.0f) {
56 | mMinAlpha = minAlpha;
57 | }
58 | }
59 |
60 | public void setMinScale(float minScale) {
61 | if (minScale >= 0.6f && minScale <= 1.0f) {
62 | mMinScale = minScale;
63 | }
64 | }
65 | }
--------------------------------------------------------------------------------
/xbanner/src/main/java/com/stx/xhb/xbanner/transformers/ZoomStackPageTransformer.java:
--------------------------------------------------------------------------------
1 | package com.stx.xhb.xbanner.transformers;
2 |
3 | import android.view.View;
4 |
5 | /**
6 | * Created by jxnk25 on 2016/10/18.
7 | *
8 | * link https://xiaohaibin.github.io/
9 | * email: xhb_199409@163.com
10 | * github: https://github.com/xiaohaibin
11 | * description:
12 | */
13 | public class ZoomStackPageTransformer extends BasePageTransformer {
14 |
15 | @Override
16 | public void handleInvisiblePage(View view, float position) {
17 | }
18 |
19 | @Override
20 | public void handleLeftPage(View view, float position) {
21 | view.setTranslationX(-view.getWidth() * position);
22 | view.setPivotX(view.getWidth() * 0.5f);
23 | view.setPivotY(view.getHeight() * 0.5f);
24 | view.setScaleX(1 + position);
25 | view.setScaleY(1 + position);
26 |
27 | if (position < -0.95f) {
28 | view.setAlpha(0);
29 | } else {
30 | view.setAlpha(1);
31 | }
32 | }
33 |
34 | @Override
35 | public void handleRightPage(View view, float position) {
36 | view.setTranslationX(-view.getWidth() * position);
37 | view.setPivotX(view.getWidth() * 0.5f);
38 | view.setPivotY(view.getHeight() * 0.5f);
39 | view.setScaleX(1 + position);
40 | view.setScaleY(1 + position);
41 |
42 | if (position > 0.95f) {
43 | view.setAlpha(0);
44 | } else {
45 | view.setAlpha(1);
46 | }
47 | }
48 |
49 | }
--------------------------------------------------------------------------------
/xbanner/src/main/res/drawable/shape_point_normal.xml:
--------------------------------------------------------------------------------
1 |
2 |