【问题标题】:Showing distance measurement of images显示图像的距离测量
【发布时间】:2015-11-13 23:40:33
【问题描述】:

我有 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("" =...." 行代码,是我试图找出两张图像之间的距离的尝试。但它没有用......

【问题讨论】:

  • 你有没有尝试过?尝试一下,看看你会得到什么。

标签: android image button textfield pixels


【解决方案1】:

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));

【讨论】:

  • 当我添加 'int distance = image2.getX" 时出现错误...错误显示“需要 int 但找到浮点数”
  • 我的错,应该是浮动距离 = image2.getX()- image1.getX()+image1.getWidth();然后:)
  • 在我将“int”更改为浮点数之后。 text2.setText(距离);得到一个错误。错误说'无法解析方法 setText(float)"
  • 然后android studio提示我一个解决方案,使用这个代码'text2.setText((int) distance);'然后错误消失了,但是当我尝试单击按钮时,应用程序崩溃了。
  • 是的,很抱歉忘记演员表,已经很晚了:P 你能告诉我当应用程序崩溃时你得到什么日志吗?
猜你喜欢
  • 2012-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-09
相关资源
最近更新 更多