gpt4 book ai didi

android - ScrollView 及其 subview 如何控制滚动

转载 作者:行者123 更新时间:2023-11-29 01:58:06 28 4
gpt4 key购买 nike

您好,我现在正在开发一个安卓应用程序,它是一个简单的模因制作工具。为了向 Canvas 添加元素(图片),我使用了像这样的自定义 View 作为 Canvas ...

<RelativeLayout>
<CustomScrollView....
<LinearLayout....
<CanvasView.....
</CanvasView>
</LinearLayout>
</CustomScrollView>
</RelativeLayout>

所有元素都将添加到 CanvasView,并且运行良好。当我对 CanvasView 中的元素添加拖放时,它有一个问题,很难在垂直方向拖放元素(水平方向很好),我认为这是因为 CustomScrollView,所以我将其删除,并且效果很好。

但是CanvasView曾经比屏幕尺寸的高度长,所以我需要CanvasView可以上下滚动,简单的我觉得ScrollView好用,如何解决“Touch Event”的问题"ScrollView 和 CanvasView 有冲突吗?

最佳答案

我已经尝试了一些时间并在这里找到了解决方案:

public class CanvasContainerScrollView extends ScrollView {
public boolean enableScrollView = true;

public CanvasContainerScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}

@Override
public boolean onTouchEvent(MotionEvent event) {
android.util.Log.v ("scrollview", "touched");
if (enableScrollView) {
return super.onTouchEvent(event);
}

return false;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (enableScrollView) {
return super.onInterceptTouchEvent(event);
}
return false;
}
}

设置 enableScrollView 将启用滚动或虎钳。在 CanvasView 上的 onTouchEvent -

@Override
public boolean onTouchEvent(MotionEvent ev) {
final int action = ev.getAction();

android.util.Log.v("canvasView", "touched");
switch (action) {
case MotionEvent.ACTION_DOWN: {

final float x = ev.getX();
final float y = ev.getY();

int i = 2;
for (i = goList.size() - 1; i >= 0; --i) {
if (currentObjectLayer == -1 && goList.get(i).inHitArea(x, y)) {

parent.enableScrollView = false;
if (currentObjectLayer != -1) {
continue;
}
goList.get(i).selected();
currentObjectLayer = i;
// break;
} else {
// if (goList.get(i).inHitArea(x, y)) {
// parent.enableScrollView = false;
// } else {
// parent.enableScrollView = true;
// }
goList.get(i).unselected();
}
}

if (currentObjectLayer == -1) {
parent.enableScrollView = true;
}
// Remember where we started
lastTouchX = x;
lastTouchY = y;
break;
}

case MotionEvent.ACTION_MOVE: {
final float x = ev.getX();
final float y = ev.getY();

// Calculate the distance moved
final float dx = x - lastTouchX;
final float dy = y - lastTouchY;

// Move the object
posX += dx;
posY += dy;

// Remember this touch position for the next move event
lastTouchX = x;
lastTouchY = y;

// android.util.Log.v("image", "dx=" + dx + ":dy=" + dy);
if (currentObjectLayer != -1) {
goList.get(currentObjectLayer).left += dx;
goList.get(currentObjectLayer).top += dy;
goList.get(currentObjectLayer).dx = dx;
goList.get(currentObjectLayer).dy = dy;
}

// Invalidate to request a redraw
invalidate();
break;
}

case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
// goList.get(currentObjectLayer).objectType = 0;
currentObjectLayer = -1;
parent.enableScrollView = true;
break;
}

invalidate();
return true;
}

关于android - ScrollView 及其 subview 如何控制滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14063664/

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