gpt4 book ai didi

安卓可拖动ImageVIew

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:04:13 24 4
gpt4 key购买 nike

我有一个 ImageView,我想在用户触摸和移动时在 Y 轴上重新定位它。我可以重新定位它,但是当我移动它时它会闪烁并且不平滑。它也不能完美地跟随我的手指。有没有办法消除闪烁并改善定位?我使用的是标准 ImageView。

这是我在 OnTouch 方法中的内容:

imageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()){
case MotionEvent.ACTION_DOWN: {
final float y = event.getY();

// Remember where we started
mLastTouchY = y;
}
break;

case MotionEvent.ACTION_MOVE:
final float y = event.getY();

// Calculate the distance moved
final float dy = y - mLastTouchY;

// Move the object
mPosY += dy;

// Remember this touch position for the next move event
mLastTouchY = y;

imageView.setTranslationY(mPosY);
return true;
}
return false;
}
});

最佳答案

你的图像只想沿着 Y 轴移动,而不是任何地方..如果这是你的问题,这就是答案

1.从这里下载项目(https://android-drag-and-drop-basic.googlecode.com/archive/942b651010cc6cad4376df1651123982c1a7474f.zip)

2.在DragAndDropBasicActivity类注释这行,我是怎么做到的

public boolean onTouch(View v, MotionEvent event) {
boolean eventConsumed = true;
int x = (int)event.getX();
int y = (int)event.getY();

int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN) {
if (v == letterView) {
dragging = true;
eventConsumed = false;
}
} else if (action == MotionEvent.ACTION_UP) {

// if (dragging) {
// emptyLetterView.getHitRect(hitRect);
// if (hitRect.contains(x, y))
// setSameAbsoluteLocation(letterView, emptyLetterView);
// }
dragging = false;
eventConsumed = false;

} else if (action == MotionEvent.ACTION_MOVE) {
if (v != letterView) {
if (dragging) {
setAbsoluteLocationCentered(letterView, x, y);
}
}
}

return eventConsumed;

}


private void setSameAbsoluteLocation(View v1, View v2) {
AbsoluteLayout.LayoutParams alp2 = (AbsoluteLayout.LayoutParams) v2.getLayoutParams();
setAbsoluteLocation(v1, alp2.x, alp2.y);
}

//This method helps the image to keep center of your touch point
private void setAbsoluteLocationCentered(View v, int x, int y) {
setAbsoluteLocation(v, x - v.getWidth() / 2, y - v.getHeight() / 2);
}

// this method help to place the image in exact position when your finger moved up (that is Action UP)
private void setAbsoluteLocation(View v, int x, int y) {
AbsoluteLayout.LayoutParams alp = (AbsoluteLayout.LayoutParams) v.getLayoutParams();

// don't forget to comment this line

//alp.x = x;
alp.y = y;
v.setLayoutParams(alp);
}

关于安卓可拖动ImageVIew,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25266065/

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