gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:27:07 29 4
gpt4 key购买 nike

在与 Camera intent 斗争了太久之后,我现在终于通过使用 SurfaceView 和 Camera api 为我的应用创建了自定义相机体验。这太棒了,因为我现在可以完全控制拍照体验。我也想控制画廊的体验。不清楚我该怎么做?有什么建议吗?基本上我希望有以下用户体验

  1. 点击图库按钮查看图库中的照片
  2. 从图库中选择一张照片
  3. 所选照片显示在自定义预览中,用户可以在其中四处移动照片以选择要使用的部分。请注意,这是一种发生在固定区域内的裁剪(在我的例子中是手机宽度的正方形):不允许调整大小;窗口内图片的区域就是使用的区域。几乎大多数社交媒体应用程序都是这样做的。我只是不知道他们如何管理预览/编辑窗口。

目前我所知道的:

  1. 我仍然可以使用图库 Intent 让用户选择图片
  2. 我遇到困难的部分是如何创建自定义编辑体验,让用户选择要显示的图像区域(固定大小的正方形)。同样,这不是调整裁剪大小;而是用户可以四处移动图像以选择适合窗口的子区域。

谢谢你的建议

最佳答案

好的,我们开始吧!

从图库中选择图像的 Activity 。

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

裁剪所选图像的 Activity :

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

这一切都希望这能给你提个醒:)

关于android - android中的自定义图库裁剪窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27803667/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com