我正在开发一个示例应用程序,它允许调整拖动视图的大小 选择旋钮。在不旋转视图时,调整大小可以正常工作。当。。。的时候 视图旋转,如果我们调整视图大小,它会偏移一定量。一世 需要使视图的对角保持在原始位置 而视图正在调整大小。任何人都可以帮助我理解这是怎么回事 在android中实现。调整大小的代码段如下:
public void resize(float dx, float dy, SelectionView.KnobTypes selectedKnob) {
float newDx = dx;
float newDy = dy;
FrameLayout.LayoutParams params;
float viewWidth, viewHeight, ratio, newWidth, newHeight;
viewWidth = getWidth();
viewHeight = getHeight();
ratio = 1;
switch (selectedKnob) {
case eLeftTop:
newWidth = viewWidth - dx;
newHeight = viewHeight - dy;
if (newWidth <= mKnobRadius * 4 || newHeight <= mKnobRadius * 4) {
break;
}
if (viewWidth > viewHeight) {
if (viewHeight != 0) {
ratio = viewWidth / viewHeight;
}
newWidth = newHeight * ratio;
} else {
if (viewWidth != 0) {
ratio = viewHeight / viewWidth;
}
newHeight = newWidth * ratio;
}
params = new FrameLayout.LayoutParams(Math.round(newWidth),
Math.round(newHeight));
setLayoutParams(params);
newDx = Math.round(newWidth) - Math.round(viewWidth);
newDy = Math.round(newHeight) - Math.round(viewHeight);
setTranslationX(getTranslationX() - newDx);
setTranslationY(getTranslationY() - newDy);
break;
case eTopRight:
newWidth = viewWidth + dx;
newHeight = viewHeight - dy;
if (newWidth <= mKnobRadius * 4 || newHeight <= mKnobRadius * 4) {
break;
}
if (viewWidth > viewHeight) {
if (viewHeight != 0) {
ratio = viewWidth / viewHeight;
}
newWidth = newHeight * ratio;
} else {
if (viewWidth != 0) {
ratio = viewHeight / viewWidth;
}
newHeight = newWidth * ratio;
}
params = new FrameLayout.LayoutParams(Math.round(newWidth),
Math.round(newHeight));
setLayoutParams(params);
newDx = Math.round(newWidth) - Math.round(viewWidth);
newDy = Math.round(newHeight) - Math.round(viewHeight);
setTranslationY(getTranslationY() - newDy);
break;
case eRightBottom:
newWidth = viewWidth + dx;
newHeight = viewHeight + dy;
if (newWidth <= mKnobRadius * 4 || newHeight <= mKnobRadius * 4) {
break;
}
if (viewWidth > viewHeight) {
if (viewHeight != 0) {
ratio = viewWidth / viewHeight;
}
newWidth = newHeight * ratio;
} else {
if (viewWidth != 0) {
ratio = viewHeight / viewWidth;
}
newHeight = newWidth * ratio;
}
params = new FrameLayout.LayoutParams(Math.round(newWidth),
Math.round(newHeight));
setLayoutParams(params);
break;
case eLeftBottom:
newWidth = viewWidth - dx;
newHeight = viewHeight + dy;
if (newWidth <= mKnobRadius * 4 || newHeight <= mKnobRadius * 4) {
break;
}
if (viewWidth > viewHeight) {
if (viewHeight != 0) {
ratio = viewWidth / viewHeight;
}
newWidth = newHeight * ratio;
} else {
if (viewWidth != 0) {
ratio = viewHeight / viewWidth;
}
newHeight = newWidth * ratio;
}
params = new FrameLayout.LayoutParams(Math.round(newWidth),
Math.round(newHeight));
setLayoutParams(params);
newDx = Math.round(newWidth) - Math.round(viewWidth);
newDy = Math.round(newHeight) - Math.round(viewHeight);
setTranslationX(getTranslationX() - newDx);
break;
default:
break;
}
}
dx和dy是鼠标移动的偏移量,选中的旋钮是当前被拖动的旋钮。
将工作样本上传到此github仓库: https://github.com/vivekrk/resizesample.git