Android 将我的方法移动到另一个类后使用它

Android 将我的方法移动到另一个类后使用它,android,Android,我编写了一个使用OnTouchListener的代码,如下所示:当我单击我在图层上设置的Imageview时,我可以在屏幕上移动它(使用事件:DOWN、move和Up) 现在,我想将我在OnTouchListener中编写的代码行移动到另一个我命名的类:“DragAndDrop”,并在其中移动到一个我命名为“action”的方法。在我将代码移动到另一个类之前,该代码工作得非常好,移动后,我无法在屏幕上移动Imageview。此外,我还尝试将代码放入MainActivity类中的一个方法中,它工作

我编写了一个使用
OnTouchListener
的代码,如下所示:当我单击我在图层上设置的Imageview时,我可以在屏幕上移动它(使用事件:DOWN、move和Up)

现在,我想将我在OnTouchListener中编写的代码行移动到另一个我命名的类:“DragAndDrop”,并在其中移动到一个我命名为“action”的方法。在我将代码移动到另一个类之前,该代码工作得非常好,移动后,我无法在屏幕上移动Imageview。此外,我还尝试将代码放入MainActivity类中的一个方法中,它工作得非常好。只有当我将其移动到另一个类时,它才停止工作。 这是我写的:

关于主要活动:

public class MainActivity extends AppCompatActivity {

   public DragAndDrop dragAndDrop;

   public ImageView face_no_face;

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

        face_no_face = (ImageView) findViewById(R.id.face_no_face);
        face_no_face.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                dragAndDrop =   new DragAndDrop();
                dragAndDrop.action(v,   event,  face_no_face);

                return true;
            }
        });
    }
public class DragAndDrop {

    private float   x,y=0.0f;
    private boolean moving;

   public void action(View v, MotionEvent event,    ImageView   face_no_face) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                moving = true;
                break;

            case MotionEvent.ACTION_MOVE:
                if (moving) {
                    x = event.getRawX() - face_no_face.getWidth()   /2;
                    y = event.getRawY() - face_no_face.getHeight()  *3    /2;
                    face_no_face.setX(x);
                    face_no_face.setY(y);
                }
                break;

            case MotionEvent.ACTION_UP:
                moving = false;
                break;
        }

    }

}
在DragAndDrop类上:

public class MainActivity extends AppCompatActivity {

   public DragAndDrop dragAndDrop;

   public ImageView face_no_face;

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

        face_no_face = (ImageView) findViewById(R.id.face_no_face);
        face_no_face.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                dragAndDrop =   new DragAndDrop();
                dragAndDrop.action(v,   event,  face_no_face);

                return true;
            }
        });
    }
public class DragAndDrop {

    private float   x,y=0.0f;
    private boolean moving;

   public void action(View v, MotionEvent event,    ImageView   face_no_face) {

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                moving = true;
                break;

            case MotionEvent.ACTION_MOVE:
                if (moving) {
                    x = event.getRawX() - face_no_face.getWidth()   /2;
                    y = event.getRawY() - face_no_face.getHeight()  *3    /2;
                    face_no_face.setX(x);
                    face_no_face.setY(y);
                }
                break;

            case MotionEvent.ACTION_UP:
                moving = false;
                break;
        }

    }

}

非常感谢您的帮助!

您应该在
onTouch
函数之外初始化
dragandrop
类。只需在
onTouch
事件中调用
action
函数。@aborocz非常感谢您,兄弟,它工作正常!我是Android Studio的新手,所以我想我搞错了一件显而易见的事情,对吗?如果那么,你能告诉我为什么它现在能工作,而以前不能工作吗?它是否与子类的初始化必须在父类的构造函数上有关?或者是因为我不应该在@Overrid方法内部初始化类实例?如果是,那么为什么?