如何为ViewGroup创建循环剪辑边界

时间:2017-01-06 15:15:44

标签: android animation canvas bounds clip

当我点击按钮时,我有ImageView,应该飞走了。为此,我使用ObjectAnimator为TRANSLATION_Y和TRANSLATION_X属性设置动画。

我需要为我的ImageView的父级定义圆形边界,以使其正确飞行。

为此,我使用下一个代码

public class CircleFrameLayout extends FrameLayout {

    private Path mClipPath = new Path();

    //Constructors

    @Override
    protected void onDraw(Canvas canvas) {
        mClipPath.reset();
        float radius = Math.min((float)getMeasuredWidth() / 2f, (float)getMeasuredHeight() / 2f) + 5;
        mClipPath.addCircle((float)getMeasuredWidth() / 2f, (float)getMeasuredHeight() / 2f, radius, Path.Direction.CCW);
        canvas.clipPath(mClipPath);
        super.onDraw(canvas);
    }
}

但没有任何反应。 ImageView使用其“父”的矩形边界而不是圆形边界。

有什么问题?

1 个答案:

答案 0 :(得分:2)

onDraw通常不会为ViewGroup类调用(例如您的自定义FrameLayout)。为了获得您想要的行为,请改为覆盖dispatchDraw

private Path mClipPath = new Path();

@Override
protected void dispatchDraw(Canvas canvas) {
    mClipPath.reset();
    float radius = Math.min((float)getMeasuredWidth() / 2f, (float)getMeasuredHeight() / 2f) + 5;
    mClipPath.addCircle((float)getMeasuredWidth() / 2f, (float)getMeasuredHeight() / 2f, radius, Path.Direction.CCW);
    canvas.clipPath(mClipPath);
    super.dispatchDraw(canvas);
}
相关问题