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+ }
0 commit comments