在Android中旋转视图时调整视图大小
我正在开发一个示例应用程序,它允许在拖动时调整视图的大小 选择旋钮。当视图未旋转时,调整大小工作正常。当 视图是旋转的,如果我们调整视图的大小,它会偏移一定量。我 需要使视图的相对角保持在其原始位置 在调整视图大小时。谁能帮我理解这是怎么回事 可以在android中实现。调整大小的代码段如下所示:在Android中旋转视图时调整视图大小,android,rotation,drawing,Android,Rotation,Drawing,我正在开发一个示例应用程序,它允许在拖动时调整视图的大小 选择旋钮。当视图未旋转时,调整大小工作正常。当 视图是旋转的,如果我们调整视图的大小,它会偏移一定量。我 需要使视图的相对角保持在其原始位置 在调整视图大小时。谁能帮我理解这是怎么回事 可以在android中实现。调整大小的代码段如下所示: public void resize(float dx, float dy, SelectionView.KnobTypes selectedKnob) { float newDx = dx;
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 repo:
请参见我的答案。拉动拖动手柄时,我需要调整视图的大小。此外,当拖动控制柄时,视图不会从视图中心调整大小。拖动旋钮的另一个旋钮在调整大小时保持不变。请参见我的答案