当我触摸imageview的中心时,我想得到图像的中心坐标(x,y),即android中的(0,0)。请帮帮我。
这是获取坐标的代码:
这是我获取坐标的代码
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;
}
});
布局
<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;
}
});
答案 0 :(得分:1)
对于视图,event.getX()
,event.getY()
会返回相对坐标,如果您希望屏幕上的绝对坐标使用event.getRawX()
,event.getRawY()
修改强>
如果由于某种原因你需要你的中心为(0,0),你需要自己做出偏移,
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;
}
});