View does not fit the canvas on canvas scale

柔情痞子 提交于 2019-12-07 15:22:11

问题


When I'm trying to scale my canvas to a draw SCALED view, my view is actually scaled, but view is getting clipped. (probably because of its layout parameters?)

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

simple image:

image after new onDraw called, for example when I click this button:

The button should be full sized when canvas is scaled. Do you have any ideas how to solve it?

p.s. call of

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

doesn't help.


I'm using MyDragShadowBuilder because I want my view to be double sized when I drag the view.

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");
            }
        }

I add my view into my Absolute Layout implementation with WRAP_CONTENT layout properties


回答1:


I ran into the same trouble. After some time i found a way to make it work :) This scales the original view by a factor of 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);
    }

}


来源:https://stackoverflow.com/questions/16191873/view-does-not-fit-the-canvas-on-canvas-scale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!