使用ImageView拖放(新)

时间:2017-03-23 14:31:10

标签: android drag-and-drop overlapping

我目前正在使用Android中的Drag and Drop项目。我想要完成的是,(如下面的链接所示)我将蘑菇拖到披萨面团上,然后,当我放下它时,我的现成蘑菇图片将与披萨面团一起看到。

我已经制作了代码,但我不确定。对于蘑菇,我用了一个按钮和披萨面团,我使用了ImageView。以下代码有效。我现在想要完成的是如何通过拖放来叠加图像。就像下面的图片/链接演示它一样。

click to see picture

public class MainActivity extends AppCompatActivity {

    Button b1, b2, b3;
    // TextView txt; //
    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b1 = (Button) findViewById(R.id.button);
        b2 = (Button) findViewById(R.id.button1);
        b3 = (Button) findViewById(R.id.button2);

        // txt = (TextView) findViewById(R.id.textView); //
        image = (ImageView) findViewById(R.id.imageView);


        b1.setOnLongClickListener(longClickListener);
        b2.setOnLongClickListener(longClickListener);
        b3.setOnLongClickListener(longClickListener);

        image.setOnDragListener(dragListener);
    }

    View.OnLongClickListener longClickListener = new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            ClipData data = ClipData.newPlainText("", "");
            View.DragShadowBuilder myShadowBuilder = new View.DragShadowBuilder(v);
            v.startDrag(data, myShadowBuilder, v, 0);
            return true;
        }
    };

    View.OnDragListener dragListener = new View.OnDragListener() {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            int dragEvent = event.getAction();

            final View view = (View) event.getLocalState();

            switch (dragEvent) {
                case DragEvent.ACTION_DRAG_ENTERED:

                    if (view.getId() == R.id.button) {
                        image.setImageResource(R.drawable.pepperoni);
                    } else if (view.getId() == R.id.button1) {
                        image.setImageResource(R.drawable.mushrooms);
                    } else if (view.getId() == R.id.button2) {
                        image.setImageResource(R.drawable.pineapples);
                    }
                    break;
                case DragEvent.ACTION_DRAG_EXITED:
                    if (view.getId() == R.id.button) {
                        image.setImageResource(R.drawable.pizzadough);
                    } else if (view.getId() == R.id.button1) {
                        image.setImageResource(R.drawable.pizzadough);
                    } else if (view.getId() == R.id.button2) {
                        image.setImageResource(R.drawable.pizzadough);
                    }
                    break;
                case DragEvent.ACTION_DROP:
                    if (view.getId() == R.id.button) {
                        image.setImageResource(R.drawable.pepperoni);
                    } else if (view.getId() == R.id.button1) {
                        image.setImageResource(R.drawable.mushrooms);
                    } else if (view.getId() == R.id.button2) {
                        image.setImageResource(R.drawable.pineapples);
                    }
                    break;
            }

            return true;
        }
    };
}

0 个答案:

没有答案