2016-05-11 28 views
0

我正在使用以下示例代碼,https://github.com/mklimek/android-crop/tree/newfeature_fied_size_crop。它給出了固定大小的裁剪視圖(即,HighlightView)。但問題是這highlightView是固定的取決於上傳的圖像。以下是兩張不同尺寸上傳圖片的屏幕截圖。如何修復裁剪高亮視圖的高度,而不是使用上傳的圖像進行更改?

我用下面的代碼行以固定HighlightView的大小:

private void beginCrop(Uri source) { 
    Uri outputUri = Uri.fromFile(new File(getCacheDir(), "cropped")); 
    new Crop(source).output(outputUri).asRectangle().withFixedSize(100, 210).start(this); 
} 

withFixedSize()方法被用於固定裁剪區域的大小,我們不能如果我們使用這種方法調整該視圖, 沒事兒。但是,裁剪區域應該固定爲寬度= 100,高度= 210,它不應該根據上傳的圖像進行更改。

大尺寸上傳圖像具有作物視圖的一小尺寸(即,HighlightView):

enter image description here

小尺寸上傳圖像具有作物視圖的大尺寸(即,HighlightView):

enter image description here

我的要求是我必須修復裁剪區域的大小,我不應該調整大小。我在谷歌世界搜索。但我沒有找到解決方案。

請幫我解決這個問題。

+0

見下面我answe。 –

回答

0

此次訪問:https://github.com/jdamcd/android-crop

作物

Crop.of(inputUri, outputUri).asSquare().start(activity) 

傾聽作物的結果(參見示例項目,如果你想要做一些錯誤處理):

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent result) { 
    if (requestCode == Crop.REQUEST_CROP && resultCode == RESULT_OK) { 
     doSomethingWithCroppedImage(outputUri); 
    } 
} 

提供了一些屬性來定製裁剪屏幕。請參閱示例項目主題。

的庫提供了一個實用的方法來開始圖像拾取器:

Crop.pickImage(activity) 

依賴

的AAR上的Maven Central發表:

compile 'com.soundcloud.android:android-crop:[email protected]' 

Java代碼:

public class MainActivity extends Activity { 

    private ImageView resultView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     resultView = (ImageView) findViewById(R.id.result_image); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return super.onCreateOptionsMenu(menu); 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     if (item.getItemId() == R.id.action_select) { 
      resultView.setImageDrawable(null); 
      Crop.pickImage(this); 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent result) { 
     if (requestCode == Crop.REQUEST_PICK && resultCode == RESULT_OK) { 
      beginCrop(result.getData()); 
     } else if (requestCode == Crop.REQUEST_CROP) { 
      handleCrop(resultCode, result); 
     } 
    } 

    private void beginCrop(Uri source) { 
     Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped")); 
     Crop.of(source, destination).asSquare().start(this); 
    } 

    private void handleCrop(int resultCode, Intent result) { 
     if (resultCode == RESULT_OK) { 
      resultView.setImageURI(Crop.getOutput(result)); 
     } else if (resultCode == Crop.RESULT_ERROR) { 
      Toast.makeText(this, Crop.getError(result).getMessage(), Toast.LENGTH_SHORT).show(); 
     } 
    } 

} 

輸出:

enter image description here

+0

你可以建議我在哪裏需要改變邏輯.. ?? –

+0

@SarithaG我更新的代碼見。 –

+0

我做了同樣的事情。但是裁剪後的視圖大小正在變化,取決於上傳的圖像。請通過安裝此應用程序來檢查代碼。 –

相關問題