博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android缩放动画
阅读量:6584 次
发布时间:2019-06-24

本文共 5553 字,大约阅读时间需要 18 分钟。

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);

转载地址:http://xpxno.baihongyu.com/

你可能感兴趣的文章
pl/sql development 查询的数据复制到excel
查看>>
自定义指令的参数
查看>>
python实现进度条
查看>>
Android 一个应用启动另一个应用的说明
查看>>
阿里云CentOS7服务器利用LVM分区挂载磁盘全记录
查看>>
Setting up the Web Admin Tool in LDAP 6.x to communicate via SSL
查看>>
SQL好习惯:编写支持可搜索的SQL
查看>>
Shadowbox
查看>>
【 程 序 员 】:伤不起的三十岁,你还有多远 ?
查看>>
openldap安装
查看>>
[leetcode]count and say
查看>>
润乾报表 - 缓存问题
查看>>
利用IFormattable接口自动参数化Sql语句
查看>>
泛型Dictionary的用法详解
查看>>
明晰三种常见存储技术:DAS、SAN和NAS
查看>>
ContentProvider简单介绍
查看>>
Visual Studio 2014 CTPs 下载 和C# 6.0 语言预览版介绍
查看>>
js混淆 反混淆 在线
查看>>
WinForm 之 程序启动不显示主窗体
查看>>
FragmentTransaction.replace() 你不知道的坑
查看>>