图片处理1-图片比例缩放
缩放时,按照大的缩放,才能保证图片在手机上完整的显示出来 |
// 1. 首先得到屏幕的宽高
WindowManager manager = getWindowManager();
Display display = manager.getDefaultDisplay();
int screenWidth = display.getWidth();
int screenHeight = display.getHeight();
System.out.println("屏幕宽度:" + screenWidth);
System.out.println("屏幕高度:" + screenHeight);
// 2. 获取图片的宽度和高度,不会真正的解析这个位图,只会获取这个位图在计算机存储的头信息
Options opts = new Options();
// 设置inJustDecodeBounds = true ;
opts.inJustDecodeBounds = true;
Bitmap bm = BitmapFactory.decodeResource(getResources(), IMGS[position%IMGS.length],opts);
// int imgWidth = bm.getWidth();
// int imgHeight = bm.getHeight();
// 获取图片宽高,不能用上面的,要用下面的,因为opts为true,此时bm为null
int imgWidth = opts.outWidth;
int imgHeight = opts.outHeight;
System.out.println("图片宽度:" + imgWidth);
System.out.println("图片高度:" + imgHeight);
int resizeX = imgWidth / screenWidth;
int resizeY = imgHeight / screenHeight;
int size = 0;
if (resizeX > resizeY) {
size = resizeX;
} else {
size = resizeY;
}
// 3、设置缩放比例
opts.inSampleSize = size;
System.out.println("缩放比例:" + size);
opts.inJustDecodeBounds = false;
// 4、真正的解析位图
bm = BitmapFactory.decodeResource(getResources(), IMGS[position%IMGS.length],opts);
|
完整代码:
|
|