码迷,mamicode.com
首页 > 其他好文 > 详细

图片的按比例显示

时间:2017-07-08 10:07:02      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:lex   option   window   jpg   1.2   display   获取   default   bsp   

1.获取手机当前分辨率

1.1

1  WindowManager wm=(WindowManager)getSystemService(WINDOW_SERVICE);
2  Point point=new Point();
3  wm.getDefaultDisplay().getSize(point);
4  int height=point.x;
5  int weight=point.y;

1.2

1 WindowManager wm = (WindowManager)getSystemService(Context.WINDOW_SERVICE);  
2 int width = wm.getDefaultDisplay().getWidth();  
3 int height = wm.getDefaultDisplay().getHeight();  

1.3

1 WindowManager manager = this.getWindowManager();
2 DisplayMetrics outMetrics = new DisplayMetrics();
3 manager.getDefaultDisplay().getMetrics(outMetrics);
4 int width2 = outMetrics.widthPixels;
5 int height2 = outMetrics.heightPixels;

2.获取图片分辨率

1   BitmapFactory.Options options=new BitmapFactory.Options();         
2 options.inJustDecodeBounds=true;//执行这句话并不会true解析位图 但还能获取图片的宽高信息 3   Bitmap bitmap=BitmapFactory.decodeFile("/mnt/sdcard/1.jpg",options); 4 //获取图片的宽和高信息 5  int weights=options.outWidth; 6 int heights=options.outHeight;

3.计算缩放比

int scale=1;
int scaleX=(weights/weight);
int scaleY=(heights/height);
if (scaleX>=scaleY&&scaleX>scale){
    scale=scaleX;
   }
if (scaleY>scaleX&&scaleY>scale){
      scale=scaleY;
    }

4.按比例显示

options.inSampleSize=scale;
options.inJustDecodeBounds=false;
Bitmap bitmap1=BitmapFactory.decodeFile("/mnt/sdcard/1.jpg",options); iv.setImageBitmap(bitmap1);

 

图片的按比例显示

标签:lex   option   window   jpg   1.2   display   获取   default   bsp   

原文地址:http://www.cnblogs.com/liuyinghai87/p/7135783.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!