-
-
Notifications
You must be signed in to change notification settings - Fork 444
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
214 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
bannerview/src/main/java/com/zhpan/bannerview/provider/OvalViewOutlineProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
bannerview/src/main/java/com/zhpan/bannerview/provider/RoundViewOutlineProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
bannerview/src/main/java/com/zhpan/bannerview/provider/ViewStyleSetter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |