1

I have followed One of the video on YouTube that I can move image by pressing the image and move around, but the starting position is at the top left corner and I try to move it manually but still at the same position this is my code in my Activity.

Any help would be appreciated :)

public class Game extends AppCompatActivity {

private ImageView Plane;
private ViewGroup RootLayout;
private int _xDelta;
private int _yDelta;

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

    RootLayout = (ViewGroup)findViewById(R.id.main3);
    Plane = (ImageView)RootLayout.findViewById(R.id.airplane);

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(280, 280);
    Plane.setLayoutParams(layoutParams);
    Plane.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, 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) Plane.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) Plane.getLayoutParams();
                    layoutParams.leftMargin = X - _xDelta;
                    layoutParams.topMargin = Y - _yDelta;
                    layoutParams.rightMargin = -250;
                    layoutParams.bottomMargin = -250;
                    Plane.setLayoutParams(layoutParams);
                    break;
            }

            RootLayout.invalidate();
            return true;
        }
    });

}

}
Son Truong
  • 13,661
  • 5
  • 32
  • 58
iPR3DATOR
  • 11
  • 6

2 Answers2

0

It is about algorithm, I am writing Pseudo code, whatever the type of image:

int nScreenWidth = FuncSW();
int nScreenHeight= FuncSH();

int nImageWidth  = FuncIW();
int nImageHeight = FuncIH();

SetImagePosition( nScreenWidth/2 - nImageWidth/2, nScreenHeight/2-nImageHeight/2 );
// SetImagePosition(xpos,ypos) where xpos is the image left pos, ypos is image the top pos
asdf
  • 221
  • 1
  • 10
  • Can you explain it briefly because I don't understand it even when you stated it below of the code – iPR3DATOR Aug 19 '18 at 09:11
  • The relationship between the image you want to show and the screen, it can be equivalent as concentric circles. It means two different circle, the bigger one is the screen, the smaller one is the image. And They have the same circle center. – asdf Aug 19 '18 at 09:16
  • Glad to help you – asdf Aug 19 '18 at 13:24
0

Just go to xml file and set the layout gravity to 'center'

Scof Lj
  • 47
  • 1
  • 10