我正在尝试制作两个(或更多)可移动的TextViews ,并使用双指缩放使整个集合可扩展。我有从FrameLayout扩展的自定义布局:
public class BublinkyLayout extends FrameLayout {
private ScaleGestureDetector mScaleDetector;
private float mScaleFactor = 1.f;
private float scale = 1;
public BublinkyLayout(Context context) {
this(context, null, 0);
}
public BublinkyLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public BublinkyLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setWillNotDraw(false);
mScaleDetector = new ScaleGestureDetector(context, new ScaleListener());
}
public void setScale(float scale) {
this.scale = scale;
invalidate();
}
public float getScale() {
return this.scale;
}
@Override
protected void onDraw(Canvas canvas) {
canvas.scale(scale, scale);
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mScaleDetector.onTouchEvent(event);
final int action = MotionEventCompat.getActionMasked(event);
switch (action) {
case MotionEvent.ACTION_MOVE:
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) this.getLayoutParams();
this.setScale(mScaleFactor);
this.setLayoutParams(layoutParams);
this.invalidate();
break;
}
return true;
}
private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
mScaleFactor *= detector.getScaleFactor();
mScaleFactor = Math.max(0.3f, Math.min(mScaleFactor, 1.5f));
invalidate();
return true;
}
}
}
但是当我使用'捏合缩放'并缩小时,子TextViews没有“触摸区域”(我可以触摸它们并移动它们的区域)与它们对齐。看起来布局是缩放的,但是区域仍然是相同的,这意味着我无法将它们从原始框中取出。
这是我用来移动TextViews的OnTouchListener:
public class BublinkaOnTouchListener implements View.OnTouchListener {
private int _xDelta;
private int _yDelta;
@Override
public boolean onTouch(View view, MotionEvent event) {
final int X = (int) event.getRawX();
final int Y = (int) event.getRawY();
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
FrameLayout.LayoutParams lParams = (FrameLayout.LayoutParams) view.getLayoutParams();
_xDelta = X - lParams.leftMargin;
_yDelta = Y - lParams.topMargin;
break;
case MotionEvent.ACTION_UP:
break;
case MotionEvent.ACTION_POINTER_DOWN:
break;
case MotionEvent.ACTION_POINTER_UP:
break;
case MotionEvent.ACTION_MOVE:
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) view.getLayoutParams();
layoutParams.leftMargin = X - _xDelta;
layoutParams.topMargin = Y - _yDelta;
layoutParams.rightMargin = -250;
layoutParams.bottomMargin = -250;
view.setLayoutParams(layoutParams);
view.invalidate();
break;
}
return true;
}
}
感谢您的每一个答案。
PS:英语不是我的母语,对于错误抱歉:)