66
77import androidx .annotation .Nullable ;
88
9+ /**
10+ * Extended implementation of com.google.android.material.imageview.ShapeableImageView
11+ * with additional customization for corner shape and radius.
12+ */
913public class ShapeableImageView extends com .google .android .material .imageview .ShapeableImageView {
1014
1115 private CornerType cornerType = CornerType .DEFAULT ;
1216 private float radius = -1 ;
1317
18+ /**
19+ * Constructs a new ShapeableImageView.
20+ *
21+ * @param context The context in which the view is created.
22+ */
1423 public ShapeableImageView (Context context ) {
1524 super (context );
16- init (context , null );
25+ initialize (context , null );
1726 }
1827
28+ /**
29+ * Constructs a new ShapeableImageView with attribute set.
30+ *
31+ * @param context The context in which the view is created.
32+ * @param attrs The attribute set.
33+ */
1934 public ShapeableImageView (Context context , @ Nullable AttributeSet attrs ) {
2035 super (context , attrs );
21- init (context , attrs );
36+ initialize (context , attrs );
2237 }
2338
24- public ShapeableImageView (Context context , @ Nullable AttributeSet attrs , int defStyle ) {
25- super (context , attrs , defStyle );
26- init (context , attrs );
39+ /**
40+ * Constructs a new ShapeableImageView with attribute set and style.
41+ *
42+ * @param context The context in which the view is created.
43+ * @param attrs The attribute set.
44+ * @param defStyleRes The default style resource.
45+ */
46+ public ShapeableImageView (Context context , @ Nullable AttributeSet attrs , int defStyleRes ) {
47+ super (context , attrs , defStyleRes );
48+ initialize (context , attrs );
2749 }
2850
51+ /**
52+ * Sets the corner shape based on the current corner type and radius.
53+ */
2954 public void setCornerShape () {
3055 this .setShapeAppearanceModel (this .getShapeAppearanceModel ()
3156 .toBuilder ()
@@ -37,54 +62,85 @@ private int getCornerFamily() {
3762 return getCornerType ().getCornerType ();
3863 }
3964
65+ /**
66+ * Sets the corner shape with the specified corner type and radius.
67+ *
68+ * @param cornerType The type of corners to be applied.
69+ * @param radius The radius of the corners.
70+ */
4071 public void setCornerShape (CornerType cornerType , float radius ) {
4172 setCornerType (cornerType );
4273 setRadius (radius );
4374 setCornerShape ();
4475 }
4576
46- private void init (Context context , AttributeSet attrs ) {
47-
77+ private void initialize (Context context , AttributeSet attrs ) {
4878 TypedArray typedArray = context .obtainStyledAttributes (attrs , R .styleable .ShapeableImageView );
4979
5080 try {
5181 // Get values from the TypedArray
5282 setCornerType (CornerType .values ()[typedArray .getInt (R .styleable .ShapeableImageView_cornerMode , /* default value */ CornerType .DEFAULT .ordinal ())]);
5383 setRadius (typedArray .getDimension (R .styleable .ShapeableImageView_radius , /* default value */ -1 ));
54- // Use the obtained values as needed
55- // ...
5684
85+ // Apply corner shape if both radius and corner type are specified
5786 if (radius > -1 && getCornerType () != CornerType .DEFAULT )
5887 setCornerShape ();
5988
6089 } finally {
6190 // Ensure the TypedArray is recycled to avoid memory leaks
6291 typedArray .recycle ();
6392 }
64-
6593 }
6694
95+ /**
96+ * Called when the size of the view changes.
97+ * Automatically sets rounded corners if the current corner type is DEFAULT.
98+ *
99+ * @param width The new width of the view.
100+ * @param height The new height of the view.
101+ * @param oldWidth The old width of the view.
102+ * @param oldHeight The old height of the view.
103+ */
67104 @ Override
68105 protected void onSizeChanged (int width , int height , int oldWidth , int oldHeight ) {
69106 super .onSizeChanged (width , height , oldWidth , oldHeight );
70107 if (getCornerType () == CornerType .DEFAULT )
71108 setCornerShape (CornerType .ROUNDED , Math .min (width , height ) * (50 / 100f ));
72-
73109 }
74110
111+ /**
112+ * Gets the radius of the corners.
113+ *
114+ * @return The radius of the corners.
115+ */
75116 public float getRadius () {
76117 return radius ;
77118 }
78119
120+ /**
121+ * Sets the radius of the corners.
122+ *
123+ * @param radius The radius to be set.
124+ */
79125 public void setRadius (float radius ) {
80126 this .radius = radius ;
81127 }
82128
129+ /**
130+ * Gets the current corner type.
131+ *
132+ * @return The current corner type.
133+ */
83134 public CornerType getCornerType () {
84135 return cornerType ;
85136 }
86137
138+ /**
139+ * Sets the corner type.
140+ *
141+ * @param cornerType The corner type to be set.
142+ */
87143 public void setCornerType (CornerType cornerType ) {
88144 this .cornerType = cornerType ;
89145 }
90- }
146+ }
0 commit comments