0

我有两个图像视图,我在其中使用 setImageBitmap 添加图像,并将 setOnTouchListener 应用于它们。但这里的问题是,第一次添加第一个 img 时,它通过触摸移动,但是当我添加第二个 img 时,第二个 img 移动,但之后我无法通过触摸移动第一个 img。对不起英语和提前感谢。

4

2 回答 2

0

您必须分别为两个图像提供 onTouchListener

 ImageView first_image=new ImageView(this);
   first_image.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_UP)
                {

                }
                if(event.getAction()==MotionEvent.ACTION_DOWN)
                {

                }
                if(event.getAction()==MotionEvent.ACTION_MOVE)
                {
                    //Code for image moving
                }
            return false;
        }
    });


        ImageView second_image=new ImageView(this);
    second_image.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
                if(event.getAction()==MotionEvent.ACTION_UP)
                {

                }
                if(event.getAction()==MotionEvent.ACTION_DOWN)
                {

                }
                if(event.getAction()==MotionEvent.ACTION_MOVE)
                {
                    //code for image moving
                }
            return false;
        }
    });
于 2012-06-09T05:29:58.090 回答
0

请为此尝试以下代码,它可能会对您有所帮助并使用 Imageview 而不是 Textview。

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;
    }
});
于 2012-06-09T05:39:27.363 回答