视图不适合画布上的画布

时间:2013-04-24 12:18:15

标签: android android-canvas android-view

当我尝试将画布缩放到绘制SCALED视图时,我的视图实际上是缩放的,但视图会被剪裁。 (可能是因为它的布局参数?)

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

简单图片:

enter image description here

调用新的onDraw后的

图像,例如当我单击此按钮时:

enter image description here

缩放画布时,按钮应为全尺寸。你有任何想法如何解决它?

P.S。致电

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布局属性将我的视图添加到Absolute Layout实现中

1 个答案:

答案 0 :(得分:4)

我遇到了同样的麻烦。过了一段时间,我找到了一种方法让它工作:) 这会将原始视图缩放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);
    }

}