首页 > 代码库 > 图片的按比例显示

图片的按比例显示

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);

 

图片的按比例显示