Android - 拖放并复制 ImageView

4

如何在拖放时创建ImageView的副本?我在谷歌上搜索了,但没有找到任何结果。

请帮忙。

2个回答

7

这里有一个完整的例子,看起来可以实现你想要做的大部分内容。唯一需要更改的是创建一个新的ImageView并将位图复制到其中。为了做到这一点,您需要修改MyDragListener类的onDrag()方法,使其类似于以下内容:

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;

java.lang.IllegalStateException: 指定的子视图已经有一个父视图。您必须先调用removeView()方法来移除该子视图的父视图。 - Regis
抱歉,我没有实际尝试过,并忘记了不能直接复制视图的事情。您需要创建一个新视图并将原始视图的位图复制到其中。我已经编辑了我的答案并进行了更改。 - scottt
这次我从那个网站构建了示例代码并测试了我的更改。 - scottt

0

当您单击特定图像时,动态创建一个ImageView,然后拖放动态创建的ImageView,这个想法怎么样?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接