0

My Problem is I am Moving the Image onTouchEvent of ImageView, I Have Two Images on one Screen but the problem is if i am touch image2 & move it that is perfectly working but if i touch on image1 that time the image2 is move to its original position, so what is the problem? Sorry for Bad English Communication.

Please Help Me.

Following is My Code:-

Main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ImageView android:layout_width="50sp" android:layout_height="50sp"
        android:id="@+id/image" android:src="@drawable/image">
    </ImageView>
    <ImageView android:layout_y="30dip" android:layout_x="118dip"
        android:layout_width="50sp" android:layout_height="50sp" android:id="@+id/image1"
        android:src="@drawable/image1">
    </ImageView>
</RelativeLayout>

MainActivity.java:-

public class MainActivity extends Activity implements OnTouchListener {
    int windowwidth;
    int windowheight;

    private RelativeLayout.LayoutParams layoutParams;
    private RelativeLayout.LayoutParams layoutParams1;

        ImageView image, image1;
    int x_cord, y_cord;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        windowwidth = getWindowManager().getDefaultDisplay().getWidth();
        windowheight = getWindowManager().getDefaultDisplay().getHeight();

        image = (ImageView) findViewById(R.id.image);
        image.setOnTouchListener(this);

        image1 = (ImageView) findViewById(R.id.image1);
        image1.setOnTouchListener(this);

    }

    public boolean onTouch(View v, MotionEvent event) {
        switch (v.getId()) {
        case R.id.image:
            System.out.println("Image is Touched");

            image = (ImageView) findViewById(R.id.image);
            layoutParams = (RelativeLayout.LayoutParams) image.getLayoutParams();
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                int x_cord = (int) event.getRawX();
                int y_cord = (int) event.getRawY();

                if (x_cord > windowwidth) {
                    x_cord = windowwidth;
                }
                if (y_cord > windowheight) {
                    y_cord = windowheight;
                }

                layoutParams.leftMargin = x_cord - 25;
                layoutParams.topMargin = y_cord - 75;

                image.setLayoutParams(layoutParams);
                break;
            default:
                break;
            }
        case R.id.image1:
            System.out.println("Image 1 is Touched");
            image1 = (ImageView) findViewById(R.id.image1);
            layoutParams1 = (RelativeLayout.LayoutParams) image1
                    .getLayoutParams();
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                break;
            case MotionEvent.ACTION_MOVE:
                x_cord = (int) event.getRawX();
                y_cord = (int) event.getRawY();

                if (x_cord > windowwidth) {
                    x_cord = windowwidth;
                }
                if (y_cord > windowheight) {
                    y_cord = windowheight;
                }

                layoutParams1.leftMargin = x_cord - 25;
                layoutParams1.topMargin = y_cord - 75;

                image1.setLayoutParams(layoutParams1);

                break;
            default:
                break;
            }
        }
        return true;
    }
}
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128

2 Answers2

2

use like this u can achieve:

tv1 = (TextView)findViewById(R.id.text_view1);
    tv1.setOnTouchListener(new View.OnTouchListener() {         

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            layoutParams1 = (RelativeLayout.LayoutParams) tv1.getLayoutParams();
           switch(event.getActionMasked())
           {
        case MotionEvent.ACTION_DOWN:
            break;
        case MotionEvent.ACTION_MOVE:
            int x_cord = (int) event.getRawX();
            int y_cord = (int) event.getRawY();
            if (x_cord > windowwidth) {
                x_cord = windowwidth;
            }
            if (y_cord > windowheight) {
                y_cord = windowheight;
            }
            layoutParams1.leftMargin = x_cord - 25;
            layoutParams1.topMargin = y_cord - 75;
            tv1.setLayoutParams(layoutParams1);
            break;
        default:
            break;
        }
        return true;
    }
    });

    tv2 = (TextView)findViewById(R.id.text_view2);
    tv2.setTextColor(Color.MAGENTA);
    tv2.setOnTouchListener(new View.OnTouchListener() {         

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            layoutParams2 = (RelativeLayout.LayoutParams) tv2.getLayoutParams();
           switch(event.getActionMasked())
           {
        case MotionEvent.ACTION_DOWN:
            break;
        case MotionEvent.ACTION_MOVE:
            int x_cord = (int) event.getRawX();
            int y_cord = (int) event.getRawY();
            if (x_cord > windowwidth) {
                x_cord = windowwidth;
            }
            if (y_cord > windowheight) {
                y_cord = windowheight;
            }
            layoutParams2.leftMargin = x_cord - 25;
            layoutParams2.topMargin = y_cord - 75;
            tv2.setLayoutParams(layoutParams2);
            break;
        default:
            break;
        }
        return true;
    }
    });
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
  • Your Code is Working with Image View also but Problem is If I am Using Big Resolution Image (for eg. 320*480) that time the Image is Not Moved Properly.if U have any idea then tell me. – Dipak Keshariya Sep 12 '11 at 06:19
  • Hello Raj How to Perform Zoom in,zoom out & rotation on this code. please help me. – Dipak Keshariya Sep 12 '11 at 06:43
  • with this small code it's not possible see this link : http://dl.dropbox.com/u/38493970/Full.java by using this it's possible panning and zooming integrate this code to u r code.. – RajaReddy PolamReddy Sep 12 '11 at 07:06
  • I integrate this code but problem is the image is scale in only fix width and height. – Dipak Keshariya Sep 12 '11 at 07:29
  • If i Remove Spanning Parts that time the same problem is occurred the Image View is Zoom in and Zoom out on its fix size means wrap_content. – Dipak Keshariya Sep 12 '11 at 07:38
  • if u r talking about zoom in out fixed size , change the parameter's maxZoom = 4; minZoom = 0.25f; you are desired values – RajaReddy PolamReddy Sep 12 '11 at 07:44
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/3360/discussion-between-dipak-keshariya-and-raj) – Dipak Keshariya Sep 12 '11 at 07:46
  • Zoom in & Zoom Out is not in Fixed size but the image is displayed in top order of layout & image size is increase but if i give image size to wrap_content that time the image is zoom in & zoom out on fix location. – Dipak Keshariya Sep 12 '11 at 07:49
0

though u have more repu here, but I am not seeeing,, relative layout orientation , horizontal or vertical?? are your images are side by side or overlapping,, means, there may be issue with image position onTouch event getting 2nd image every time when any image is touch.. sorry for my english too, if u r not getting it..

Abdul Wahab
  • 819
  • 1
  • 7
  • 18