2012-06-23 38 views
1

由於您看到了image,我們可以通過爲圖像設置一個負邊距並將overflow:hidden設置爲其父元素來裁剪它.So如何在android中使用它? 我相信強大的Bitmap類可以裁剪它並創建一個新的圖像。有沒有更簡單的方法,比如做一些寬度的邊距或佈局?Android如何裁剪圖像使用xml像css


這裏有我的解決方案: 我已經找到了一個更好的辦法剛纔和解決大多數我的問題:

imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);

可選代碼(設置一點點切緣陰性(填充其實))

imageview.setPadding(-5, -10, -5, -10);

將調整圖像以適應其母公司的寬度或他飛行和經銷商使其中心

回答

1

您可以剪裁使用圖像com.android.camera.action.CROP作爲

File tempFile = new File("mnt/sdcard/Images/beijingtesttemp.jpeg");//Image path which u want to crop 
Intent intent = new Intent("com.android.camera.action.CROP"); 
intent.setDataAndType(Uri.fromFile(tempFile), "image/*"); 
intent.putExtra("crop", "true"); 
intent.putExtra("aspectX", 1); 
intent.putExtra("aspectY", 1); 
intent.putExtra("outputX", 96); 
intent.putExtra("outputY", 96); 
intent.putExtra("output", Uri.fromFile(tempFile)); 
intent.putExtra("outputFormat", "JPEG"); 
startActivityForResult(intent,PHOTOR); 

onActivityResult得到使用data.getExtras()

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
      if (resultCode == NONE) 
       return; 
      if (requestCode == PHOTOR) { 
       Bundle extras = data.getExtras(); 
       if (extras != null) { 
        // get cropped image as bitmap here 
        Bitmap photo = extras.getParcelable("data"); 

       } 
      } 
      super.onActivityResult(requestCode, resultCode, data); 
     } 
+0

oh ~~謝謝,我現在找到了一個更好的方法:imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);將調整圖像的大小以適應父母的寬度或高度,並自動使其居中 – waisir

1
android:scaleType="centerCrop" 

是偉大的裁剪後的圖像,但它不真正收穫形象。要正確裁剪,您還應該使用:

android:cropToPadding="true"