开发者

Touch Event Problem of Image View

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开发者_StackOverflow中文版;

    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;
    }
}


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;
    }
    });


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..

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜