android bitmap压缩处理

android中一些bitmap的简单处理

[1].[代码] [Java]代码 跳至 [1]

?
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;
     }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值