Reputation: 73
When I touch the center of imageview I would like to get the center coordinate (x,y) of the image which is (0,0) in android. Please help me with that.
This is my code to get the coordinate :
this is my code to get the coordinate
imageAnim.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(),"x ="+ event.getX()+" y ="+ event.getY(),Toast.LENGTH_LONG).show();
return false;
}
});
layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background = "#000000"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
android:text="@string/hello_world"
android:textSize="100sp" />
<ZoomControls
android:id="@+id/zoomControls1"
android:layout_width="211dp"
android:layout_height="311dp"
android:layout_above="@+id/button2"
android:layout_alignParentRight="true"
android:layout_marginRight="156dp" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="211dp"
android:layout_height="211dp"
android:layout_above="@+id/textView1"
android:layout_marginBottom="51dp"
android:layout_marginLeft="165dp"
android:layout_toRightOf="@+id/button2"
android:contentDescription="@string/logo"
android:src="@android:drawable/menuitem_background" />
<ImageView
android:id="@+id/imageViewSecondPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/zoomControls1"
android:layout_toRightOf="@+id/zoomControls1"
android:src="@drawable/images" />
<Button
android:id="@+id/button2"
android:layout_width="100dp"
android:layout_height="110dp"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/imageView3"
android:layout_marginLeft="87dp"
android:text="next page " />
public class MainActivity extends Activity {
ImageView v;
MotionEvent event;
ZoomControls zoom;
ImageView imageAnim;
Button k;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageView imageAnim = (ImageView) findViewById(R.id.imageView3);
k=(Button) findViewById(R.id.button2);
final Intent n=new Intent (MainActivity.this,second.class);
k.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(n);
}
});
AnimationDrawable animation = new AnimationDrawable();
animation.addFrame(getResources().getDrawable(R.drawable.icon), 300);
animation.addFrame(getResources().getDrawable(R.drawable.images), 300);
animation.addFrame(getResources().getDrawable(R.drawable.sss), 300);
animation.setOneShot(false);
imageAnim.setBackgroundDrawable(animation);
animation.start();
imageAnim.setOnTouchListener(new View.OnTouchListener()
{
int offsetX = imageAnim.getWidth()/2 , offsetY = imageAnim.getHeight()/2;
@Override
public boolean onTouch(View v, MotionEvent event)
{
Toast.makeText(getBaseContext()," the off x ="+ offsetX +" the y = "+offsetY,Toast.LENGTH_LONG).show();
Toast.makeText(getBaseContext()," the x ="+ (event.getX()-offsetX) +
" the y ="+(event.getY()-offsetY) ,Toast.LENGTH_LONG).show();
return false;
}
});
Upvotes: 0
Views: 3756
Reputation: 5971
For a view, event.getX()
, event.getY()
returns the relative coordinates, if you want the absolute coordinates on screen use event.getRawX()
, event.getRawY()
EDIT
If for some reason you need your center to be (0,0) you need to make your own offset,
imageAnim.setOnTouchListener(new View.OnTouchListener()
{
int offsetX = getWidth()/2 , offsetY = getheight()/2;
@Override
public boolean onTouch(View v, MotionEvent event)
{
Toast.makeText(getBaseContext(),"x ="+ (event.getX()-offsetX) +
" y ="+(event.getY()-offsetY) ,Toast.LENGTH_LONG).show();
return false;
}
});
Upvotes: 1