簡體   English   中英

如何固定裁剪突出顯示視圖的高度,而不是使用上載的圖像進行更改?

[英]How to Fix the Height of the Cropping Highlighted View, instead of changing using uploaded Image?

我正在使用以下示例代碼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()方法用於固定裁剪區域的大小,如果使用此方法,則無法調整該視圖的大小,這很好。 但是裁切后的區域應固定為width = 100,height = 210,它不應更改,取決於上傳的圖像。

大尺寸上傳的圖片的裁切視圖尺寸較小(即HighlightView):

在此處輸入圖片說明

上傳尺寸較小的圖片具有較大的裁切視圖尺寸(即HighlightView):

在此處輸入圖片說明

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

請幫我。

訪問此: 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:1.0.1@aar'

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

}

輸出:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM