【问题标题】:Cannot start this animator on a detached view无法在分离视图上启动此动画器
【发布时间】:2018-04-25 16:54:17
【问题描述】:

谁能告诉我这是怎么回事?

View view = findViewById(R.id.thumbnail_image_header); // thumbnail_image_header 是一个imageView

    int cx = (view.getLeft() + view.getRight()) / 2;
    int cy = (view.getTop() + view.getBottom()) / 2;

    // get the final radius for the clipping circle
    int dx = Math.max(cx, view.getWidth() - cx);
    int dy = Math.max(cy, view.getHeight() - cy);
    float finalRadius = (float) Math.hypot(dx, dy);

    // Android native animator
    Animator animator =
            ViewAnimationUtils.createCircularReveal(view, cx, cy, 0, finalRadius);
    animator.setInterpolator(new AccelerateDecelerateInterpolator());
    animator.setDuration(1500);
    animator.start();

【问题讨论】:

    标签: android android-animation circularreveal


    【解决方案1】:

    在MainLayout的addOnLayoutChangeListener下添加动画。

    mainView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
                view.removeOnLayoutChangeListener(this);
    
                //Add circular revel animation on activity start
                mainView.post(new Runnable() {
                    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
                    @Override
                    public void run() {
                        //Your Animation Code
                    }
                });
    
            }
        });
    

    【讨论】:

      【解决方案2】:

      您可以检查当前是否附加了视图。如果没有添加 OnAttachStateChangeListener 并在附加视图后立即启动动画。

      if (view.isAttachedToWindow()) {
      
          // startAnimation..
      
      } else {
          view.addOnAttachStateChangeListener(new OnAttachStateChangeListener() {
              @Override
              public void onViewAttachedToWindow(View v) {                
                  v.removeOnAttachStateChangeListener(this);
      
                  // startAnimation..
      
              }
              
              @Override
              public void onViewDetachedFromWindow(View v) {
              }
          });
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-08-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多