其他分享
首页 > 其他分享> > android-视图在画布比例上不适合画布

android-视图在画布比例上不适合画布

作者:互联网

当我尝试将画布缩放到“绘制缩放”视图时,实际上是缩放了视图,但是视图被裁剪了. (可能是因为其布局参数?)

public void onDraw(Canvas canvas) {
    canvas.scale(2f, 2f);
    view.draw(canvas);
}

简单的图像:

调用新onDraw之后的图像,例如,当我单击此按钮时:

缩放画布时,按钮应为全尺寸.您有任何解决方法的想法吗?

ps.呼叫

view.invalidate();
view.requestLayout();

没有帮助.

我使用MyDragShadowBuilder是因为我希望在拖动视图时将视图放大两倍.

private final class MyDragShadowBuilder extends DragShadowBuilder {

        public MyDragShadowBuilder(View view) {
            super(view);
        }

        @Override
        public void onDrawShadow(Canvas canvas) {
            final View view = getView();
            if (view != null) {
                canvas.scale(2f, 2f);
                view.draw(canvas);
            } else {
                Log.e("DragShadowBuilder", "Asked to draw drag shadow but no view");
            }
        }

我将视图添加到具有WRAP_CONTENT布局属性的绝对布局实现中

解决方法:

我遇到了同样的麻烦.一段时间后,我找到了一种使它起作用的方法:)
这会将原始视图缩放4倍.

private static class MyDragShadowBuilder extends View.DragShadowBuilder {

    private static final int SCALING_FACTOR = 4;

    public MyDragShadowBuilder(View view) {
        super(view);
    }

    @Override
    public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
        View v = getView();
        final int width = v.getWidth() * SCALING_FACTOR;
        final int height = v.getHeight() * SCALING_FACTOR;
        shadowSize.set(width, height);
        shadowTouchPoint.set(width / 2, height / 2);
    }

    @Override
    public void onDrawShadow(Canvas canvas) {
        canvas.scale(SCALING_FACTOR, SCALING_FACTOR);
        getView().draw(canvas);
    }

}

标签:android-view,android-canvas,android
来源: https://codeday.me/bug/20191031/1972014.html