android - 如何在屏幕上拖动图像

标签 android

谁能帮我编写一个可以在屏幕上拖动的图像的程序?你能给我一个示例代码吗?多谢。我怎样才能为它做一个游戏得分。感谢您的回复。

最佳答案

imageView.setOnLongClickListener(new OnLongClickListener() {

            public boolean onLongClick(View v) {
                modeEdit = false;
                ClipData.Item item = new ClipData.Item((CharSequence) v.getTag());
                String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
                ClipData dragData = new ClipData(v.getTag().toString(), mimeTypes, item);

                // Instantiates the drag shadow builder.
                DragShadowBuilder myShadow = new MyDragShadowBuilder(treeBitmap);
                v.startDrag(dragData,  // the data to be dragged
                        myShadow,  // the drag shadow builder
                        treeBitmap,      // no need to use local data
                        0          // flags (not currently used, set to 0)
                );
                return true;
            }
        });

下面这段代码是设置拖动监听器

MyDragEventListener mDragListen = new MyDragEventListener();

这是拖动监听器

protected class MyDragEventListener implements OnDragListener {

        public boolean onDrag(View v, DragEvent event) {
            final int action = event.getAction();

            switch(action) {

                case DragEvent.ACTION_DRAG_STARTED:
                    Log.d(TAG, "FIELD ACTION_DRAG_STARTED");
                    if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) {
                        v.invalidate();
                        return(true);
                    } 
                    else {
                        return(false);
                    }

                case DragEvent.ACTION_DRAG_ENTERED:
                    Log.d(TAG, "FIELD ACTION_DRAG_ENTERED");
                    treePresentInMyView = true;
                    //Toast.makeText(getApplicationContext(), "Dragged data entered", Toast.LENGTH_LONG).show();
                    v.invalidate();
                    return(true);

                case DragEvent.ACTION_DRAG_LOCATION:
                    Log.d(TAG, "FIELD ACTION_DRAG_LOCATION");
                    positionX = event.getX();
                    positionY = event.getY();
                    layoutParams = v.getLayoutParams();
                    return(true);

                case DragEvent.ACTION_DRAG_EXITED:
                    Log.d(TAG, "FIELD ACTION_DRAG_EXITED");
                    modeEdit = false;
                    treePresentInMyView = false;
                    //Toast.makeText(getApplicationContext(), "Dragged data exited", Toast.LENGTH_LONG).show();
                    v.invalidate();
                    return(true);

                case DragEvent.ACTION_DROP:
                    Log.d(TAG, "FIELD ACTION_DROP");
                    keyOfTree = event.getClipDescription().getLabel().toString();
                    Log.d(TAG, "KEY VALUE = "+keyOfTree);

                    final ImageView view = new ImageView(getApplicationContext());
                    String treeWidth = getResources().getString(R.string.treeWidth);
                    String treeHeight = getResources().getString(R.string.treeHeight);

                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(Integer.parseInt(treeWidth), Integer.parseInt(treeHeight));
                    view.setLayoutParams(params);
                    view.setImageResource(R.drawable.tree1);

                    //to get image width and height:
                    BitmapFactory.Options o = new BitmapFactory.Options();
                    o.inTargetDensity = DisplayMetrics.DENSITY_DEFAULT;
                    Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.tree1, o);
                    int w = bmp.getWidth();
                    int h = bmp.getHeight();

                    Log.d(TAG, "width = " + w + " height = " +h);
                    params.leftMargin = (int) positionX - w;//-w and -h so dragged image placed not at mouse
                    params.topMargin = (int) positionY - h;//    but where actual image is on map

                    view.setVisibility(View.VISIBLE);
                    openField.addView(view);
                    LinearLayout.LayoutParams paramsOpenField = new LinearLayout.LayoutParams(openFieldWidth, openFieldHeight);
                    openField.setLayoutParams(paramsOpenField);

                    try {
                        Thread.sleep(1000);
                    } 
                    catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    v.invalidate();
                    return true;

                case DragEvent.ACTION_DRAG_ENDED:
                    Log.d(TAG, "FIELD ACTION_DRAG_ENDED");
                    v.invalidate();
                    if (event.getResult() & treePresentInMyView) {
                        Log.d(TAG, "(x,y) = ("+positionX+", "+positionY+")");
                        int subSubPlot = 0;
                        if (positionX > openField.measuredWidth/2) { //3 and 4
                            if (positionY > openField.measuredHeight/2) {
                                subSubPlot = 4;
                            }
                            else {
                                subSubPlot = 3;
                            }
                        }
                        else {
                            if (positionY > openField.measuredHeight/2) {
                                subSubPlot = 1;
                            }
                            else {
                                subSubPlot = 2;
                            }
                        }
                        double latitudeOfTree = mValues[0] + (Math.abs(mValues[0] - mValues[2]) / openField.measuredWidth) * positionX;
                        double longitudeOfTree = mValues[1] + (Math.abs(mValues[1] - mValues[3])/ openField.measuredHeight) * positionY;
//                      openField.drawTreeCoordinated(latitudeOfTree, longitudeOfTree, positionX, positionY);

                        return true;
                    } 
                    else {
                        return false;
                    }

                default:
                    Log.e(TAG,"Unknown action type received by OnDragListener.");
                break;
            };
            return false;
        }
    };

关于android - 如何在屏幕上拖动图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11006775/

相关文章:

android - 使用地理定位调用 map Intent ,是否可以在其上放置图钉?

android - SQLITE 批量插入以通过 UPDATE 和 INSERT 增加优化

Android - JSONObect 的 Put 方法在字符串中找到 '\' 时添加 '/'

android - Ant 编译错误 - NavUtils

android - Maven Android 插件的可变包名称

java - Android NullPointerException(getDatabaseLocked 和 getWritableDatabase)

java - 在 ListView 中为我自己的类创建一个 ArrayAdapter

android - IONIC 5 Cordova 插件可以构建,但在运行 android 时无法工作。它仅在 -livereload 中工作

android - 如何使用 Media Player android 随机播放列表歌曲

java - 使用 SQlite 数据库在自定义基础适配器 ListView 中存储复选框状态