ソースを参照

Optimize cod for issue #202,setRoundCorner(int,int, int,int) supported.

zhpanvip 4 年 前
コミット
d8fa38df08

+ 1 - 1
app/src/main/java/com/example/zhpan/banner/fragment/OthersFragment.java

@@ -73,7 +73,7 @@ public class OthersFragment extends BaseFragment implements View.OnClickListener
         mViewPager.setIndicatorSliderGap(BannerUtils.dp2px(6))
                 .setIndicatorView(mIndicatorView)
                 .setLifecycleRegistry(getLifecycle())
-                .setRoundCorner(BannerUtils.dp2px(6))
+                .setRoundCorner(BannerUtils.dp2px(36), 0, 0, BannerUtils.dp2px(36))
                 .setOnPageClickListener((clickedView, position) -> {
                     ToastUtils.showShort("position:" + position);
                     int currentItem = mViewPager.getCurrentItem();

+ 32 - 10
bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java

@@ -1,7 +1,9 @@
 package com.zhpan.bannerview;
 
 import android.content.Context;
-import android.os.Build;
+import android.graphics.Canvas;
+import android.graphics.Path;
+import android.graphics.RectF;
 import android.os.Bundle;
 import android.os.Handler;
 import android.os.Parcelable;
@@ -26,7 +28,6 @@ import com.zhpan.bannerview.constants.PageStyle;
 import com.zhpan.bannerview.manager.BannerManager;
 import com.zhpan.bannerview.manager.BannerOptions;
 import com.zhpan.bannerview.provider.ReflectLayoutManager;
-import com.zhpan.bannerview.provider.ViewStyleSetter;
 import com.zhpan.bannerview.utils.BannerUtils;
 import com.zhpan.indicator.IndicatorView;
 import com.zhpan.indicator.annotation.AIndicatorSlideMode;
@@ -81,6 +82,10 @@ public class BannerViewPager<T> extends RelativeLayout implements LifecycleObser
         }
     };
 
+    private RectF mRadiusRectF;
+
+    private Path mRadiusPath;
+
     private int startX, startY;
 
     private final ViewPager2.OnPageChangeCallback mOnPageChangeCallback = new ViewPager2.OnPageChangeCallback() {
@@ -286,7 +291,6 @@ public class BannerViewPager<T> extends RelativeLayout implements LifecycleObser
         if (list != null) {
             setIndicatorValues(list);
             setupViewPager(list);
-            initRoundCorner();
         }
     }
 
@@ -342,11 +346,15 @@ public class BannerViewPager<T> extends RelativeLayout implements LifecycleObser
         }
     }
 
-    private void initRoundCorner() {
-        int roundCorner = mBannerManager.getBannerOptions().getRoundRectRadius();
-        if (roundCorner > 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
-            ViewStyleSetter.applyRoundCorner(this, roundCorner);
+    @Override
+    protected void dispatchDraw(Canvas canvas) {
+        if (mRadiusRectF != null && mRadiusPath != null) {
+            mRadiusRectF.right = this.getWidth();
+            mRadiusRectF.bottom = this.getHeight();
+            mRadiusPath.addRoundRect(mRadiusRectF, mBannerManager.getBannerOptions().getRoundRectRadius(), Path.Direction.CW);
+            canvas.clipPath(mRadiusPath);
         }
+        super.dispatchDraw(canvas);
     }
 
     private void setupViewPager(List<T> list) {
@@ -498,13 +506,27 @@ public class BannerViewPager<T> extends RelativeLayout implements LifecycleObser
 
     /**
      * Set round rectangle effect for BannerViewPager.
-     * <p>
-     * Require SDK_INT>=LOLLIPOP(API 21)
      *
      * @param radius round radius
      */
     public BannerViewPager<T> setRoundCorner(int radius) {
-        mBannerManager.getBannerOptions().setRoundRectRadius(radius);
+        setRoundCorner(radius, radius, radius, radius);
+        return this;
+    }
+
+    /**
+     * Set round rectangle effect for BannerViewPager.
+     *
+     * @param topLeftRadius     top left round radius
+     * @param topRightRadius    top right round radius
+     * @param bottomLeftRadius  bottom left round radius
+     * @param bottomRightRadius bottom right round radius
+     */
+    public BannerViewPager<T> setRoundCorner(int topLeftRadius, int topRightRadius, int bottomLeftRadius,
+                                             int bottomRightRadius) {
+        mRadiusRectF = new RectF(0, 0, 0, 0);
+        mRadiusPath = new Path();
+        mBannerManager.getBannerOptions().setRoundRectRadius(topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius);
         return this;
     }
 

+ 21 - 5
bannerview/src/main/java/com/zhpan/bannerview/manager/BannerOptions.java

@@ -1,5 +1,7 @@
 package com.zhpan.bannerview.manager;
 
+import android.graphics.Path;
+import android.graphics.RectF;
 import android.view.View;
 
 import androidx.viewpager2.widget.ViewPager2;
@@ -9,6 +11,8 @@ import com.zhpan.bannerview.utils.BannerUtils;
 import com.zhpan.indicator.enums.IndicatorOrientation;
 import com.zhpan.indicator.option.IndicatorOptions;
 
+import java.util.Arrays;
+
 import static com.zhpan.bannerview.transform.ScaleInTransformer.DEFAULT_MIN_SCALE;
 
 /**
@@ -25,7 +29,7 @@ public class BannerOptions {
         pageMargin = BannerUtils.dp2px(20);
         rightRevealWidth = DEFAULT_REVEAL_WIDTH;
         leftRevealWidth = DEFAULT_REVEAL_WIDTH;
-        rtl = false;
+        roundRadius = new float[8];
     }
 
     public static final int DEFAULT_REVEAL_WIDTH = -1000;
@@ -56,7 +60,7 @@ public class BannerOptions {
 
     private int scrollDuration;
 
-    private int roundRadius;
+    private float[] roundRadius;
 
     private boolean userInputEnabled = true;
 
@@ -210,12 +214,24 @@ public class BannerOptions {
         mIndicatorMargin = new IndicatorMargin(left, top, right, bottom);
     }
 
-    public int getRoundRectRadius() {
+    public float[] getRoundRectRadius() {
         return roundRadius;
     }
 
-    public void setRoundRectRadius(int roundRadius) {
-        this.roundRadius = roundRadius;
+    public void setRoundRectRadius(int radius) {
+        setRoundRectRadius(radius, radius, radius, radius);
+    }
+
+    public void setRoundRectRadius(int topLeftRadius, int topRightRadius, int bottomLeftRadius,
+                                   int bottomRightRadius) {
+        roundRadius[0] = topLeftRadius;
+        roundRadius[1] = topLeftRadius;
+        roundRadius[2] = topRightRadius;
+        roundRadius[3] = topRightRadius;
+        roundRadius[4] = bottomRightRadius;
+        roundRadius[5] = bottomRightRadius;
+        roundRadius[6] = bottomLeftRadius;
+        roundRadius[7] = bottomLeftRadius;
     }
 
     public int getScrollDuration() {