Chintan Raghwani
Chintan Raghwani

Reputation: 3370

Programatically execute another touch event when one touch event occures in android

I am making ImageView Drag and drop with creating duplicate instance by touching original one.

I am having one horizontal-layout containg many small images. when I touch on it it creates duplicate instance of it and puts at that position on screen slight different.

Now, I want to continue the touch event but want to finish up the first one touch event done on the original ImageView and want to start-up the touch event of duplicate instance of it. but with-out removing fingure up.

I have done following :

img22.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        ImageView img2 = null;

        switch (event.getActionMasked()) {
        case MotionEvent.ACTION_DOWN :
...   ...
...   ...
            Bitmap bmp2 = BitmapFactory.decodeResource(ImageListActivity.this.getResources(),
                R.drawable.img2);
            //ImageView 
            img2 = new ImageView(ImageListActivity.this);
            img2.setImageBitmap(bmp2);
            img2Param.setMargins( (rx-10-x), (ry-10-y-50), 0, 0);
            img2.setLayoutParams(img2Param);

            img2.setOnTouchListener(new OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {


                    System.out.println("   ---   img2 On Touch Called   ---");

                    RelativeLayout.LayoutParams img2Param =
                    (RelativeLayout.LayoutParams) v.getLayoutParams();
                    img2Param.setMargins((int)event.getRawX()-10, (int)event.getRawY()-60, 0, 0);
                    v.setLayoutParams(img2Param);
                    v.invalidate();
                        return true;
                    }
                });
            long downTime2 = SystemClock.currentThreadTimeMillis(),
                    eventTime2 = SystemClock.currentThreadTimeMillis() + 10;
            float x2 = 0.0f, y2 = 0.0f;
            int metaState2 = 0;
            MotionEvent motionEvent2 = MotionEvent.obtain(
                    downTime2, eventTime2, MotionEvent.ACTION_POINTER_1_UP, x2, y2, 1);
            v.dispatchTouchEvent(motionEvent2);

            long downTime = SystemClock.currentThreadTimeMillis(),
                    eventTime = SystemClock.currentThreadTimeMillis();// + 5000;
            float x1 = 0.0f, y1 = 0.0f;
                int metaState = 0;
            MotionEvent motionEvent = MotionEvent.obtain(
                    downTime, eventTime, MotionEvent.ACTION_MOVE, x1, y1, 1);
            img2.onTouchEvent(motionEvent);
...    ...
...    ...

Can any one give me Idea about continuing touch event and changing the instance of ImageView.

Upvotes: 1

Views: 1844

Answers (2)

Phil
Phil

Reputation: 36289

If I understand the problem right, you need to return false in img2's onTouch method in order to return the touch to the dispatcher (img22).

Upvotes: 0

Graeme
Graeme

Reputation: 25864

It sounds like what you want to do is click "Image1", have this action create an "Image2" and continue the touch gesture as if it was clicked on "Image2"?

Instead of doing this it might be a better idea to have a Touch Surface - An invisible view layered above these view's which captures the touch event and then performs the actions as needed - this will mean you can continue your gesture "off" the image without creating a new gesture.

Upvotes: 3

Related Questions