gpt4 book ai didi

android - 如何在Android中禁用左右拖动ImageView?

转载 作者:行者123 更新时间:2023-11-29 00:25:09 25 4
gpt4 key购买 nike

在我的应用程序中,我想通过在屏幕中触摸来拖放 Imageview。我可以在屏幕上沿四个方向拖动它,但我只想在顶部拖动到 Botton 并禁用左右拖动 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;
}
}

如何解决?谢谢

最佳答案

使用这个可能会帮助你...

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;
}

关于android - 如何在Android中禁用左右拖动ImageView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20236767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com