触摸图像视图的事件问题

时间:2011-09-09 05:28:38

标签: android scaling android-imageview

我的问题是我正在移动ImageView的onTouchEvent图像,我在一个屏幕上有两个图像,但问题是我是否触摸image2&移动它是完美的工作,但如果我触摸image1那时image2移动到其原始位置,那么问题是什么?对不起英语沟通不好。

请帮助我。

以下是我的代码: -

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

2 个答案:

答案 0 :(得分:2)

使用这样你可以实现:

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

答案 1 :(得分:0)

虽然你在这里有更多的repu,但我不是看见,相对布局方向,水平或垂直?你的图像是并排或重叠,意味着,每当触摸任何图像时,图像位置onTouch事件可能会出现第二张图像问题。 对不起我的英语,如果你没有得到它..