【问题标题】:How to make circular clip bounds for ViewGroup如何为 ViewGroup 制作圆形剪辑边界
【发布时间】:2017-01-06 15:15:44
【问题描述】:

我有 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 使用其父级的矩形边界而不是圆形边界。

有什么问题?

【问题讨论】:

  • 覆盖draw,而不是onDraw
  • 不幸的是没有帮助
  • 添加一些Log.d 语句并确保addCircle 的参数正确

标签: android animation canvas bounds clip


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-09-22
    • 2021-03-10
    • 2011-05-15
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 2019-07-01
    • 1970-01-01
    • 2012-01-30
    相关资源
    最近更新 更多