Skip to content
This repository was archived by the owner on Aug 22, 2020. It is now read-only.

Commit 5b3ae6a

Browse files
committed
ClipOvalFrameLayout: Support Pre-lollipop
Signed-off-by: Fung <[email protected]>
1 parent 23c6774 commit 5b3ae6a

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ task clean(type: Delete) {
2121

2222
ext {
2323
supportLibraryVersion = '26.0.0'
24-
minSdkVersion = 21
24+
minSdkVersion = 17
2525
targetSdkVersion = 26
2626
buildToolsVersion = "26.0.1"
2727
versionCode = 1

library/src/main/java/moe/feng/common/stepperview/internal/ClipOvalFrameLayout.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package moe.feng.common.stepperview.internal;
22

33
import android.content.Context;
4-
import android.graphics.Outline;
4+
import android.graphics.Canvas;
5+
import android.graphics.Path;
56
import android.os.Build;
67
import android.util.AttributeSet;
7-
import android.view.View;
8-
import android.view.ViewOutlineProvider;
98
import android.widget.FrameLayout;
109

1110
/**
1211
* @hide
1312
*/
1413
public class ClipOvalFrameLayout extends FrameLayout {
1514

15+
private Path path = new Path();
16+
1617
public ClipOvalFrameLayout(Context context) {
1718
super(context);
1819
init();
@@ -29,11 +30,38 @@ public ClipOvalFrameLayout(Context context, AttributeSet attrs, int defStyleAttr
2930
}
3031

3132
private void init() {
32-
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
33+
if (!isPreLollipop()) {
3334
setClipToOutline(true);
35+
}
36+
}
37+
38+
@Override
39+
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
40+
super.onSizeChanged(w, h, oldw, oldh);
41+
42+
if (isPreLollipop()) {
43+
float halfWidth = w / 2f;
44+
float halfHeight = h / 2f;
45+
path.reset();
46+
path.addCircle(halfWidth, halfHeight, Math.min(halfWidth, halfHeight), Path.Direction.CW);
47+
path.close();
48+
}
49+
}
50+
51+
@Override
52+
protected void dispatchDraw(Canvas canvas) {
53+
if (isPreLollipop()) {
54+
int save = canvas.save();
55+
canvas.clipPath(path);
56+
super.dispatchDraw(canvas);
57+
canvas.restoreToCount(save);
3458
} else {
35-
// TODO Support SDK < 21
59+
super.dispatchDraw(canvas);
3660
}
3761
}
3862

63+
private static boolean isPreLollipop() {
64+
return Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP;
65+
}
66+
3967
}

0 commit comments

Comments
 (0)