|
| 1 | +package android.graphics; |
| 2 | + |
| 3 | +import android.os.Parcel; |
| 4 | +import android.os.Parcelable; |
| 5 | +import java.util.regex.Matcher; |
| 6 | +import java.util.regex.Pattern; |
| 7 | + |
| 8 | +public final class Rect { |
| 9 | + int left; |
| 10 | + int top; |
| 11 | + int right; |
| 12 | + int bottom; |
| 13 | + |
| 14 | + private static final class UnflattenHelper { |
| 15 | + private static final Pattern FLATTENED_PATTERN = Pattern.compile( |
| 16 | + "(-?\\d+) (-?\\d+) (-?\\d+) (-?\\d+)"); |
| 17 | + |
| 18 | + static Matcher getMatcher(String str) { |
| 19 | + return FLATTENED_PATTERN.matcher(str); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public Rect() { |
| 24 | + } |
| 25 | + |
| 26 | + public Rect(int left, int top, int right, int bottom) { |
| 27 | + this.left = left; |
| 28 | + this.top = top; |
| 29 | + this.right = right; |
| 30 | + this.bottom = bottom; |
| 31 | + } |
| 32 | + |
| 33 | + public Rect(Rect r) { |
| 34 | + if (r == null) { |
| 35 | + this.left = 0; |
| 36 | + this.top = 0; |
| 37 | + this.right = 0; |
| 38 | + this.bottom = 0; |
| 39 | + } else { |
| 40 | + this.left = left; |
| 41 | + this.top = top; |
| 42 | + this.right = right; |
| 43 | + this.bottom = bottom; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + public final int getWidth() { |
| 48 | + return right - left; |
| 49 | + } |
| 50 | + |
| 51 | + public final int getHeight() { |
| 52 | + return bottom - top; |
| 53 | + } |
| 54 | + |
| 55 | + public static Rect unflattenFromString(String str) { |
| 56 | + if (str.isEmpty()) { |
| 57 | + return null; |
| 58 | + } |
| 59 | + |
| 60 | + Matcher matcher = UnflattenHelper.getMatcher(str); |
| 61 | + if (!matcher.matches()) { |
| 62 | + return null; |
| 63 | + } |
| 64 | + |
| 65 | + return new Rect(Integer.parseInt(matcher.group(1)), |
| 66 | + Integer.parseInt(matcher.group(2)), |
| 67 | + Integer.parseInt(matcher.group(3)), |
| 68 | + Integer.parseInt(matcher.group(4))); |
| 69 | + } |
| 70 | + |
| 71 | + public String toShortString() { |
| 72 | + return toShortString(new StringBuilder(32)); |
| 73 | + } |
| 74 | + |
| 75 | + public String toShortString(StringBuilder sb) { |
| 76 | + sb.setLength(0); |
| 77 | + sb.append('['); sb.append(left); sb.append(','); |
| 78 | + sb.append(top); sb.append("]["); sb.append(right); |
| 79 | + sb.append(','); sb.append(bottom); sb.append(']'); |
| 80 | + return sb.toString(); |
| 81 | + } |
| 82 | + |
| 83 | + public String flattenToString() { |
| 84 | + StringBuilder sb = new StringBuilder(32); |
| 85 | + sb.append(left); |
| 86 | + sb.append(' '); |
| 87 | + sb.append(top); |
| 88 | + sb.append(' '); |
| 89 | + sb.append(right); |
| 90 | + sb.append(' '); |
| 91 | + sb.append(bottom); |
| 92 | + return sb.toString(); |
| 93 | + } |
| 94 | + |
| 95 | + public void writeToParcel(Parcel out, int flags) { |
| 96 | + out.writeInt(left); |
| 97 | + out.writeInt(top); |
| 98 | + out.writeInt(right); |
| 99 | + out.writeInt(bottom); |
| 100 | + } |
| 101 | + |
| 102 | + public static final Parcelable.Creator<Rect> CREATOR = new Parcelable.Creator<Rect>() { |
| 103 | + @Override |
| 104 | + public Rect createFromParcel(Parcel in) { |
| 105 | + Rect r = new Rect(); |
| 106 | + r.readFromParcel(in); |
| 107 | + return r; |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public Rect[] newArray(int size) { |
| 112 | + return new Rect[size]; |
| 113 | + } |
| 114 | + }; |
| 115 | + |
| 116 | + public void readFromParcel(Parcel in) { |
| 117 | + left = in.readInt(); |
| 118 | + top = in.readInt(); |
| 119 | + right = in.readInt(); |
| 120 | + bottom = in.readInt(); |
| 121 | + } |
| 122 | +} |
0 commit comments