pleerock
pleerock

Reputation: 18856

View does not fit the canvas on canvas scale

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:

enter image description here

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

enter image description here

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

Upvotes: 3

Views: 2613

Answers (1)

Jose Luis Pintado
Jose Luis Pintado

Reputation: 118

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

}

Upvotes: 4

Related Questions