【问题标题】:How to disable right and left drag ImageView in Android?如何在Android中禁用左右拖动ImageView?
【发布时间】:2013-11-27 07:54:35
【问题描述】:

在我的应用程序中,我想通过触摸屏幕来拖放 Imageview。我可以在屏幕上以四个方向拖动它,但我只想将其拖到底部并禁用左右拖动 Imageview。

我在 srceen 中拖放 imageview 的代码:

public class MainActivity extends Activity implements OnTouchListener,
        OnDragListener {
    private static final String LOGCAT = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.textView1).setOnTouchListener(this);
        findViewById(R.id.pinkLayout).setOnDragListener(this);
        findViewById(R.id.yellowLayout).setOnDragListener(this);
    }

    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(null, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);
            return true;
        } else {
            return false;
        }
    }

    public boolean onDrag(View layoutview, DragEvent dragevent) {
        int action = dragevent.getAction();
        switch (action) {
        case DragEvent.ACTION_DRAG_STARTED:
            Log.d(LOGCAT, "Drag event started");
            break;
        case DragEvent.ACTION_DRAG_ENTERED:
            Log.d(LOGCAT, "Drag event entered into " + layoutview.toString());
            break;
        case DragEvent.ACTION_DRAG_EXITED:
            Log.d(LOGCAT, "Drag event exited from " + layoutview.toString());
            break;
        case DragEvent.ACTION_DROP:
            Log.d(LOGCAT, "Dropped");
            View view = (View) dragevent.getLocalState();
            ViewGroup owner = (ViewGroup) view.getParent();
            owner.removeView(view);
            LinearLayout container = (LinearLayout) layoutview;
            container.addView(view);
            view.setVisibility(View.VISIBLE);
            break;
        case DragEvent.ACTION_DRAG_ENDED:
            Log.d(LOGCAT, "Drag ended");
            break;
        default:
            break;
        }
        return true;
    }
}

如何解决? 谢谢

【问题讨论】:

    标签: android drag-and-drop android-imageview


    【解决方案1】:

    使用它可能会对您有所帮助...

    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
    savedMatrix.set(matrix);
    start.set(event.getX(), event.getY());
    Log.d(TAG, "mode=DRAG" );
    mode = DRAG;
    break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
    mode = NONE;
    Log.d(TAG, "mode=NONE" );
    break;
    case MotionEvent.ACTION_MOVE:
    if (mode == DRAG) {
    matrix.set(savedMatrix);
    matrix.postTranslate(event.getX() - start.x,
    event.getY() - start.y);
    }
    break;
    }
    

    【讨论】:

    • 静态最终 int NONE = 0;静态最终int DRAG = 1; int 模式 = NONE;
    • 它不工作,因为左右工作。我想禁用在屏幕左右移动
    猜你喜欢
    • 2010-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-10
    • 1970-01-01
    相关资源
    最近更新 更多