我有一个从 RelativeLayout 派生的自定义布局。我希望自定义布局根据放置它的容器布局的大小进行缩放。
这是我到目前为止的自定义布局:
public class ScaledRelativeLayout extends RelativeLayout {
private static final float HEIGHT = 460;
private static final float WIDTH = 380;
private float scale = 1.0f;
private float pivotX = 0.0f;
private float pivotY = 0.0f;
Matrix matrix = new Matrix();
public ScaledRelativeLayout( Context cxt, AttributeSet attrs ) {
super( cxt, attrs );
}
@Override
public void dispatchDraw( Canvas canvas ) {
canvas.save();
View parent = (View)getParent();
int parentHeight = parent.getHeight();
int parentWidth = parent.getWidth();
float scaleX = (float)parentWidth / WIDTH;
float scaleY = (float)parentHeight / HEIGHT;
scale = Math.min( scaleX, scaleY );
pivotX = (float)parentWidth / 2;
pivotY = 0;
canvas.scale( scale, scale, pivotX, pivotY );
super.dispatchDraw( canvas );
canvas.restore();
}
}
这几乎可以工作。问题是自定义视图由于某种原因在底部被裁剪。就好像在绘制视图时剪辑区域太小了。