Jeff Bootsholz
Jeff Bootsholz

Reputation: 3068

Android imageview move drag within the limit

I have long been working the code for dragging the image within the relativelayout with all width and height wrap_content. When it comes to the execution, it shows the blinking arrow image to set the y-position of the imageview within the relative layout. Would you please tell me the way to construct the image drag vertically within the arbitrary limit . ?

The below is my code :

dragImage.setOnTouchListener(new OnTouchListener(){
                //private int _xDelta;
                private int _yDelta;
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                v.getParent().requestDisallowInterceptTouchEvent(true);



                final float y = event.getY();

                switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    //_yDelta = Y - lParams.topMargin;
                    mOldY2 = y;
                    break;
                case MotionEvent.ACTION_UP:
                    mViewPager.setPagingEnabled(true);
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    btn4.setBackgroundResource(R.drawable.fbl02);
                    final float dy = y - mOldY2;
                    mNewY2 += dy;
                    mOldY2 = y;
                    System.out.println(mNewY2);                 

                    while(mNewY2 > 224){
                        mNewY2 -= 224;
                    }

                    while(mNewY2 < 157){
                        mNewY2 += 157;
                    }


                    if(mNewY2 < 157 || mNewY2 > 224)
                        break;

                    v.setTranslationY((int)mNewY2);

                    v.invalidate();

                    float power = (float) ( 51.5/67 -(0.2/67) * mNewY2) ;
                    System.out.println(power);

                    Float roundF = new Float( Math.round(power));
                    midBandStick = roundF;
                    btn4.setText(String.valueOf(midBandStick) );
                    //}
                    //break;
                }
                return true;

            }

Upvotes: 0

Views: 3701

Answers (1)

Ankit Khare
Ankit Khare

Reputation: 1385

Try this, see if it helps you

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        parent = (ViewGroup) findViewById(R.id.root);
image = (ImageView) findViewById(R.id.image1);
        parent.post(new Runnable() {

            @Override
            public void run() {
                originalX = (int) image1.getX();
                originalY = (int) image1.getY();
            }
        });


                image.setOnTouchListener(this);
    }

    public boolean onTouch(View view, MotionEvent event) {
        final int X = (int) event.getRawX();
        final int Y = (int) event.getRawY();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view
                    .getLayoutParams();
            x = X - lParams.leftMargin;
            y = Y - lParams.topMargin;
            break;
        case MotionEvent.ACTION_UP:
            RelativeLayout.LayoutParams layout = (RelativeLayout.LayoutParams) view
                    .getLayoutParams();
            layout.leftMargin = originalX;
            layout.topMargin = originalY;
            view.setLayoutParams(layout);
            break;
        case MotionEvent.ACTION_MOVE:
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                    .getLayoutParams();
            layoutParams.leftMargin = X - x;
            layoutParams.topMargin = Y - y;
            view.setLayoutParams(layoutParams);
            break;

        }
        parent.invalidate();
        return true;
    }

This will let you drag image in a layout and when you leave the image it returns to it's original position.

Upvotes: 4

Related Questions