我的ImageView不能在我在xml中设置的位置上工作,但是何时它在左上角工作也很有限。那么如何调整位置?

时间:2018-11-18 19:07:07

标签: android xml android-studio imageview android-imageview

我在imageView上遇到问题,在添加layoutParams并设置宽度和高度后,我的图像在编译后进入了左上角。我该如何解决...谢谢andvance。 我是android studio的新手,所以不确定当前是否一切正常,然后开始制作一些小游戏,这种情况就发生了。

这是我的XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintGuide_end="100dp"
android:background="@drawable/spacebackground"
tools:context=".MainActivity">

<ImageView
    android:id="@+id/plane"
    android:layout_width="wrap_content"
    android:layout_height="150dp"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_marginLeft="0dp"
    android:layout_marginRight="119dp"
    android:layout_marginBottom="29dp"
    android:layout_x="116dp"
    android:layout_y="343dp"
    android:src="@drawable/warplane" />
  </RelativeLayout>

这是我的代码

package com.example.marko.warmachine;


  public class MainActivity extends Activity {

//variable
private ViewGroup mainLayout;
private ImageView image;
private int xDelta;
private int yDelta;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mainLayout = (RelativeLayout) findViewById(R.id.main);
    image = (ImageView) findViewById(R.id.plane);

    plane.setOnTouchListener(onTouchListener());
}

private View.OnTouchListener onTouchListener() {
    return new View.OnTouchListener() {

        @SuppressLint("ClickableViewAccessibility")
        @Override
        public boolean onTouch(View view, MotionEvent event) {

            final int x = (int) event.getRawX();
            final int y = (int) event.getRawY();

            switch (event.getAction() & MotionEvent.ACTION_MASK) {

                case MotionEvent.ACTION_DOWN:
                    RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams)
                            view.getLayoutParams();

                    xDelta = x - lParams.leftMargin;
                    yDelta = y - lParams.topMargin;
                    break;

                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;

                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                            .getLayoutParams();
                    layoutParams.leftMargin = x - xDelta;
                    layoutParams.topMargin = y - yDelta;
                    layoutParams.rightMargin = 0;
                    layoutParams.bottomMargin = 0;
                    view.setLayoutParams(layoutParams);
                    break;
            }
            mainLayout.invalidate();
            return true;
        }
    };
}

我是EDITET代码,所以这是我得到的但仍然无法使用。

1 个答案:

答案 0 :(得分:0)

您不应从头开始初始化layoutParams。从image视图实例本身获取先前定义的属性:

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) image.getLayoutParams();