自由式裁剪图像

时间:2013-08-02 07:48:39

标签: android

我是android新手。搜索了很多,找不到任何可能的解决方案,手工裁剪任何形状的图像并保存。请帮助。

另外,凭借我的小知识,我有了一个想法(不知道它是否有效)。

是:

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.**

}

请帮忙......

2 个答案:

答案 0 :(得分: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);
        }
    }

}

这是你怎么做的。 我希望这会对你有所帮助。

答案 1 :(得分: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");