我有一个相对布局,我通过onLongClick填充imageview,我的问题是我没有得到精确的坐标,每次我填充imageview它被放置在我的手指不在的点,我觉得如果我的代码:
,我错过了看下面其他应用程序的内容此方法随机生成一个imageview。
private ImageView generateImageView()
{
ImageView imageView = new ImageView(this);
imageView.setImageResource(R.drawable.image_1);
imageView.setAdjustViewBounds(true);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT );
imageView.setLayoutParams( params );
this.parentLayout.addView(imageView);
return imageView;
}
此方法将imageview移动到ontouch
中坐标提供的位置 public boolean onTouch(View view, MotionEvent motionEvent) {
this.absoluteClickedX = (int) motionEvent.getRawX();
Log.d("X Coordinate", ""+this.absoluteClickedX);
this.absoluteClickedY = (int) motionEvent.getRawY();
Log.d("Y Coordinate", ""+this.absoluteClickedY);
return false;
}
最后,此方法将imageview设置为新位置
private void setAbsoluteLocation(ImageView imageView, int x, int y)
{
RelativeLayout.LayoutParams alp = (RelativeLayout.LayoutParams) imageView.getLayoutParams();
alp.leftMargin = x-50;
alp.topMargin = y-50;
imageView.setLayoutParams(alp);
}
我哪里错了?抱歉我的英语不好