11package moe .feng .common .stepperview .internal ;
22
33import android .content .Context ;
4- import android .graphics .Outline ;
4+ import android .graphics .Canvas ;
5+ import android .graphics .Path ;
56import android .os .Build ;
67import android .util .AttributeSet ;
7- import android .view .View ;
8- import android .view .ViewOutlineProvider ;
98import android .widget .FrameLayout ;
109
1110/**
1211 * @hide
1312 */
1413public 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