Skip to content

Commit fa2684f

Browse files
committed
version[1.1] Bug fixed.
1 parent 60e2e75 commit fa2684f

5 files changed

Lines changed: 101 additions & 14 deletions

File tree

ShapeableImageView-release.aar

213 Bytes
Binary file not shown.

ShapeableImageView/src/main/java/com/jummania/CornerType.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,40 @@
11
package com.jummania;
22

3+
/**
4+
* Enum representing different corner types for a ShapeableImageView.
5+
*/
36
public enum CornerType {
7+
/**
8+
* Rounded corners.
9+
*/
410
ROUNDED(0),
11+
12+
/**
13+
* Cut corners.
14+
*/
515
CUT(0),
16+
17+
/**
18+
* Default corners.
19+
*/
620
DEFAULT(0);
721

822
private final int cornerType;
23+
24+
/**
25+
* Constructs a CornerType with the specified corner type.
26+
*
27+
* @param cornerType The corner type value.
28+
*/
929
CornerType(int cornerType) {
1030
this.cornerType = cornerType;
1131
}
1232

33+
/**
34+
* Gets the corner type value.
35+
*
36+
* @return The corner type value.
37+
*/
1338
public int getCornerType() {
1439
return cornerType;
1540
}

ShapeableImageView/src/main/java/com/jummania/ShapeableImageView.java

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,26 +6,51 @@
66

77
import 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+
*/
913
public 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+
}
Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3+
4+
<!-- Declare a styleable for ShapeableImageView -->
35
<declare-styleable name="ShapeableImageView">
46

7+
<!-- Define an attribute for corner mode with enum values -->
58
<attr name="cornerMode" format="enum">
9+
<!-- Rounded corners -->
610
<enum name="rounded" value="0" />
11+
<!-- Cut corners -->
712
<enum name="cut" value="1" />
813
</attr>
914

15+
<!-- Define an attribute for corner radius with float format -->
1016
<attr name="radius" format="float" />
1117

1218
</declare-styleable>
13-
</resources>
19+
20+
</resources>

app/src/main/res/layout/activity_main.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
xmlns:tools="http://schemas.android.com/tools"
44
android:layout_width="match_parent"
55
android:layout_height="match_parent"
6-
xmlns:app="http://schemas.android.com/apk/res-auto"
76
tools:context=".MainActivity"
87
android:orientation="vertical"
98
android:layout_gravity="center"

0 commit comments

Comments
 (0)