1

我是安卓新手。搜索了很多,找不到任何可能的解决方案来手动裁剪任何形状的图像并保存它。请帮忙。

同样以我的小知识,我有一个想法(不知道它是否有效)。

这是:

public boolean onTouchEvent(MotionEvent event) {
int eventaction = event.getAction();

    switch (eventaction) {
case MotionEvent.ACTION_MOVE:
            // finger moves on the screen

**Here code for getting X & Y positions and dynamically cropping side by side.**

}

请帮助...

4

2 回答 2

1

您可以使用默认的 android Crop 功能,如下所示:

private void imageCrop(Uri picUri) {
    try {

        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        // indicate image type and Uri
        cropIntent.setDataAndType(picUri, "image/*");
        // set crop properties
        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);
        }
    }

}

这是你如何做到的。我希望这能帮到您。

于 2013-08-02T07:56:48.027 回答
0

您可以尝试使用 android 的默认裁剪功能。

private void performCrop(Uri uri) {
    try {
        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        cropIntent.setDataAndType(uri, "image/*");
        cropIntent.putExtra("crop", "true");
        cropIntent.putExtra("return-data", true);
        startActivityForResult(cropIntent,
                request_code); // int value
    }
    catch (ActivityNotFoundException anfe) {
        // device doesn't support cropping
    } catch (Exception e) {
        // something went wrong
    }
}

然后通过onActivityResult获取图片

Bitmap img = (Bitmap) data.getExtras().get("data");
于 2013-08-02T08:03:30.600 回答