2013-03-05 94 views
25

我想裁剪圖像我發現了一些非常有用的圖像,但不知何故就像缺乏未選區域變暗,所以我想知道有誰知道如何?或讓我走向正確的方向?我發現的在線教程顯示,會使選定的區域變暗,但是當我使用它時,它不會。請多謝我,非常抱歉我的英文不好。在android中的裁剪圖像

指向我使用的教程的鏈接。

Crop image tutorial 1

Crop Image tutorial 2

我希望它是這樣的。

I want it be something like this

editButton.setOnClickListener(new Button.OnClickListener(){ 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      Intent goEdit; 
      goEdit = new Intent(PreviewActivity.this, CropImage.class); 
      goEdit.putExtra("image-path", path); 
      goEdit.putExtra("scale", true); 
      goEdit.putExtra("fileName", nameFromPath); 
      //finish(); 
      checkEdit = true; 
      startActivityForResult(goEdit,0); 

     } 
}); 

編輯 我用這個按鈕監聽器調用的類CropImage活動調入cropImage文件。這是一個自定義的意圖,而不是android內的裁剪功能,但我認爲它的副本使它支持所有版本,但是當我調用它時,選定區域不亮,我不知道哪裏有問題可以引導我?謝謝 這是我正在使用的圖書館drioid4you crop image

+1

請問你可以發表你試過的代碼嗎?並且,描述當你嘗試時會發生什麼?你收到任何類型的錯誤信息嗎? – Jodes 2013-03-05 16:43:52

+0

我的意思是我沒有得到任何錯誤信息我只是不知道如何使選定的區域變亮和未選區域變暗顯示不同 – user1235085 2013-03-06 00:59:38

+0

檢查[這個問題] [1]爲我在那裏建議的替代庫。 [1]:http://stackoverflow.com/questions/12758425/how-to-set-the-output-image-use-com-android-camera-action-crop/ – hcpl 2013-05-22 23:44:04

回答

41

你可以使用默認的android裁剪功能嗎?

這裏是我的代碼

private void performCrop(Uri picUri) { 
    try { 
     Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
     // indicate image type and Uri 
     cropIntent.setDataAndType(picUri, "image/*"); 
     // set crop properties here 
     cropIntent.putExtra("crop", true); 
     // indicate aspect of desired crop 
     cropIntent.putExtra("aspectX", 1); 
     cropIntent.putExtra("aspectY", 1); 
     // indicate output X and Y 
     cropIntent.putExtra("outputX", 128); 
     cropIntent.putExtra("outputY", 128); 
     // retrieve data on return 
     cropIntent.putExtra("return-data", true); 
     // start the activity - we handle returning in onActivityResult 
     startActivityForResult(cropIntent, PIC_CROP); 
    } 
    // respond to users whose devices do not support the crop action 
    catch (ActivityNotFoundException anfe) { 
     // display an error message 
     String errorMessage = "Whoops - your device doesn't support the crop action!"; 
     Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
} 

聲明:

final int PIC_CROP = 1; 

在頂部。

在onActivity結果的方法,令狀下面的代碼:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == PIC_CROP) { 
     if (data != null) { 
      // get the returned data 
      Bundle extras = data.getExtras(); 
      // get the cropped bitmap 
      Bitmap selectedBitmap = extras.getParcelable("data"); 

      imgView.setImageBitmap(selectedBitmap); 
     } 
    } 
} 

這是很容易的,我實現,也顯示變暗的區域。

+0

嗨,我嘗試你的代碼,但它仍然無法顯示我手機上的黑暗區域:(我想知道是否是因爲我的清單?或電話版本? – user1235085 2013-03-06 07:10:17

+0

10哪個版本是你使用? – 2013-03-06 07:33:28

+0

當前samsung s3 4.1.2 – user1235085 2013-03-06 09:05:35

1

希望你做得很好。 你可以使用我的代碼來裁剪圖像。你只需要創建一個類並將這個類用於你的XMljava類。 Crop image。 您可以將選定的圖像裁剪成圓形並將其裁剪成許多選項。 希望它完全適合你。因爲這對你來說是完全可以管理的,你可以根據你的需要改變它。

喜歡你的工作:)

+0

也添加'R.styleable ....'元素。 – 2017-03-07 11:55:42

+0

是肯定的..將盡快更新 – 2017-03-08 08:28:32

0

我發現了一個非常酷的庫,嘗試了這一點。 這是非常順利和易於使用。

https://github.com/TakuSemba/CropMe

+0

,但不是手動x和y值隨時改變,所以任何其他庫開發然後聲明... – 2017-10-26 07:30:29