Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Drag and Drop an copy of ImageView

Tags:

android

How I can create a copy of ImageView on Drad and Drop? I searched in google, but i don't found nothing.

Please, help me.

like image 383
Regis Avatar asked Mar 20 '23 04:03

Regis


1 Answers

Here's a full example that appears to do most of what you want to do. It appears the only change required is to create a new ImageView and copy the bitmap into it.To do that, you'd need to alter the onDrag() method of the class MyDragListener to be something like this:

case DragEvent.ACTION_DROP:
    // Dropped, reassign View to ViewGroup
    View view = (View) event.getLocalState();
    //ViewGroup owner = (ViewGroup) view.getParent(); // Removed
    //owner.removeView(view);                         // Removed
    LinearLayout container = (LinearLayout) v;

    // Added the following to copy the old view's bitmap to a new ImageView:
    ImageView oldView = (ImageView) view;
    ImageView newView = new ImageView(getApplicationContext());
    newView.setImageBitmap(((BitmapDrawable) oldView.getDrawable()).getBitmap());

    container.addView(newView);                       // Changed to add new view instead

    view.setVisibility(View.VISIBLE);
    break;
like image 99
scottt Avatar answered Apr 02 '23 06:04

scottt