Skip to content

Commit

Permalink
Restore OutLineProvider.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhpanvip committed Jun 21, 2021
1 parent 4d06519 commit f046594
Show file tree
Hide file tree
Showing 7 changed files with 214 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ private void initRadioGroup() {
} else if (checkedId == R.id.rb_vector_drawable) {
setDrawableIndicator(getVectorDrawableIndicator());
} else if (checkedId == R.id.rb_round_corner) {
mViewPager.setRoundCorner(1);
setRoundCorner();
}
});
Expand All @@ -109,7 +110,7 @@ private void setRoundCorner() {
int dp36 = getResources().getDimensionPixelOffset(R.dimen.dp_36);
mViewPager
.setRoundCorner(dp36, 0, 0, dp36)
.refreshData(getPicList(4));
.create(getPicList(4));
}

private void setDrawableIndicator(IIndicator indicator) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/res/layout/fragment_others.xml
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/dp_10"
android:layout_marginEnd="15dp"
android:text="Round Corner"
android:text="Top Left Bottom Right Round Corner"
android:textSize="@dimen/sp_16" />

</RadioGroup>
Expand Down
48 changes: 43 additions & 5 deletions bannerview/src/main/java/com/zhpan/bannerview/BannerViewPager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Parcelable;
Expand All @@ -28,6 +32,7 @@
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;
Expand Down Expand Up @@ -83,8 +88,8 @@ public void run() {
};

private RectF mRadiusRectF;

private Path mRadiusPath;
private Paint mPaint;

private int startX, startY;

Expand Down Expand Up @@ -298,6 +303,7 @@ private void initBannerData() {
if (list != null) {
setIndicatorValues(list);
setupViewPager(list);
initRoundCorner();
}
}

Expand Down Expand Up @@ -360,13 +366,27 @@ protected void dispatchDraw(Canvas canvas) {
if (mRadiusRectF != null && mRadiusPath != null) {
mRadiusRectF.right = this.getWidth();
mRadiusRectF.bottom = this.getHeight();
mRadiusPath.addRoundRect(mRadiusRectF, mBannerManager.getBannerOptions().getRoundRectRadius(),
mRadiusPath.addRoundRect(mRadiusRectF,
mBannerManager.getBannerOptions().getRoundRectRadiusArray(),
Path.Direction.CW);
canvas.clipPath(mRadiusPath);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
canvas.clipPath(mRadiusPath);
} else {
if (mPaint != null) {
canvas.drawPath(mRadiusPath, mPaint);
}
}
}
super.dispatchDraw(canvas);
}

private void initRoundCorner() {
int roundCorner = mBannerManager.getBannerOptions().getRoundRectRadius();
if (roundCorner > 0 && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
ViewStyleSetter.applyRoundCorner(this, roundCorner);
}
}

private void setupViewPager(List<T> list) {
if (mBannerPagerAdapter == null) {
throw new NullPointerException("You must set adapter for BannerViewPager");
Expand Down Expand Up @@ -521,7 +541,8 @@ public BaseBannerAdapter<T> getAdapter() {
* @param radius round radius
*/
public BannerViewPager<T> setRoundCorner(int radius) {
return setRoundCorner(radius, radius, radius, radius);
mBannerManager.getBannerOptions().setRoundRectRadius(radius);
return this;
}

/**
Expand All @@ -537,6 +558,10 @@ public BannerViewPager<T> setRoundCorner(int topLeftRadius, int topRightRadius,
int bottomRightRadius) {
mRadiusRectF = new RectF();
mRadiusPath = new Path();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
}
mBannerManager.getBannerOptions()
.setRoundRectRadius(topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius);
return this;
Expand All @@ -551,6 +576,20 @@ public BannerViewPager<T> setRoundRect(int radius) {
return setRoundCorner(radius);
}

/**
* 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> setRoundRect(int topLeftRadius, int topRightRadius,
int bottomLeftRadius,
int bottomRightRadius) {
return setRoundCorner(topLeftRadius, topRightRadius, bottomLeftRadius, bottomRightRadius);
}

/**
* Enable/disable auto play
*
Expand Down Expand Up @@ -1105,7 +1144,6 @@ public BannerViewPager<T> stopLoopWhenDetachedFromWindow(boolean stopLoopWhenDet
}

/**
*
* @param showIndicatorWhenOneItem 只有一个item时是否显示指示器,
* true:显示,false:不显示,默认值false
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.zhpan.bannerview.manager;

import android.graphics.Path;
import android.graphics.RectF;
import android.view.View;

import androidx.viewpager2.widget.ViewPager2;
Expand All @@ -11,8 +9,6 @@
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;

/**
Expand All @@ -29,7 +25,6 @@ public BannerOptions() {
pageMargin = BannerUtils.dp2px(20);
rightRevealWidth = DEFAULT_REVEAL_WIDTH;
leftRevealWidth = DEFAULT_REVEAL_WIDTH;
roundRadius = new float[8];
}

public static final int DEFAULT_REVEAL_WIDTH = -1000;
Expand Down Expand Up @@ -60,7 +55,9 @@ public BannerOptions() {

private int scrollDuration;

private float[] roundRadius;
private float[] roundRadiusArray;

private int roundRadius;

private boolean userInputEnabled = true;

Expand Down Expand Up @@ -126,7 +123,7 @@ public void setIndicatorSliderWidth(int normalWidth, int checkedWidth) {
mIndicatorOptions.setSliderWidth(normalWidth, checkedWidth);
}

public void showIndicatorWhenOnItem(boolean showIndicatorWhenOneItem){
public void showIndicatorWhenOnItem(boolean showIndicatorWhenOneItem) {
mIndicatorOptions.setShowIndicatorOneItem(showIndicatorWhenOneItem);
}

Expand Down Expand Up @@ -218,24 +215,29 @@ public void setIndicatorMargin(int left, int top, int right, int bottom) {
mIndicatorMargin = new IndicatorMargin(left, top, right, bottom);
}

public float[] getRoundRectRadius() {
public float[] getRoundRectRadiusArray() {
return roundRadiusArray;
}

public int getRoundRectRadius() {
return roundRadius;
}

public void setRoundRectRadius(int radius) {
setRoundRectRadius(radius, radius, radius, radius);
this.roundRadius = 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;
roundRadiusArray = new float[8];
roundRadiusArray[0] = topLeftRadius;
roundRadiusArray[1] = topLeftRadius;
roundRadiusArray[2] = topRightRadius;
roundRadiusArray[3] = topRightRadius;
roundRadiusArray[4] = bottomRightRadius;
roundRadiusArray[5] = bottomRightRadius;
roundRadiusArray[6] = bottomLeftRadius;
roundRadiusArray[7] = bottomLeftRadius;
}

public int getScrollDuration() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@

package com.zhpan.bannerview.provider;

import android.annotation.TargetApi;
import android.graphics.Outline;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.view.ViewOutlineProvider;

/**
* <pre>
* Created by zhangpan on 2018/12/26.
* Description:圆形效果
* </pre>
*/

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class OvalViewOutlineProvider extends ViewOutlineProvider {


public OvalViewOutlineProvider() {}

@Override
public void getOutline(final View view, final Outline outline) {
Rect ovalRect = convertToCircleRect(new Rect(0, 0, view.getWidth(), view.getHeight()));
outline.setOval(ovalRect);
}

/**
* 以矩形的中心点为圆心,较短的边为直径画圆
*
* 注意, 由于整除省略了小数, (x/2)*2不一定等于x
*
* Currently, only Outlines that can be represented as a rectangle, circle, or round rect
* support clipping.
*
* @param rect
* @return
*/
private Rect convertToCircleRect(Rect rect) {
int left, top, right, bottom;
if(rect.width() > rect.height()) {
int dH = rect.height() / 2;
left = rect.centerX() - dH;
top = 0;
right = rect.centerX() + dH;
bottom = dH * 2;
} else {
int dW = rect.width() / 2;
left = 0;
top = rect.centerY() - dW;
right = dW * 2;
bottom = rect.centerY() + dW;
}
rect.set(left, top, right, bottom);
return rect;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@


package com.zhpan.bannerview.provider;

import android.annotation.TargetApi;
import android.graphics.Outline;
import android.graphics.Rect;
import android.os.Build;
import android.view.View;
import android.view.ViewOutlineProvider;

/**
* <pre>
* Created by zhangpan on 2018/12/26.
* Description:圆角效果
* </pre>
*/

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class RoundViewOutlineProvider extends ViewOutlineProvider {

private float mRadius;// 圆角弧度

public RoundViewOutlineProvider(float radius) {
this.mRadius = radius;
}

@Override
public void getOutline(View view, Outline outline) {
Rect selfRect = new Rect(0, 0, view.getWidth(), view.getHeight());// 绘制区域
outline.setRoundRect(selfRect, mRadius);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@


package com.zhpan.bannerview.provider;

import android.os.Build;
import android.view.View;
import androidx.annotation.RequiresApi;

/**
* <pre>
* Created by zhangpan on 2018/12/26.
* Description:为View设置圆角/圆形效果,支持View及ViewGroup,适用Android 5.1及以上系统。
* </pre>
*/

public class ViewStyleSetter {

private ViewStyleSetter() {}

/**
* 为View设置圆角效果
*
* @param radius 圆角半径
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
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 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 static void clearShapeStyle(View target) {
if(target == null) {
return;
}
target.setClipToOutline(false);
}
}

0 comments on commit f046594

Please sign in to comment.