@Override
 protected void onDetachedFromWindow() {
   super.onDetachedFromWindow();
   if (mProgressDrawable != null) {
     mProgressDrawable.stop();
     mProgressDrawable.setVisible(false, false);
   }
 }
  @Override
  protected void onAttachedToWindow() {
    super.onAttachedToWindow();
    if (mProgressDrawable != null) {
      mProgressDrawable.stop();
      mProgressDrawable.setVisible(getVisibility() == VISIBLE, false);

      requestLayout();
    }
  }
 @Override
 public void setVisibility(int visibility) {
   super.setVisibility(visibility);
   if (mProgressDrawable != null) {
     mProgressDrawable.setVisible(visibility == VISIBLE, false);
     if (visibility != VISIBLE) {
       mProgressDrawable.stop();
     } else {
       if (mProgressDrawable.isRunning()) {
         mProgressDrawable.stop();
       }
     }
   }
 }