Parcourir la source

Optimize ViewStyleSetter

zhpanvip il y a 4 ans
Parent
commit
ea9db3ce40

+ 1 - 2
app/src/main/java/com/example/zhpan/circleviewpager/view/CornerImageView.kt

@@ -12,8 +12,7 @@ class CornerImageView @JvmOverloads constructor(context: Context, attrs: Attribu
 
     fun setRoundCorner(radius: Int) {
         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
-            val viewStyleSetter = ViewStyleSetter(this)
-            viewStyleSetter.setRoundRect(radius.toFloat())
+            ViewStyleSetter.applyRoundCorner(this, radius.toFloat())
         }
     }
 }

+ 1 - 2
bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java

@@ -342,8 +342,7 @@ public class BannerViewPager<T, VH extends BaseViewHolder<T>> extends RelativeLa
     private void initRoundCorner() {
         int roundCorner = mBannerManager.getBannerOptions().getRoundRectRadius();
         if (roundCorner > 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
-            ViewStyleSetter viewStyleSetter = new ViewStyleSetter(this);
-            viewStyleSetter.setRoundRect(roundCorner);
+            ViewStyleSetter.applyRoundCorner(this,roundCorner);
         }
     }
 

+ 3 - 1
bannerview/src/main/java/com/zhpan/bannerview/BaseViewHolder.java

@@ -12,6 +12,8 @@ import androidx.annotation.NonNull;
 import androidx.annotation.StringRes;
 import androidx.recyclerview.widget.RecyclerView;
 
+import com.zhpan.bannerview.utils.BannerUtils;
+
 /**
  * <pre>
  *   Created by zhpan on 2020/4/5.
@@ -21,7 +23,7 @@ import androidx.recyclerview.widget.RecyclerView;
  */
 public abstract class BaseViewHolder<T> extends RecyclerView.ViewHolder {
 
-    private SparseArray<View> mViews = new SparseArray<>();
+    private final SparseArray<View> mViews = new SparseArray<>();
 
     public BaseViewHolder(@NonNull View itemView) {
         super(itemView);

+ 19 - 14
bannerview/src/main/java/com/zhpan/bannerview/provider/ViewStyleSetter.java

@@ -15,37 +15,42 @@ import android.view.View;
 
 public class ViewStyleSetter {
 
-    private View mView;
-
-    public ViewStyleSetter(View view) {
-        this.mView = view;
-    }
+    private ViewStyleSetter() {}
 
     /**
      * 为View设置圆角效果
-     * 
+     *
      * @param radius 圆角半径
      */
     @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
-    public void setRoundRect(float radius) {
-        this.mView.setClipToOutline(true);
-        this.mView.setOutlineProvider(new RoundViewOutlineProvider(radius));
+    public static void applyRoundCorner(View target, float radius) {
+        if(target == null) {
+            return;
+        }
+        target.setClipToOutline(true);// 用outline裁剪内容区域
+        target.setOutlineProvider(new RoundViewOutlineProvider(radius));
     }
 
     /**
      * 设置View为圆形
      */
     @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
-    public void setOvalView() {
-        this.mView.setClipToOutline(true);
-        this.mView.setOutlineProvider(new OvalViewOutlineProvider());
+    public static void applyCircle(View target) {
+        if(target == null) {
+            return;
+        }
+        target.setClipToOutline(true);// 用outline裁剪内容区域
+        target.setOutlineProvider(new OvalViewOutlineProvider());
     }
 
     /**
      * 清除View的圆角效果
      */
     @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
-    public void clearShapeStyle() {
-        this.mView.setClipToOutline(false);
+    public static void clearShapeStyle(View target) {
+        if(target == null) {
+            return;
+        }
+        target.setClipToOutline(false);
     }
 }