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

android-如何在屏幕上拖动图像,第1张

概述谁能帮我编码可在屏幕上拖动图像的程序?您能给我一个示例代码吗?非常感谢.以及我该如何为它得分.我将感谢您的答复.解决方法:imageView.setOnLongClickListener(newOnLongClickListener(){publicbooleanonLongClick(Viewv){modeEdit=false

谁能帮我编码可在屏幕上拖动的图像的程序?您能给我一个示例代码吗?非常感谢.以及我该如何为它得分.我将感谢您的答复.

解决方法:

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-如何在屏幕上拖动图像全部内容,希望文章能够帮你解决android-如何在屏幕上拖动图像所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1085203.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存