android中的自定义图库裁剪窗口

时间:2015-01-06 17:10:15

标签: android image android-layout gallery

经过长时间冥想相机意图后,我现在终于使用SurfaceView和Camera api为我的应用程序创建了自定义相机体验。它非常棒,因为我现在可以完全控制拍照体验。我也想控制画廊的体验。目前尚不清楚我该怎么做?有什么建议?基本上我希望有以下用户体验

  1. 点击图库按钮查看图库中的照片
  2. 从图库中选择一张照片
  3. 所选照片显示在自定义预览中,用户可以在其中移动照片以选择要使用的部分。请注意,这是一种在固定区域内发生的裁剪(在我的情况下是一个与手机宽度大小相同的方格):不允许调整大小;窗口内图片的区域是使用的区域。几乎所有社交媒体应用程序都是这样做的。我只是不清楚他们如何管理预览/编辑窗口。
  4. 到目前为止我所知道的:

    1. 我仍然可以使用图库意图让用户选择图像
    2. 我遇到的部分是如何创建自定义编辑体验,让用户选择要显示的图像区域(固定大小的正方形)。再次,这不是重新调整大小;但是用户可以移动图像以选择适合窗口的子区域。
    3. 感谢您的任何建议

1 个答案:

答案 0 :(得分:1)

好的,我们走了!

活动从图库中选择图像。

import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;

public class GalleryUtil extends Activity{
    private final static int RESULT_SELECT_IMAGE = 100;
    public static final int MEDIA_TYPE_IMAGE = 1;
    private static final String TAG = "GalleryUtil";

    String mCurrentPhotoPath;
    File photoFile = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try{
            //Pick Image From Gallery
            Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, RESULT_SELECT_IMAGE); 
        }catch(Exception e){
            e.printStackTrace();
        }
    }

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

        switch(requestCode){
        case RESULT_SELECT_IMAGE:

            if (resultCode == Activity.RESULT_OK && data != null && data.getData() != null) {
                try{
                Uri selectedImage = data.getData();
                String[] filePathColumn = {MediaStore.Images.Media.DATA };
                Cursor cursor = getContentResolver().query(selectedImage,
                        filePathColumn, null, null, null);
                cursor.moveToFirst();
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String picturePath = cursor.getString(columnIndex);
                cursor.close();

                //return Image Path to the Main Activity
                Intent returnFromGalleryIntent = new Intent();
                returnFromGalleryIntent.putExtra("picturePath",picturePath);
                setResult(RESULT_OK,returnFromGalleryIntent);     
                finish();
                }catch(Exception e){
                    e.printStackTrace();
                    Intent returnFromGalleryIntent = new Intent();
                    setResult(RESULT_CANCELED, returnFromGalleryIntent);     
                    finish();   
                }
            }else{
                Log.i(TAG,"RESULT_CANCELED");     
                Intent returnFromGalleryIntent = new Intent();
                setResult(RESULT_CANCELED, returnFromGalleryIntent);     
                finish();
            }
            break;
        }
    }
}

活动裁剪所选图像:

public class ImageSelecter extends Activity{

    private final int GALLERY_ACTIVITY_CODE=200;
    private final int RESULT_CROP = 400;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        btn_choose.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //Start Activity To Select Image From Gallery   
                Intent gallery_Intent = new Intent(getApplicationContext(), GalleryUtil.class);
                startActivityForResult(gallery_Intent, GALLERY_ACTIVITY_CODE);
                break;
            }
        });

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == GALLERY_ACTIVITY_CODE) {
             if(resultCode == Activity.RESULT_OK){  
                 picturePath = data.getStringExtra("picturePath");  
                 //perform Crop on the Image Selected from Gallery
                 performCrop(picturePath);
             }
        }

        if (requestCode == RESULT_CROP ) {
             if(resultCode == Activity.RESULT_OK){  
                 Bundle extras = data.getExtras();
                 Bitmap selectedBitmap = extras.getParcelable("data");
                 // Set The Bitmap Data To ImageView
                 image_capture1.setImageBitmap(selectedBitmap);                             
                 image_capture1.setScaleType(ScaleType.FIT_XY);
             }
        }
    }

    private void performCrop(String picUri) {
        try {
            //Start Crop Activity

            Intent cropIntent = new Intent("com.android.camera.action.CROP");
            // indicate image type and Uri
            File f = new File(picUri);
            Uri contentUri = Uri.fromFile(f);

            cropIntent.setDataAndType(contentUri, "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", 280);
            cropIntent.putExtra("outputY", 280);

            // retrieve data on return
            cropIntent.putExtra("return-data", true);
            // start the activity - we handle returning in onActivityResult
            startActivityForResult(cropIntent, RESULT_CROP);
        }
        // respond to users whose devices do not support the crop action
        catch (ActivityNotFoundException anfe) {
            // display an error message
            String errorMessage = "your device doesn't support the crop action!";
            Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }   

这一切都希望这会让你头脑发热:)