Obtenir la position de contact à l'intérieur de l'imageview dans android

J'ai une imageview dans mon activité et je suis en mesure d'obtenir la position où l'utilisateur touche l'imageview, par le biais de onTouchListener. J'ai placé une autre image lorsque l'utilisateur touche plus que l'image. J'ai besoin de stocker la touche position(x,y), et de l'utiliser dans une autre activité, à afficher les étiquettes. J'ai stocké la position de contact dans la première activité. Dans la première activité, mon imageview en haut de l'écran. Dans la deuxième activité, de ses en bas de l'écran. Si j'utilise la position enregistrée à partir de la première acitvity, il place la balise de l'image en haut, et non pas sur l'imageview, où j'ai déjà cliqué dans la première activité. Est-il de toute façon à obtenir la position à l'intérieur de l'imageview.

FirstActivity:

cp.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
x = (int) event.getX();
y = (int) event.getY();
Log.v("touched x val of cap img >>", x + "");
Log.v("touched y val of cap img >>", y + "");
tag.setVisibility(View.VISIBLE);
int[] viewCoords = new int[2];
cp.getLocationOnScreen(viewCoords);
int imageX = x - viewCoords[0]; //viewCoods[0] is the X coordinate
int imageY = y - viewCoords[1]; //viewCoods[1] is the y coordinate
Log.v("Real x >>>",imageX+"");
Log.v("Real y >>>",imageY+"");
RelativeLayout rl = (RelativeLayout) findViewById(R.id.lay_lin);
ImageView iv = new ImageView(Capture_Image.this);
Bitmap bm = BitmapFactory.decodeResource(getResources(),
R.drawable.tag_icon_32);
iv.setImageBitmap(bm);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.leftMargin = x;
params.topMargin = y;
rl.addView(iv, params);
Intent intent= new Intent(Capture_Image.this,Tag_Image.class);
Bundle b=new Bundle();
b.putInt("xval", imageX);
b.putInt("yval", imageY);
intent.putExtras(b);
startActivity(intent);
return false;
}
});

Dans TagImage.java j'ai utilisé les éléments suivants:

im = (ImageView) findViewById(R.id.img_cam22);
b=getIntent().getExtras();
xx=b.getInt("xval");
yy=b.getInt("yval");
im.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
int[] viewCoords = new int[2];
im.getLocationOnScreen(viewCoords);
int imageX = xx + viewCoords[0]; //viewCoods[0] is the X coordinate
int imageY = yy+ viewCoords[1]; //viewCoods[1] is the y coordinate
Log.v("Real x >>>",imageX+"");
Log.v("Real y >>>",imageY+"");
RelativeLayout rl = (RelativeLayout) findViewById(R.id.lay_lin);
ImageView iv = new ImageView(Tag_Image.this);
Bitmap bm = BitmapFactory.decodeResource(getResources(),
R.drawable.tag_icon_32);
iv.setImageBitmap(bm);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
30, 40);
params.leftMargin =imageX ;
params.topMargin = imageY;
rl.addView(iv, params);
return true;
}
});
InformationsquelleAutor Manikandan | 2012-07-03