| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.graphics.Point; |
| 22 |
import android.os.Parcel; |
| 23 |
import android.os.Parcelable; |
| 24 |
|
| 25 |
import com.android.internal.annotations.VisibleForTesting; |
| 26 |
|
| 27 |
import java.lang.annotation.Retention; |
| 28 |
import java.lang.annotation.RetentionPolicy; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public final class RoundedCorner implements Parcelable { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
public static final int POSITION_TOP_LEFT = 0; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public static final int POSITION_TOP_RIGHT = 1; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public static final int POSITION_BOTTOM_RIGHT = 2; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public static final int POSITION_BOTTOM_LEFT = 3; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
@IntDef(prefix = { "POSITION_" }, value = { |
| 64 |
POSITION_TOP_LEFT, |
| 65 |
POSITION_TOP_RIGHT, |
| 66 |
POSITION_BOTTOM_RIGHT, |
| 67 |
POSITION_BOTTOM_LEFT |
| 68 |
}) |
| 69 |
@Retention(RetentionPolicy.SOURCE) |
| 70 |
public @interface Position {} |
| 71 |
|
| 72 |
private final @Position int mPosition; |
| 73 |
private final int mRadius; |
| 74 |
@NonNull |
| 75 |
private final Point mCenter; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
@VisibleForTesting |
| 82 |
public RoundedCorner(@Position int position) { |
| 83 |
mPosition = position; |
| 84 |
mRadius = 0; |
| 85 |
mCenter = new Point(0, 0); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
public RoundedCorner(@Position int position, int radius, int centerX, |
| 102 |
int centerY) { |
| 103 |
mPosition = position; |
| 104 |
mRadius = radius; |
| 105 |
mCenter = new Point(centerX, centerY); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
RoundedCorner(RoundedCorner rc) { |
| 114 |
mPosition = rc.getPosition(); |
| 115 |
mRadius = rc.getRadius(); |
| 116 |
mCenter = new Point(rc.getCenter()); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public @Position int getPosition() { |
| 128 |
return mPosition; |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public int getRadius() { |
| 138 |
return mRadius; |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
@NonNull |
| 147 |
public Point getCenter() { |
| 148 |
return new Point(mCenter); |
| 149 |
} |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public boolean isEmpty() { |
| 160 |
return mRadius == 0 || mCenter.x <= 0 || mCenter.y <= 0; |
| 161 |
} |
| 162 |
|
| 163 |
private String getPositionString(@Position int position) { |
| 164 |
switch (position) { |
| 165 |
case POSITION_TOP_LEFT: |
| 166 |
return "TopLeft"; |
| 167 |
case POSITION_TOP_RIGHT: |
| 168 |
return "TopRight"; |
| 169 |
case POSITION_BOTTOM_RIGHT: |
| 170 |
return "BottomRight"; |
| 171 |
case POSITION_BOTTOM_LEFT: |
| 172 |
return "BottomLeft"; |
| 173 |
default: |
| 174 |
return "Invalid"; |
| 175 |
} |
| 176 |
} |
| 177 |
|
| 178 |
@Override |
| 179 |
public boolean equals(Object o) { |
| 180 |
if (o == this) { |
| 181 |
return true; |
| 182 |
} |
| 183 |
if (o instanceof RoundedCorner) { |
| 184 |
RoundedCorner r = (RoundedCorner) o; |
| 185 |
return mPosition == r.mPosition && mRadius == r.mRadius |
| 186 |
&& mCenter.equals(r.mCenter); |
| 187 |
} |
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
@Override |
| 192 |
public int hashCode() { |
| 193 |
int result = 0; |
| 194 |
result = 31 * result + mPosition; |
| 195 |
result = 31 * result + mRadius; |
| 196 |
result = 31 * result + mCenter.hashCode(); |
| 197 |
return result; |
| 198 |
} |
| 199 |
|
| 200 |
@Override |
| 201 |
public String toString() { |
| 202 |
return "RoundedCorner{" |
| 203 |
+ "position=" + getPositionString(mPosition) |
| 204 |
+ ", radius=" + mRadius |
| 205 |
+ ", center=" + mCenter |
| 206 |
+ '}'; |
| 207 |
} |
| 208 |
|
| 209 |
@Override |
| 210 |
public int describeContents() { |
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
@Override |
| 215 |
public void writeToParcel(@NonNull Parcel out, int flags) { |
| 216 |
out.writeInt(mPosition); |
| 217 |
out.writeInt(mRadius); |
| 218 |
out.writeInt(mCenter.x); |
| 219 |
out.writeInt(mCenter.y); |
| 220 |
} |
| 221 |
|
| 222 |
public static final @NonNull Creator<RoundedCorner> CREATOR = new Creator<RoundedCorner>() { |
| 223 |
@Override |
| 224 |
public RoundedCorner createFromParcel(Parcel in) { |
| 225 |
return new RoundedCorner(in.readInt(), in.readInt(), in.readInt(), in.readInt()); |
| 226 |
} |
| 227 |
|
| 228 |
@Override |
| 229 |
public RoundedCorner[] newArray(int size) { |
| 230 |
return new RoundedCorner[size]; |
| 231 |
} |
| 232 |
}; |
| 233 |
} |