使用ImageView拖放

时间:2017-03-23 12:46:57

标签: android drag-and-drop

我目前正在使用Android中的Drag and Drop项目。我想要完成的是,(如下面的链接所示)我将蘑菇拖到披萨面团上,然后,当我放下它时,我的现成蘑菇图片将与披萨面团一起看到。我已经制作了代码,但我不确定。对于蘑菇,我使用了一个按钮和披萨面团,我使用了ImageView。我已经尝试过运行它,但每次运行它时都会显示“很遗憾,您的应用已停止”

please see image for clearer understanding

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 个答案:

没有答案