我试图允许用户从位置拖放到另一个位置。屏幕布局如下:
1 2 3
4 5 6
7 8 9
我希望用户抓取图像2,4,6或8并将其拖动到图像5.拖动到图像5后,我想加载一个片段。用户只能将图像从其当前位置沿直线拖动到5的位置。即图像2并且仅向下拖动直到它超出图像5,图像4只能向右拖动直到超过5等等。
非常感谢任何有关如何做到这一点的见解。
谢谢, DMAN
答案 0 :(得分:0)
所以我找到了让它发挥作用的方法。非常hacky,但我基本上使用图像位置,并通过我想要的直接边距转换到它的中心,直到它到达所需的位置。
我创建了一个临时图像(mTempImage)并隐藏了我的主图像,因为图像在布局中设置了参数,强制它保持在布局中的特定项目之上,并且不会让它比项目高度更小。
以下是来自onTouchListener的示例
int eventX = (int)event.getX();
int eventY = (int)event.getY();
int movingViewHeight = view.getHeight();
int movingViewWidth = view.getWidth();
int destinationTileTop = mTileHere.getTop();
int destinationTileLeft = mTileHere.getLeft();
// Transparent 'Tile Here' image (1)
// Transitional 'Tile Here' image (0 - 1)
// Opaque 'Tile Here' image (0)
float alphaMultiplier = 0;
switch(view.getId()) {
case R.id.item_position_2:
// Only allow sliding down from center of object (positive eventY)
if (eventY > movingViewHeight / 2) {
// Calculate the amount that the image has moved from it's center point
int posFromImgCenter = (eventY - movingViewHeight / 2);
// Check if the image has reached the center point and stop it's motion any farther
if (posFromImgCenter >= destinationTileTop) {
params.setMargins(view.getLeft(), destinationTileTop, 0, 0);
alphaMultiplier = 1;
}
// Slide the image with the positioning of the persons finger
else {
params.setMargins(view.getLeft(), posFromImgCenter, 0, 0);
alphaMultiplier = ((float)posFromImgCenter / (float)destinationTileTop);
}
}
// Attempting to slide in an invalid direction leave the image where it is
else
params.setMargins(view.getLeft(), view.getTop(), 0, 0);
... MORE CODE HERE FOR OTHER ITEM POSITIONS
// AFTER SWITCH STATEMENT COMPLETE UPDATE VIEW ITEMS ACCORDINGLY
// default 0 if not set with valid movement
mTileHere.setAlpha((int)(255 - (255 * alphaMultiplier)));
mTempImage.setLayoutParams(params);
mTempImage.invalidate();
谢谢, DMAN