我有一个覆盖层,带有一个可用于掩模的透明区域 x,y,宽度,高度婴儿车。我正在从画廊和一个图像 将所选图像放置在给定的x,y坐标处。这个位置 在框架布局中完成。现在我可以旋转,缩放,移动 面具区。在我装上那个面具之后我想得到的 屏幕中图像的当前x,y坐标,以便 结合我使用基于x,y的画布组合图像 position.presently我在触摸时使用event.getx(),event.gety()获取值。 如果我使用那些位置绘制蒙版图像没有正确绘制 放大或移动后的位置
这是我的代码。如果有人有这个想法,请帮助我。
@覆盖 protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); 的setContentView(R.layout.activity_crop);
mSelectedVersion = getIntent().getExtras().getInt(
CROP_VERSION_SELECTED_KEY, -1);
Object overLayPath = getIntent().getExtras().containsKey("overLayPath") ? getIntent()
.getExtras().get("overLayPath") : null;
className = getIntent().getExtras().containsKey("class") ? (String) getIntent()
.getExtras().get("class") : null;
pageCount = (int) (getIntent().getExtras().containsKey("pageCount") ? getIntent()
.getExtras().getInt("pageCount") : 0);
File file = new File(overLayPath.toString());
String imgFile = (String) getIntent().getExtras().get("bitmap");
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
mScreenHeight = metrics.heightPixels;
mScreenWidth = metrics.widthPixels;
if (photoImg != null) {
photoImg.recycle();
photoImg = null;
}
if (className.equalsIgnoreCase("Fb_List")) {
if (imgFile != null && !imgFile.equalsIgnoreCase("")) {
File imageFile = new File(imgFile);
photoImg = BitmapFactory
.decodeFile(imageFile.getAbsolutePath());
}
} else {
if (imgFile != null && !imgFile.equalsIgnoreCase("")) {
Uri selectedImageURI = Uri.parse(imgFile);
InputStream is;
try {
is = getContentResolver().openInputStream(selectedImageURI);
if (photoImg != null) {
photoImg.recycle();
photoImg = null;
}
photoImg = decodeSampledBitmapFromResource(is,
(int) (320 * metrics.density),
(int) (416 * metrics.density));
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
mImg = (ImageView) findViewById(R.id.cp_img);
photoImg = createMaskImage(photoImg, mImg);
if (!file.exists()) {
bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.transparentoverlay);
} else {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
bitmap = BitmapFactory.decodeFile(overLayPath.toString(), options);
}
BitmapDrawable drawable = (BitmapDrawable) mImg.getDrawable();
if (drawable != null && drawable.getBitmap() != null) {
drawable.getBitmap().recycle();
mImg.setImageDrawable(null);
}
mTemplateImg = (ImageView) findViewById(R.id.cp_face_template);
mTemplateImg.setImageBitmap(bitmap);
mImg.setOnTouchListener(this);
if (mScreenWidth > 600 && mScreenHeight > 1024) {
bitmap = Bitmap.createScaledBitmap(bitmap,
(int) (320 * metrics.density),
(int) (416 * metrics.density), true);
mTemplateImg.setImageBitmap(bitmap);
} else if (mScreenWidth > 460 && mScreenHeight > 720) {
bitmap = Bitmap.createScaledBitmap(bitmap,
(int) (320 * metrics.density),
(int) (416 * metrics.density), true);
mTemplateImg.setImageBitmap(bitmap);
}
mImg.setImageBitmap(photoImg);
mMatrix.postScale(mScaleFactor, mScaleFactor);
mImg.setImageMatrix(mMatrix);
// Setup Gesture Detectors
mScaleDetector = new ScaleGestureDetector(getApplicationContext(),
new ScaleListener());
mRotateDetector = new RotateGestureDetector(getApplicationContext(),
new RotateListener());
mMoveDetector = new MoveGestureDetector(MaskCropActivity.this,
new MoveListener());
}
private Bitmap createMaskImage(Bitmap photoImg2, ImageView maskView) {
if (photoImg2 != null) {
String template = Common.getPrefrenceValue(
Constants.TEMPLATE_OBJECT, MaskCropActivity.this);
JSONObject templateObject = null;
if (template != null && !template.equalsIgnoreCase("")) {
try {
templateObject = new JSONObject(template);
JSONArray pageDetails = templateObject
.getJSONArray("pageDetails");
JSONObject pageObject = pageDetails
.getJSONObject(pageCount - 1);
JSONArray maskImages;
maskImages = pageObject.getJSONArray("maskImages");
if (maskImages != null && maskImages.length() > 0) {
JSONObject maskObject = maskImages.getJSONObject(0);
String coOrdinates = maskObject.has("coOrdinates") ? maskObject
.getString("coOrdinates") : "";
if (coOrdinates.contains(",")
&& coOrdinates.split(",").length == 4) {
int x = Integer.parseInt(coOrdinates.split(",")[0]);
int y = Integer.parseInt(coOrdinates.split(",")[1]);
maskX = x;
maskY = y;
int width = Integer
.parseInt(coOrdinates.split(",")[2]);
int height = Integer.parseInt(coOrdinates
.split(",")[3]);
if (width != 0 && height != 0) {
if (((photoImg2.getWidth() - (int) (width * metrics.density)) / 2)
+ (int) (width * metrics.density) > photoImg2
.getWidth()
|| (50 + (int) (height * metrics.density)) > photoImg2
.getHeight()) {
Log.i(TAG,
"image size is less than required");
photoImg2 = Bitmap.createScaledBitmap(
photoImg2,
(int) (width * metrics.density),
(int) (height * metrics.density),
true);
} else {
Log.i(TAG,
"image size is more than required");
photoImg2 = Bitmap
.createBitmap(
photoImg2,
(photoImg2.getWidth() - (int) (width * metrics.density)) / 2,
50,
(int) (width * metrics.density),
(int) (height * metrics.density));
}
}
MarginLayoutParams mlp = (MarginLayoutParams) maskView
.getLayoutParams();
mlp.setMargins(x, y, 0, 0);// all in pixels
maskView.setLayoutParams(mlp);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return photoImg2;
}
return photoImg2;
}
public static Bitmap decodeSampledBitmapFromResource(
InputStream inputStream, int reqWidth, int reqHeight) {
byte[] byteArr = new byte[0];
byte[] buffer = new byte[1024];
int len;
int count = 0;
try {
while ((len = inputStream.read(buffer)) > -1) {
if (len != 0) {
if (count + len > byteArr.length) {
byte[] newbuf = new byte[(count + len) * 2];
System.arraycopy(byteArr, 0, newbuf, 0, count);
byteArr = newbuf;
}
System.arraycopy(buffer, 0, byteArr, count, len);
count += len;
}
}
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(byteArr, 0, count, options);
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
options.inPurgeable = true;
options.inInputShareable = true;
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
int[] pids = { android.os.Process.myPid() };
return BitmapFactory.decodeByteArray(byteArr, 0, count, options);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public void onCropImageButton(View v) {
// Create progress dialog and display it.
try {
mProgressDialog = new ProgressDialog(v.getContext());
mProgressDialog.setCancelable(false);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
mProgressDialog.setMessage("Cropping Image\nPlease Wait.....");
mProgressDialog.show();
} catch (Exception e) {
e.printStackTrace();
}
// Setting values so that we can retrive the image from
// ImageView multiple times.
mImg.buildDrawingCache(true);
mImg.setDrawingCacheEnabled(true);
mTemplateImg.buildDrawingCache(true);
mTemplateImg.setDrawingCacheEnabled(true);
// Create new thread to crop.
new Thread(new Runnable() {
@Override
public void run() {
if (mSelectedVersion == VERSION_1) {
croppedImg = cropImage(photoImg, bitmap);
Common common = new Common();
if (className.equalsIgnoreCase("Fb_List")) {
storeImage(croppedImg, "overlay" + pageCount + ".png");
} else {
Common.storeCropImage(croppedImg, "overlay1.png",
MaskCropActivity.this);
photoImg.recycle();
}
} /*
* else { croppedImg = ImageProcess.cropImageVer2(
* mImg.getDrawingCache(true),
* mTemplateImg.getDrawingCache(true), 320, 440); }
*/
// }
try {
mProgressDialog.dismiss();
} catch (Exception e) {
// TODO: handle exception
}
mImg.setDrawingCacheEnabled(false);
mTemplateImg.setDrawingCacheEnabled(false);
}
}).start();
}
private boolean storeImage(Bitmap imageData, String filename) {
String iconsStoragePath = Common.SDCARD_PATH
+ Common.UNZIPPED_PATH
+ Common.getPrefrenceValue(Constants.ZIPFILE_NAME,
MaskCropActivity.this) + "/custom";
File sdIconStorageDir = new File(iconsStoragePath);
sdIconStorageDir.mkdirs();
try {
String filePath = sdIconStorageDir.toString() + "/" + filename;
FileOutputStream fileOutputStream = new FileOutputStream(filePath);
BufferedOutputStream bos = new BufferedOutputStream(
fileOutputStream);
System.out.println("imageData" + imageData);
imageData.compress(CompressFormat.PNG, 90, bos);
Intent intent = new Intent(MaskCropActivity.this,
DesignPriviewEditing.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
bos.flush();
bos.close();
} catch (FileNotFoundException e) {
return false;
} catch (IOException e) {
return false;
}
return true;
}
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
public boolean onTouch(View v, MotionEvent event) {
mScaleDetector.onTouchEvent(event);
mRotateDetector.onTouchEvent(event);
mMoveDetector.onTouchEvent(event);
float scaledImageCenterX = (mImageWidth * mScaleFactor) / 2;
float scaledImageCenterY = (mImageHeight * mScaleFactor) / 2;
mMatrix.reset();
mMatrix.postScale(mScaleFactor, mScaleFactor);
mMatrix.postRotate(mRotationDegrees, scaledImageCenterX,
scaledImageCenterY);
mMatrix.postTranslate(mFocusX - scaledImageCenterX, mFocusY
- scaledImageCenterY);
float[] values = new float[9];
mMatrix.getValues(values);
relativeX = (int) event.getX();
relativeY = (int) event.getY();
ImageView view = (ImageView) v;
view.setImageMatrix(mMatrix);
return true;
}
private class ScaleListener extends
ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
mScaleFactor = Math.max(0.1f, Math.min(mScaleFactor, 10.0f));
return true;
}
}
private class RotateListener extends
RotateGestureDetector.SimpleOnRotateGestureListener {
@Override
public boolean onRotate(RotateGestureDetector detector) {
mRotationDegrees -= detector.getRotationDegreesDelta();
return true;
}
}
private class MoveListener extends
MoveGestureDetector.SimpleOnMoveGestureListener {
@Override
public boolean onMove(MoveGestureDetector detector) {
PointF d = detector.getFocusDelta();
mFocusX += d.x;
mFocusY += d.y;
System.out.println("ON MOVE LISTSNER" + mFocusX + "MFOCUS Y"
+ mFocusY);
return true;
}
}
@Override
public void onBackPressed() {
Intent intent = new Intent(MaskCropActivity.this,
CropImagesActivity.class);
System.out
.println("pageCount in mASK CROP ON BACK PRESSED" + pageCount);
intent.putExtra("pageCount", pageCount);
startActivity(intent);
finish();
}
public Bitmap cropImage(Bitmap img, Bitmap templateImage) {
Bitmap bm = Bitmap.createBitmap((int) (320 * metrics.density),
(int) (416 * metrics.density), Bitmap.Config.ARGB_8888);
Canvas combineImg = new Canvas(bm);
combineImg.drawBitmap(img, relativeX, relativeY, null);
combineImg.drawBitmap(templateImage, 0, 0, null);
return bm;
}