Android scale animation on view

前端 未结 7 1041
轮回少年
轮回少年 2020-11-28 04:10

I want to use ScaleAnimation (programmatically not in xml) to change height to view from 0 to 60% of parent height. Width of view is constant and is 50px. View is empty onl

7条回答
  •  一个人的身影
    2020-11-28 04:48

    Resize using helper methods and start-repeat-end handlers like this:

    resize(
        view1,
        1.0f,
        0.0f,
        1.0f,
        0.0f,
        0.0f,
        0.0f,
        150,
        null,
        null,
        null);
    
      return null;
    }
    

    Helper methods:

    /**
     * Resize a view.
     */
    public static void resize(
      View view,
      float fromX,
      float toX,
      float fromY,
      float toY,
      float pivotX,
      float pivotY,
      int duration) {
    
      resize(
        view,
        fromX,
        toX,
        fromY,
        toY,
        pivotX,
        pivotY,
        duration,
        null,
        null,
        null);
    }
    
    /**
     * Resize a view with handlers.
     *
     * @param view     A view to resize.
     * @param fromX    X scale at start.
     * @param toX      X scale at end.
     * @param fromY    Y scale at start.
     * @param toY      Y scale at end.
     * @param pivotX   Rotate angle at start.
     * @param pivotY   Rotate angle at end.
     * @param duration Animation duration.
     * @param start    Actions on animation start. Otherwise NULL.
     * @param repeat   Actions on animation repeat. Otherwise NULL.
     * @param end      Actions on animation end. Otherwise NULL.
     */
    public static void resize(
      View view,
      float fromX,
      float toX,
      float fromY,
      float toY,
      float pivotX,
      float pivotY,
      int duration,
      Callable start,
      Callable repeat,
      Callable end) {
    
      Animation animation;
    
      animation =
        new ScaleAnimation(
          fromX,
          toX,
          fromY,
          toY,
          Animation.RELATIVE_TO_SELF,
          pivotX,
          Animation.RELATIVE_TO_SELF,
          pivotY);
    
      animation.setDuration(
        duration);
    
      animation.setInterpolator(
        new AccelerateDecelerateInterpolator());
    
      animation.setFillAfter(true);
    
      view.startAnimation(
        animation);
    
      animation.setAnimationListener(new AnimationListener() {
    
        @Override
        public void onAnimationStart(Animation animation) {
    
          if (start != null) {
            try {
    
              start.call();
    
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
    
        }
    
        @Override
        public void onAnimationEnd(Animation animation) {
    
          if (end != null) {
            try {
    
              end.call();
    
            } catch (Exception e) {
              e.printStackTrace();
            }
          }
        }
    
        @Override
        public void onAnimationRepeat(
          Animation animation) {
    
          if (repeat != null) {
            try {
    
              repeat.call();
    
            } catch (Exception e) {
              e.printStackTrace();
            }
          }  
        }
      });
    }
    

提交回复
热议问题