Skip to content

Commit a440a7e

Browse files
committed
Basic android.graphics Rect and Canvas implementation
Some extensions use more Canvas methods, but they don't really seem to get that far yet, all the others I was able to test seem to work now.
1 parent 0a748cd commit a440a7e

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package android.graphics;
2+
3+
import java.awt.Graphics2D;
4+
import java.awt.image.BufferedImage;
5+
import javax.imageio.ImageIO;
6+
7+
public final class Canvas {
8+
private BufferedImage canvasImage;
9+
private Graphics2D canvas;
10+
11+
public Canvas(Bitmap bitmap) {
12+
canvasImage = bitmap.getImage();
13+
canvas = canvasImage.createGraphics();
14+
}
15+
16+
public void drawBitmap(Bitmap sourceBitmap, Rect src, Rect dst, Paint paint) {
17+
BufferedImage sourceImage = sourceBitmap.getImage();
18+
BufferedImage sourceImageCropped = sourceImage.getSubimage(src.left, src.top, src.getWidth(), src.getHeight());
19+
canvas.drawImage(sourceImageCropped, null, dst.left, dst.top);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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

Comments
 (0)