Android缩放动画

核心方法

public void startAnimation(Animation animation)
  • 执行动画,参数可以是各种动画的对象,Animation的多态,也可以是组合动画,后面会有。

4个参数构造方法

/** * Constructor to use when building a ScaleAnimation from code *  * @param fromX Horizontal scaling factor to apply at the start of the animation * @param toX Horizontal scaling factor to apply at the end of the animation * @param fromY Vertical scaling factor to apply at the start of the animation * @param toY Vertical scaling factor to apply at the end of the animation */public ScaleAnimation(float fromX, float toX, float fromY, float toY) {    mResources = null;    mFromX = fromX;    mToX = toX;    mFromY = fromY;    mToY = toY;    mPivotX = 0;    mPivotY = 0;}

用法

public void scale(View view) {    // 创建缩放的动画对象    ScaleAnimation sa = new ScaleAnimation(0f,1.0f,0f,1.0f);    // 设置动画播放的时间    sa.setDuration(1000);    // 开始播放动画    iv.startAnimation(sa);}

效果

以图片左上角为原点,从没有,放大到图片原大小

6个参数构造方法

   /**    * Constructor to use when building a ScaleAnimation from code    *     * @param fromX Horizontal scaling factor to apply at the start of the animation    * @param toX Horizontal scaling factor to apply at the end of the animation    * @param fromY Vertical scaling factor to apply at the start of the animation    * @param toY Vertical scaling factor to apply at the end of the animation    * @param pivotX The X coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the left edge. (This point remains fixed while the object changes size.)    * @param pivotY The Y coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the top edge. (This point remains fixed while the object changes size.)    */   public ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY) {       mResources = null;       mFromX = fromX;       mToX = toX;       mFromY = fromY;       mToY = toY;       mPivotXType = ABSOLUTE;       mPivotYType = ABSOLUTE;       mPivotXValue = pivotX;       mPivotYValue = pivotY;       initializePivotPoint();   }
  • 前4个参数和上面的用法一样,后两个参数是设置图片缩放的原点,四个参数的构造默认将这两个参数都设置了0,所以是在图片左上角开始缩放

用法

ScaleAnimation sa = new ScaleAnimation(0f, 1.0f, 0f, 1.0f, iv.getWidth() / 2, iv.getHeight() / 2);// 设置动画播放的时间sa.setDuration(1000);// 开始播放动画iv.startAnimation(sa);

效果

以图片的中心为原点,从没有放大到图片原大小

8个参数构造方法

  /**    * Constructor to use when building a ScaleAnimation from code    *     * @param fromX Horizontal scaling factor to apply at the start of the animation    * @param toX Horizontal scaling factor to apply at the end of the animation    * @param fromY Vertical scaling factor to apply at the start of the animation    * @param toY Vertical scaling factor to apply at the end of the animation    * @param pivotXType Specifies how pivotXValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.    * @param pivotXValue The X coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the left edge. (This point remains fixed while the object changes size.) This value can either be an absolute number if pivotXType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.    * @param pivotYType Specifies how pivotYValue should be interpreted. One of Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or Animation.RELATIVE_TO_PARENT.    * @param pivotYValue The Y coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the top edge. (This point remains fixed while the object changes size.) This value can either be an absolute number if pivotYType is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.    */   public ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) {       mResources = null;       mFromX = fromX;       mToX = toX;       mFromY = fromY;       mToY = toY;       mPivotXValue = pivotXValue;       mPivotXType = pivotXType;       mPivotYValue = pivotYValue;       mPivotYType = pivotYType;       initializePivotPoint();   }

用法

// 创建缩放的动画对象ScaleAnimation sa = new ScaleAnimation(0f, 1.0f, 0f, 1.0f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f, ScaleAnimation.RELATIVE_TO_SELF, 0.5f);// 设置动画播放的时间sa.setDuration(1000);// 开始播放动画iv.startAnimation(sa);
  • 和上面6个参数的相比只是多了第5和第7个参数,分别设置他们的类型,注释里面已经说明了,可以设置Animation.ABSOLUTE、Animation.RELATIVE_TO_SELF、Animation.RELATIVE_TO_PARENT类型

效果

效果和上面一样,以图片的中心为原点,从没有放大到图片原大小。

设置动画重复播放的次数的方法

/** * Sets how many times the animation should be repeated. If the repeat * count is 0, the animation is never repeated. If the repeat count is * greater than 0 or {@link #INFINITE}, the repeat mode will be taken * into account. The repeat count is 0 by default. * * @param repeatCount the number of times the animation should be repeated * @attr ref android.R.styleable#Animation_repeatCount */public void setRepeatCount(int repeatCount) {    if (repeatCount < 0) {        repeatCount = INFINITE;    }    mRepeatCount = repeatCount;}

使用

sa.setRepeatCount(2);

设置动画重复播放的模式的方法

/** * Defines what this animation should do when it reaches the end. This * setting is applied only when the repeat count is either greater than * 0 or {@link #INFINITE}. Defaults to {@link #RESTART}. * * @param repeatMode {@link #RESTART} or {@link #REVERSE} * @attr ref android.R.styleable#Animation_repeatMode */public void setRepeatMode(int repeatMode) {    mRepeatMode = repeatMode;}

使用

sa.setRepeatMode(ScaleAnimation.REVERSE);

更多相关文章

  1. 浅谈Java中Collections.sort对List排序的两种方法
  2. mybatisplus的坑 insert标签insert into select无参数问题的解决
  3. Python技巧匿名函数、回调函数和高阶函数
  4. Python list sort方法的具体使用
  5. python list.sort()根据多个关键字排序的方法实现
  6. PopupWindow 动画显示与消失(逐惭缩放、逐惭透明)
  7. Android实现点击两次返回键退出
  8. Android(安卓)SDK下载和更新失败的解决方法
  9. Android自定义对话框(Dialog)位置,大小

随机推荐

  1. Dialog-普通对话框-列表对话框
  2. Android与JS交互---内嵌框架iframe
  3. Android Retrofit 图片上传的最简单和明
  4. Android 监听屏幕熄屏亮屏和主动唤醒屏幕
  5. android显示自定义view
  6. 利用Java反射机制-访问Android隐藏API
  7. android studio 启动失败(Failed to load
  8. Android 三星128G SD卡格式化为内部存储
  9. android内存管理工具类
  10. 从xml中改变checkBox大小和形状