2015-11-13 98 views
0

我有2个图像和一个可以更靠近地移动图像的按钮。现在我需要获得一个文本字段以显示两个图像之间的距离(以像素为单位)。因此,标签可以显示图像距离彼此20个像素,或者如果我移动图像更接近文本字段将显示19个像素。显示图像的距离测量

这里是我现在的代码

button.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       ((ViewGroup.MarginLayoutParams) red.getLayoutParams()).topMargin += 1; 
       red.requestLayout(); 
       ((ViewGroup.MarginLayoutParams) blue.getLayoutParams()).topMargin -= 1.5; 
       blue.requestLayout(); 
       text2.setText("" + (int) red.getCameraDistance()); 

的“text2.setText(‘’= ......”行代码,是我的尝试,试图找出2幅图像之间的距离。但是,没有工作......

+0

你有试过什么吗?刺穿它,看看你得到了什么。 –

回答

0

XML:

<ImageView android:id="@+id/image1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
.../> 

<ImageView android:id="@+id/image2" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
.../> 

<EditText 
android:id="@+id/distance" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
.../> 

活动:

EditText textDistance = (EditText) findViewById(R.id.distance); 
ImageView image1 = (ImageView) findViewById(R.id.image1); 
ImageView image2 = (ImageView) findViewById(R.id.image2); 

float distance = image2.getX()- image1.getX()+image1.getWidth(); 

textDistance.setText((String.valueOf(distance)); 
+0

我得到一个错误,当我添加'int distance = image2.getX“...错误说”需要int但发现浮动“ – user5560712

+0

我的不好,应该是浮动距离= image2.getX() - image1.getX()+ image1.getWidth();然后:) – Kamajabu

+0

当我改变'int'为float。text2.setText(distance);得到一个错误,错误说'不能解析方法setText(float)“ – user5560712