1. 程式人生 > >圖片的按比例顯示

圖片的按比例顯示

col nsa color deb factory bounds gets splay bit

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

圖片的按比例顯示