我希望从图像中获取关于设备的遮蔽区域的XY坐标,并且想要从我的galary到触摸事件上的遮罩区域绘制所选图像。
有一个带有两个遮罩区域的图像,当我触摸第一个遮罩区域时想要从galary加载图像并需要在第一个遮罩区域中绘制它。
现在,我可以使用按钮单击事件通过两个相对于蒙版的单独按钮在这些蒙版区域中绘制图像。这些遮罩区域使用Photoshop计算,并在画布中绘制时从资产中使用。
有时图像可能包含多个遮罩区域,因此我想知道遮蔽区域的XY坐标,以便我可以在特定遮罩区域中绘制所选图像。
getleft和getTop值从资产加载 这些值使用照相馆计算并放置在资产中 img_left_2 = 765; img_top_2 = 245; img_right_2 = 1145; img_bottom_2 = 843
Heres是代码:
public void loadImagefromGallery(查看视图){
switch (view.getId()) {
case R.id.buttonLoadPicture1:
pictureIndex = 0;
break;
case R.id.buttonLoadPicture2:
pictureIndex = 1;
break;
}
Log.d(LOG_TAG, view.getId() + " : " + pictureIndex);
if (pictureIndex != -1) {
Intent galleryIntent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, Constants.RESULT_LOAD_IMG);
} else {
prepareImage();
}
}
private File getTempFile(String templateId, int pictureIndex) {
return new File(android.os.Environment.getExternalStorageDirectory(),
"temp_" + templateId + "_" + pictureIndex + ".jpg");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (requestCode == Constants.RESULT_LOAD_IMG
&& resultCode == RESULT_OK && null != data) {
Uri imageUri = data.getData();
System.out.println("Gallery Image URI : " + imageUri);
outPutFile = getTempFile(template.getId(), pictureIndex);
CropUtil.cropImage(this, imageUri,
template.getPictures()[pictureIndex], outPutFile);
} else if (requestCode == Constants.RESULT_CROP_IMG) {
try {
if (outPutFile.exists()) {
Log.d(LOG_TAG, "temp image : " + outPutFile.getPath());
} else {
Toast.makeText(getApplicationContext(),
"Error while save image", Toast.LENGTH_SHORT)
.show();
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
Toast.makeText(this, "You haven't picked Image",
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
}
}
public void prepareImage() {
ImageView mImageView = (ImageView) findViewById(R.id.imgView);
Bitmap result = Bitmap.createBitmap(template.getWidth(),
template.getHeight(), Config.ARGB_8888);
Canvas mCanvas = new Canvas(result);
for (int i = 0; i < template.getImageCount(); i++) {
File tempFile = getTempFile(template.getId(), i);
Log.d(LOG_TAG, "tempFile.exists() : " + tempFile.exists());
if (tempFile.exists()) {
String imgDecodableString = getTempFile(template.getId(), i)
.getPath();
Log.d(LOG_TAG, "Preparing image : " + imgDecodableString);
Bitmap originalImage = BitmapFactory
.decodeFile(imgDecodableString);
Log.d(LOG_TAG,
originalImage.getWidth() + ","
+ originalImage.getHeight());
mCanvas.drawBitmap(originalImage,
template.getPictures()[i].getLeft(),
template.getPictures()[i].getTop(), null);
recycleBitmap(originalImage);
}
}
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER));
Bitmap imageMask = Util.decodeSampledBitmapFromResource(getResources(),
R.drawable.template1_mask, template.getWidth()/2,
template.getHeight()/2);
mCanvas.drawBitmap(imageMask, 0, 0, paint);
paint.setXfermode(null);
mImageView.setImageBitmap(result);
mImageView.setScaleType(ScaleType.CENTER_INSIDE);
mImageView.setAdjustViewBounds(true);
mImageView.setBackgroundColor(Color.parseColor("#BED1CF"));
}
private static void recycleBitmap(Bitmap bitmap) {
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
bitmap = null;
}
}
答案 0 :(得分:0)
在触控侦听器上使用以获取X和Y位置
@Override
public boolean onTouchEvent(final MotionEvent event) {
// mScaleDetector.onTouchEvent(event);
gestureDetector.onTouchEvent(event);
handled = false;
actionIndex = event.getActionIndex();
CustomTextview touchTextView;
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
Log.e(TAG, "Down");
xTouch = (int) event.getX(0);
yTouch = (int) event.getY(0);
// check if we've touched inside some Bitmap
touchTextView = getActiveTextView(xTouch, yTouch);
break;
case MotionEvent.ACTION_POINTER_DOWN:
Log.e(TAG, "Pointer down");
xTouch = (int) event.getX(actionIndex);
yTouch = (int) event.getY(actionIndex);
break;
case MotionEvent.ACTION_MOVE:
pointerCount = event.getPointerCount();
handled = true;
xTouch = (int) event.getX(actionIndex);
yTouch = (int) event.getY(actionIndex);
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_UP:
se;
break;
case MotionEvent.ACTION_CANCEL:
break;
default:
// do nothing
break;
}
return true;
}
enter code here