我正在制作Android应用程序,其中我有一个场景,我从库中选择一个图像,裁剪它并在imageview上显示它.现在在裁剪时我也希望缩放该图像.我正在使用TouchImageView类来缩放图像.
请注意,如果我只想在ImageView上应用TouchImageView,它可以正常工作.但是当我将它用于裁剪功能时它不起作用.
我应该如何在ImageView上一次应用裁剪缩放功能?
任何类型的帮助将不胜感激.以下是我的尝试.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
System.out.println(requestCode);
if (resultCode != RESULT_OK) {
return;
}
Bitmap bitmap;
switch (requestCode) {
case 0:
mImageCaptureUri = data.getData();
doCrop();
break;
case 1:
mImageCaptureUri = data.getData();
doCrop();
break;
case 2:
Bundle extras = data.getExtras();
/**
* After cropping the image, get the bitmap of the cropped image and
* display it on imageview.
*/
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
img_v.setImageBitmap(photo);
//myBitmap = getCircleImage(photo);
//String image_base64 = postimage();
// new PostCoverTask().execute(userid, image_base64);
}
File f = new File(mImageCaptureUri.getPath());
/**
* Delete the temporary image
*/
if (f.exists())
f.delete();
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
private void doCrop()
{
final ArrayList cropOptions = new ArrayList();
/**
* Open image crop app by starting an intent
* ‘com.android.camera.action.CROP‘.
*/
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(mImageCaptureUri,"image/*");
/**
* Check if there is image cropper app installed.
*/
List list = getPackageManager().queryIntentActivities(
intent, 0);
int size = list.size();
/**
* If there is no image cropper app, display warning message
*/
if (size == 0)
{
Toast.makeText(this, "Can not find image crop app",
Toast.LENGTH_SHORT).show();
return;
}
else
{
size=1;
/**
* Specify the image path, crop dimension and scale
*/
// intent.setData(mImageCaptureUri);
intent.putExtra("crop", "true");
intent.putExtra("outputX", 256);
intent.putExtra("outputY", 256);
intent.putExtra("aspectX",3);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("scaleUpIfNeeded", true);
intent.putExtra("return-data", true);
System.out.println("Put image in Extra");
/**
* There is posibility when more than one image cropper app exist,
* so we have to check for it first. If there is only one app, open
* then app.
*/
if (size == 1) {
Intent i = new Intent(intent);
ResolveInfo res=null ;
for (int i1=0;i1
{
if(list.get(i1)!=null)
{
res = list.get(i1);
break;
}
}
i.setComponent(new ComponentName(res.activityInfo.packageName,
res.activityInfo.name));
System.out.println("size is equal to "+size);
startActivityForResult(i, 2);
}
else
{
System.out.println("size is equal to "+size);
/**
* If there are several app exist, create a custom chooser to
* let user selects the app.
*/
for (ResolveInfo res : list)
{
final CropOption co = new CropOption();
co.title = getPackageManager().getApplicationLabel(
res.activityInfo.applicationInfo);
co.icon = getPackageManager().getApplicationIcon(
res.activityInfo.applicationInfo);
co.appIntent = new Intent(intent);
co.appIntent
.setComponent(new ComponentName(
res.activityInfo.packageName,
res.activityInfo.name));
cropOptions.add(co);
}
CropOptionAdapter adapter = new CropOptionAdapter(
getApplicationContext(), cropOptions);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose Crop App");
builder.setAdapter(adapter,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int item) {
System.out.println("option "+cropOptions.get(item));
//startActivityForResult(cropOptions.get(item).appIntent,
// CROP_FROM_CAMERA);
System.out.println("builder.setAdapter");
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
if (mImageCaptureUri != null) {
getContentResolver().delete(mImageCaptureUri, null,
null);
mImageCaptureUri = null;
}
System.out.println("Click on cancel");
}
});
AlertDialog alert = builder.create();
alert.show();
System.out.println("alert");
}
}
}