【发布时间】:2012-05-12 02:33:06
【问题描述】:
我正在尝试制作一个支持平移和缩放其内容的 ViewGroup。我在网上只能找到在 ImageView 中执行此操作的想法和实现,但绝不是容器。我想显示一个地图,在它上面我想显示多个标记,它们是 ImageButtons,所以用户可以点击它们来获取更多信息。这是通过 UIScrollView 在 iOS 上实现的,但我在 Android 上找不到替代方案。
我决定使用 FrameView,因此我可以设置一个 ImageView 以图像为背景,并在其顶部添加一个 RelativeLayout,我可以在其上添加 ImageButtons 并使用边距定位它们。
我借用了 TouchImageView here 的部分实现,但我遇到了麻烦。我已经开始平移,我部分成功了,它平移了容器,但是平移效果很糟糕,抖动很多。这是我的代码:
public class ScrollingViewGroup extends FrameLayout {
private int x = 0;
private int y = 0;
// We can be in one of these 3 states
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
int mode = NONE;
// Remember some things for zooming
PointF last = new PointF();
PointF start = new PointF();
public ScrollingViewGroup(Context context) {
super(context);
sharedConstructing(context);
}
public ScrollingViewGroup(Context context, AttributeSet attrs) {
super(context, attrs);
sharedConstructing(context);
}
private void sharedConstructing(Context context) {
super.setClickable(true);
this.context = context;
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
mScaleDetector.onTouchEvent(event);
PointF curr = new PointF(event.getX(), event.getY());
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
last.set(event.getX(), event.getY());
start.set(last);
mode = DRAG;
break;
case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
float deltaX = curr.x - last.x;
float deltaY = curr.y - last.y;
Log.d("ScrollingViewGroup", Float.toString(deltaX));
Log.d("ScrollingViewGroup", Float.toString(deltaY));
float scaleWidth = Math.round(origWidth * saveScale);
float scaleHeight = Math.round(origHeight * saveScale);
x += deltaX;
y += deltaY;
last.set(curr.x, curr.y);
}
break;
case MotionEvent.ACTION_UP:
mode = NONE;
int xDiff = (int) Math.abs(curr.x - start.x);
int yDiff = (int) Math.abs(curr.y - start.y);
if (xDiff < CLICK && yDiff < CLICK)
performClick();
break;
case MotionEvent.ACTION_POINTER_UP:
mode = NONE;
break;
}
// setImageMatrix(matrix);
setTranslationX(x);
setTranslationY(y);
invalidate();
return true; // indicate event was handled
}
});
}
非常感谢任何想法。
编辑:抖动似乎是原因,因为移动时,deltaX 和 deltaY 在正数和负数之间交替,检查 LogCat ......仍然不知道为什么。 这是由 curr 变量引起的,该变量每次都给出不同的值,但不是一致的,它们看起来好像手指会前后移动而不是向前移动。例如,curr.x 不是 0,1,2,3,4 等,而是 0,1,0.5,2,1.5 等。也不知道为什么。
【问题讨论】:
-
您找到解决方案了吗?我正在寻找完全相同的东西。带有按钮的缩放/平移地图
-
嘿,抱歉,我离开公司前一段时间无法提供代码,我确实找到了一些解决方法,但想不起我是如何做到的。
标签: android zooming viewgroup android-framelayout panning