Skip to content

Commit c54bd61

Browse files
author
Paul Soucy
committed
added a few extra utility classes
1 parent 8637942 commit c54bd61

File tree

3 files changed

+141
-0
lines changed

3 files changed

+141
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.devsmart.android;
2+
3+
import java.io.FileDescriptor;
4+
5+
import android.graphics.Bitmap;
6+
import android.graphics.BitmapFactory;
7+
import android.graphics.Canvas;
8+
import android.graphics.Matrix;
9+
import android.graphics.Paint;
10+
import android.graphics.Rect;
11+
import android.graphics.RectF;
12+
import android.graphics.Matrix.ScaleToFit;
13+
14+
public class GraphicsUtils {
15+
16+
public static enum ScaleType {
17+
CENTER_CROP,
18+
CENTER_FIT
19+
}
20+
21+
public static Matrix createScaleRect(RectF src, RectF dest, ScaleType type){
22+
Matrix retval = new Matrix();
23+
24+
switch(type){
25+
case CENTER_CROP:
26+
float[] points = new float[8];
27+
28+
points[0] = src.left;
29+
points[1] = src.top;
30+
points[2] = src.right;
31+
points[3] = src.bottom;
32+
33+
points[4] = dest.left;
34+
points[5] = dest.top;
35+
points[6] = dest.right;
36+
points[7] = dest.bottom;
37+
38+
final float ratioSrc = src.width() / src.height();
39+
final float ratioDest = dest.width() / dest.height();
40+
41+
if(ratioSrc > ratioDest){
42+
float fwidth = ratioSrc * dest.height();
43+
points[4] = dest.left - (fwidth - dest.width()) / 2;
44+
points[6] = dest.right + (fwidth - dest.width()) / 2;
45+
} else {
46+
float fheight = dest.width() / ratioSrc;
47+
points[5] = dest.top - (fheight - dest.height()) / 2;
48+
points[7] = dest.bottom + (fheight - dest.height()) / 2;
49+
}
50+
retval.setPolyToPoly(points, 0, points, 4, 2);
51+
52+
break;
53+
54+
case CENTER_FIT:
55+
retval.setRectToRect(src, dest, ScaleToFit.CENTER);
56+
}
57+
58+
return retval;
59+
}
60+
61+
public static Bitmap rotateBitmap(Bitmap input, int degrees) {
62+
RectF srcRect = new RectF(0, 0, input.getWidth(), input.getHeight());
63+
Matrix matrix = new Matrix();
64+
matrix.setRotate(degrees);
65+
matrix.mapRect(srcRect);
66+
matrix.postTranslate(0 - srcRect.left, 0 - srcRect.top);
67+
68+
Bitmap targetBitmap = Bitmap.createBitmap(Math.round(srcRect.width()), Math.round(srcRect.height()), Bitmap.Config.RGB_565);
69+
Canvas canvas = new Canvas(targetBitmap);
70+
canvas.drawBitmap(input, matrix, new Paint());
71+
return targetBitmap;
72+
}
73+
74+
public static Bitmap downsampleBitmap(FileDescriptor fd, int maxArea) {
75+
BitmapFactory.Options opts = new BitmapFactory.Options();
76+
opts.inJustDecodeBounds = true;
77+
78+
Rect outRect = new Rect();
79+
BitmapFactory.decodeFileDescriptor(fd, outRect, opts);
80+
81+
int subsample = 1;
82+
int width = opts.outWidth;
83+
int height = opts.outHeight;
84+
while(width * height > maxArea) {
85+
width /= 2;
86+
height /= 2;
87+
subsample++;
88+
}
89+
90+
opts.inJustDecodeBounds = false;
91+
opts.inSampleSize = subsample;
92+
Bitmap retval = BitmapFactory.decodeFileDescriptor(fd, null, opts);
93+
return retval;
94+
}
95+
96+
97+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.devsmart.android;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.Collections;
6+
import java.util.Iterator;
7+
import java.util.Random;
8+
9+
public class RandomIterator {
10+
11+
public static <T> Iterator<T> createRandomIterator(Collection<T> collection){
12+
return createRandomIterator(collection, new Random());
13+
}
14+
15+
public static <T> Iterator<T> createRandomIterator(Collection<T> collection, Random r){
16+
ArrayList<T> list = new ArrayList<T>(collection);
17+
Collections.shuffle(list, r);
18+
return list.iterator();
19+
}
20+
21+
22+
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.devsmart.android.mathutils;
2+
3+
public class Quotient {
4+
5+
public int numerator;
6+
public int denominator;
7+
8+
public Quotient(int n, int d) {
9+
numerator = n;
10+
denominator = d;
11+
}
12+
13+
public double getDoubleValue() {
14+
return (double)numerator / (double)denominator;
15+
}
16+
17+
public Quotient inverse() {
18+
return new Quotient(denominator, numerator);
19+
}
20+
21+
}

0 commit comments

Comments
 (0)