Android ImageView设置图片原理

关于图片和ImageView,其实有更多可以介绍的,比如在解析图片前获得图片的宽高,用来做图片适配;比如等比例缩小图片,以减小内存占用;比如图片旋转效果等等。

第一个问题和第二个问题:

               BitmapFactory.Options options = new BitmapFactory.Options();
/*
* If set to true, the decoder will return null (no bitmap), but the
* out... fields will still be set, allowing the caller to query the
* bitmap without having to allocate the memory for its pixels.
*/
options.inJustDecodeBounds = true;
String url=Environment.getExternalStorageDirectory().getPath() +"/test.jpg";//sdcard/test.jpg
BitmapFactory.decodeFile(url,options);
int outWidth=options.outWidth;//获得图片的宽
int outHeight=options.outHeight;//获得图片的高
int width=100;//放图片组件的宽
int height=100;//放图片组件的高
double shink=outHeight*outWidth/width/height;//缩小的比例
options.inSampleSize=(int) Math.sqrt(shink);//inSampleSize设置,则缩小比例即它的平方,如2则比例为1/4。

Bitmap bitmap=BitmapFactory.decodeFile(url, options);//此值就是我们所需要的值