標籤:des android style blog color 使用 io for
1、適屏
提取手機的圖片庫,並且進行選擇圖片的功能:
Button onClick:Intent intent = new Intent( Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);// 開啟手機的圖片庫 startActivityForResult(intent, IMAGE_SELECT);
處理圖片,按照手機的螢幕大小顯示:
if (requestCode == IMAGE_SELECT) { Uri url = data.getData();// 獲得圖片的路徑 int dw = getWindowManager().getDefaultDisplay().getWidth();// 獲得螢幕的寬度 int dh = getWindowManager().getDefaultDisplay().getWidth() / 2;// 獲得螢幕的高度/2 try { /** * BitampFactory :可以建立一個Bitmap位元影像對象 * BitampFactory.Options為匿名內部類 */ // 實現對圖片的裁剪的類,是一個匿名內部類 BitmapFactory.Options factory = new BitmapFactory.Options(); factory.inJustDecodeBounds = true;// 如果設定為true,允許查詢圖片不是按照像素分配給記憶體 // 將一個流轉換為Bitmap位元影像對象 Bitmap bmp = BitmapFactory.decodeStream( getContentResolver().openInputStream(url), null, factory); // 對圖片的寬度和高度對應手機的螢幕進行匹配 int hRatio = (int) Math .ceil(factory.outHeight / (float) dh); // 如果大於1 表示圖片的高度大於手機螢幕的高度 int wRatio = (int) Math.ceil(factory.outWidth / (float) dw); // 如果大於1表示圖片的寬度大於手機螢幕的寬度 // 縮放到1/radio的尺寸 和1/radio^2像素 if (hRatio > 1 || wRatio > 1) { if (hRatio > wRatio) { factory.inSampleSize = hRatio; } else { factory.inSampleSize = wRatio; } } factory.inJustDecodeBounds = false; // 使用BitmapFactory對圖片進行適屏的操作 bmp = BitmapFactory.decodeStream(getContentResolver() .openInputStream(url), null, factory); imageView.setImageBitmap(bmp); } catch (Exception e) { // TODO: handle exception } }
2、縮放:
Intent intent2 = getImageClipIntent(); startActivityForResult(intent2, IMAGE_CUT);
if (requestCode == IMAGE_CUT) { Bitmap bitmap = data.getParcelableExtra("data"); imageView.setImageBitmap(bitmap); }