gpt4 book ai didi

android - 当拖动 Imageview 到屏幕外时

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

我想在拖动时实现一个可移动的imageView。拖动工作正常。但是当我拖到最后时,imageView 会离开屏幕。 如何让 imageview 只在屏幕内移动?

这是我的 OnTouch 监听器:

public boolean onTouch(View view, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
dX = view.getX() - event.getRawX();
dY = view.getY() - event.getRawY();
lastAction = MotionEvent.ACTION_DOWN;
break;

case MotionEvent.ACTION_MOVE:
view.setY(event.getRawY() + dY);
view.setX(event.getRawX() + dX);
lastAction = MotionEvent.ACTION_MOVE;
break;

case MotionEvent.ACTION_UP:
if (lastAction == MotionEvent.ACTION_DOWN)
Toast.makeText(DraggableView.this, "Clicked!", Toast.LENGTH_SHORT).show();
break;

default:
return false;
}
return true;
}

这是我的布局。布局包含一个 ScrollView 及其 subview ,以及我要拖动的 ImageView 。

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true" >

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#ff0000"
android:gravity="center"
android:text="View 1"
android:textColor="#000"
android:textSize="100dp" />

<TextView
android:layout_width="match_parent"
android:layout_height="400dp"
android:background="#00ff00"
android:text="View 2"
android:textColor="#000"
android:textSize="100dp" />
</LinearLayout>
</ScrollView>

<ImageView
android:id="@+id/draggable_view"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_gravity="bottom|right"
android:layout_marginBottom="20dp"
android:layout_marginEnd="20dp"
android:background="@drawable/icon" />

</FrameLayout>

最佳答案

花了更多的时间,终于找到了最好的解决方案。

将我的 OnTouch 监听器部分修改为:

 DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
screenHight = displaymetrics.heightPixels;
screenWidth = displaymetrics.widthPixels;

@SuppressLint("NewApi") @Override
public boolean onTouch(View view, MotionEvent event) {


float newX, newY;

switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:

dX = view.getX() - event.getRawX();
dY = view.getY() - event.getRawY();
lastAction = MotionEvent.ACTION_DOWN;
break;

case MotionEvent.ACTION_MOVE:

newX = event.getRawX() + dX;
newY = event.getRawY() + dY;

// check if the view out of screen
if ((newX <= 0 || newX >= screenWidth-view.getWidth()) || (newY <= 0 || newY >= screenHight-view.getHeight()))
{
lastAction = MotionEvent.ACTION_MOVE;
break;
}

view.setX(newX);
view.setY(newY);

lastAction = MotionEvent.ACTION_MOVE;

break;

case MotionEvent.ACTION_UP:
if (lastAction == MotionEvent.ACTION_DOWN)
Toast.makeText(DraggableView.this, "Clicked!", Toast.LENGTH_SHORT).show();
break;

default:
return false;
}
return true;
}

它运行完美:)

关于android - 当拖动 Imageview 到屏幕外时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36402719/

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