我正在使用此代码获取ImageView的X Y位置...在设备上工作正常,但使用仿真器时在logcat上却什么也没得到,这可以给我带来更好的结果。
import android.graphics.Matrix;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
public class Postion extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final ImageView iv = new ImageView(this);
setContentView(iv);
iv.setImageResource(R.drawable.constat);
View.OnTouchListener otl = new View.OnTouchListener() {
Matrix inverse = new Matrix();
@Override
public boolean onTouch(View v, MotionEvent event) {
iv.getImageMatrix().invert(inverse);
float[] pts = {
event.getX(), event.getY()
};
inverse.mapPoints(pts);
Log.d("cordonnés" , "onTouch x: " + Math.floor(pts[0]) + ", y: " + Math.floor(pts[1]));
return false;
}
};
iv.setOnTouchListener(otl);
}
}