标签:
android中一些bitmap的简单处理
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
//根据指定的bitmap路径加载,并定义好了目标的大小 public
static
Bitmap decodeFile(String path, int
dstWidth, int
dstHeight) { Options
options = new
Options(); options.inJustDecodeBounds
= true ; BitmapFactory.decodeFile(path,
options); options.inJustDecodeBounds
= false ; options.inSampleSize
= calculateSampleSize(options.outWidth, options.outHeight, dstWidth, dstHeight); Bitmap
unscaledBitmap = BitmapFactory.decodeFile(path, options); return
unscaledBitmap; } /** *
获得经过处理的bitmap */ public
static
Bitmap createScaledBitmap(Bitmap unscaledBitmap, int
dstWidth, int
dstHeight) { Rect
srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth,
dstHeight); Rect
dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth,
dstHeight); Bitmap
scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(), Config.ARGB_8888); Canvas
canvas = new
Canvas(scaledBitmap); canvas.drawBitmap(unscaledBitmap,
srcRect, dstRect, new
Paint(Paint.FILTER_BITMAP_FLAG)); return
scaledBitmap; } /** *
计算Option的inSampleSize属性 */ public
static
int
calculateSampleSize( int
srcWidth, int
srcHeight, int
dstWidth, int
dstHeight) { final
float
srcAspect = ( float )
srcWidth / ( float )
srcHeight; final
float
dstAspect = ( float )
dstWidth / ( float )
dstHeight; if
(srcAspect > dstAspect) { return
srcWidth / dstWidth; }
else
{ return
srcHeight / dstHeight; } } /** *
计算源文件的Rect */ public
static
Rect calculateSrcRect( int
srcWidth, int
srcHeight, int
dstWidth, int
dstHeight) { return
new
Rect( 0 ,
0 ,
srcWidth, srcHeight); } /** *
计算目标bitmap的rect区域 */ public
static
Rect calculateDstRect( int
srcWidth, int
srcHeight, int
dstWidth, int
dstHeight) { final
float
srcAspect = ( float )
srcWidth / ( float )
srcHeight; final
float
dstAspect = ( float )
dstWidth / ( float )
dstHeight; if
(srcAspect > dstAspect) { return
new
Rect( 0 ,
0 ,
dstWidth, ( int )
(dstWidth / srcAspect)); }
else
{ return
new
Rect( 0 ,
0 ,
( int )
(dstHeight * srcAspect), dstHeight); } }
/** *
View转化为BitMap */ public
static
Bitmap convertViewToBitmap(View view) { view.measure(MeasureSpec.makeMeasureSpec( 0 ,
MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec( 0 ,
MeasureSpec.UNSPECIFIED)); view.layout( 0 ,
0 ,
view.getMeasuredWidth(), view.getMeasuredHeight()); view.buildDrawingCache(); Bitmap
bitmap = view.getDrawingCache(); return
bitmap; } |
标签:
原文地址:http://blog.csdn.net/u014311064/article/details/42284275