【问题标题】:Android : Get new position of View after animationAndroid:动画后获取视图的新位置
【发布时间】:2018-03-21 05:47:22
【问题描述】:

动画后如何获得新的视图位置?

if(animationCompeleted == 1)
{
    return;
}

if(animationCounter != 0)
{
    this.width = this.width + 1.f;
    this.heigh = this.heigh + 1.f;
}
final ScaleAnimation scale = new ScaleAnimation(this.width, this.width + 1.0f, this.heigh, this.heigh + 1.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1.0f);
scale.setDuration(1000);
scale.setFillAfter(true); 
scale.setFillEnabled(true);
scale.setAnimationListener(new Animation.AnimationListener()
{
    @Override
    public void onAnimationStart(Animation arg0) 
    {

        animationCompeleted = 1;
        animationCounter++;
    }           
    @Override
    public void onAnimationRepeat(Animation arg0) 
    {
        animationCompeleted = 2;
    }           
    @Override
    public void onAnimationEnd(Animation arg0) 
    {
        animationCompeleted = 2;
        //I want to get new position of imageView for comparing with another view(detection going-on it)


    }
});     
imageView.startAnimation(scale);

我想知道我的 imageView 何时靠近另一个视图。
怎么查?
如何在动画后获得新的 X 和 Y 或高度和宽度?
我想让它们进入 onAnimationEnd ,但它是静态值,动画结束后它不会改变..
对不起语法

【问题讨论】:

  • onAnimationEnd(): View.getX() & View.getY() 不起作用?
  • 这个值在动画后没有改变,我在动画前后得到相同的值
  • 哇,这违反直觉。你能在动画开始之前计算 newX 和 newY 吗?
  • 我没试过,也不知道该怎么做

标签: java android


【解决方案1】:

尝试 getLocationOnScreen 方法。

int[] locationOnScreent = new int[2];
yourView.getLocationOnScreen(locationOnScreent);
// locationOnScreent[0] = x
// locationOnScreent[1] = y

【讨论】:

  • 我在动画前后得到相同的值
  • 你可以试试这个:anim.setFillAfter(true); anim.setFillBefore(false); anim.setFillEnabled(true);
  • 我调整了,你可以在我的问题上看到
  • 对不起,没看到,我用谷歌搜索了一个惊人的答案。 stackoverflow.com/a/15808679/1928342
  • 如果您找到了解决方案,请在当前问题上发布您的答案。
【解决方案2】:

要获取视图的尺寸,请使用View.getLayoutParams().heightView.getLayoutParams().width

要获得位置,必须将 LayoutParams 转换为 MarginLayoutParams 并获取 leftMargin/topMargin/rightMargin/bottomMargin

【讨论】:

  • 动画前后 iew.getLayoutParams().height 的值相同
【解决方案3】:

这里的主要困惑是,您已经为视图的表示设置了动画,而不是视图本身。 因此,动画后视图的坐标从未改变。

此外,setFillAfter(true) 参数不会干扰该过程。 setFillAfter(true) 仍然只影响视图的表示。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 2012-07-11
    • 1970-01-01
    相关资源
    最近更新 更多