图片的缩放处理

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
package com.sanya.largeimage;
  
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Point;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
  
public class MainActivity extends Activity {
    private ImageView lv;
    private WindowManager wm;
    private Point outSize;
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ImageView) this.findViewById(R.id.lv);
        wm = (WindowManager) getSystemService(WINDOW_SERVICE);
        outSize = new Point();
  
    }
  
      
    public void click(View view) {
        //新版的api
  
    /*  wm.getDefaultDisplay().getSize(outSize);
        int windowHeight = outSize.y;
        int windowWidth = outSize.x;
*/
        //旧版的api
         int windowHeight = wm.getDefaultDisplay().getHeight(); 
        int  windowWidth = wm.getDefaultDisplay().getWidth();
          
  
        BitmapFactory.Options opts = new Options();
        // 设置 不去真正的解析位图 不把他加载到内存 只是获取这个图片的宽高信息
        opts.inJustDecodeBounds = true;
        BitmapFactory.decodeFile("/sdcard/a.jpg", opts);
  
        int width = opts.outWidth;
        int height = opts.outWidth;
        if (width > windowWidth || height > windowHeight) {
            int x = width / windowWidth;
            int y = height / windowHeight;
            opts.inSampleSize = x > y ? x : y;
        } else {
  
            opts.inSampleSize = 1;
        }
        // 让位图工厂真正的去解析图片
        opts.inJustDecodeBounds = false;
        Bitmap file = BitmapFactory.decodeFile("/sdcard/a.jpg", opts);
        lv.setImageBitmap(file);
  
    }
}



来自为知笔记(Wiz)


posted on 2014-04-28 14:02  转折点人生  阅读(246)  评论(0)    收藏  举报