Android动画之View Animation(视图动画)

本文介绍了Android中的视图动画View Animation,包括平移TranslateAnimation、旋转RotateAnimation、缩放ScaleAnimation和虚化AlphaAnimation。这些动画不会改变控件的实际位置,但展示了它们在Java代码和资源文件中的定义与使用。此外,还提到了Interpolator(插值器)的作用和部分内置插值器类型。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     平移TranslateAnimation旋转RotateAnimation缩放ScaleAnimation虚化AlphaAnimation 这四种动画就是View Animation。注意:视图动画并不会改变作用控件的实际位置,也就是getTop,getLeft的值都不会改变,包括view的大小和点击时间的热区范围等等. 本文介绍他们在java和资源文件中是是怎么定义和使用的,后续还会介绍属性动画和自定义动画等等。
1.在代码中定义视图动画
1).Animation:Animation是属性动画的基类,封装了属性动画的一些公用方法,下面列举了几个

//设置动画执行时间,以毫秒为单位
aa.setDuration(int);
//设置动画执行次数
aa.setRepeatCount(int);
//设置重复执行的样式 
// Animation.RESTART:到达终点时立即回到起点重新执行
// Animation.REVERSE:在终点和起点间来回执行动画
aa.setRepeatMode(int);
//true动画结束时保留view的位置,false结束后回到view原有位置
aa.setFillAfter(boolean);
//true动画开始时保留view的位置,false回到view原有位置后再开始动画
aa.setFillBefore(boolean);
//设置Interpolator(插值器,即动画变化的速率)
aa.getInterpolator(Interpolator);

2).Animation子类:上面的提到的四种动画都是Animation的子类,来看下各自的构造函数。

//TranslateAnimation类,平移动画
//以view原始位置的左上标为参照
//fromXDelta:开始X坐标,toXDelta:结束X坐标,fromYDelta:起始Y坐标,toYDelta:结束Y坐标
 public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)

//ScaleAnimation类,缩放动画
//fromX:X方向起始比例
//toX:X方向结束比例
//fromY:Y方向起始比例
//toY:Y方向起始比例
//pivotX:X缩放原点
//pivotY:Y缩放原点
public ScaleAnimation(float fromX, float toX, float fromY, float toY,float pivotX, float pivotY)

//AlphaAnimation类,透明度动画
//fromAlpha:起始透明度,toAlpha:结束透明度。0为全透明,1为全不透明
public AlphaAnimation(float fromAlpha, float toAlpha)

//AlphaAnimation类,旋转动画
/*fromDegrees和toDegrees是开始角度和结束角度,而pivotXType和pivotXValue是旋转中心的参照类型和中心X坐标值,其中pivotXType可取三个值
Animation.ABSOLUTE:旋转中心值是个坐标值,pivotXValue可取任意值,参照view左上角
Animation.RELATIVE_TO_SELF:围绕自身的某个点旋转(即自身旋转),pivotXValue可取0到1的值;
Animation.RELATIVE_TO_PARENT:围绕自身以外的某个点旋转,pivotXValue可取0到1的值,值越大旋转幅度越大;
其实上面三中动画也有构造函数可以用这三常量值,具体的自己试一下就知道了*/
public RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue,
            int pivotYType, float pivotYValue)

//AnimationSet类:动画集合,AnimationSet也是Animation的子类之一,他可以把几种动画加到一起给view同事执行
//shareInterpolator:是否然他包含的动画共享同一Interpolator
public AnimationSet(boolean shareInterpolator)

//上面五个类的其他构造函数就不多介绍了

3).简单事例

public class MainActivity extends AppCompatActivity {

    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView = (ImageView) findViewById(R.id.anim_iv);
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.translate_btn:
                TranslateAnimation ta = new TranslateAnimation(0,300,0,300);
                ta.setDuration(3000);
                imageView.startAnimation(ta);
                break;
            case R.id.rotate_btn:
                RotateAnimation ra = new RotateAnimation(0, 180);
                ra.setDuration(3000);
                imageView.startAnimation(ra);
                break;
            case R.id.scale_btn:
                ScaleAnimation sa = new ScaleAnimation(1, 3, 1, 3);
                sa.setDuration(3000);
                imageView.startAnimation(sa);
                break;
            case R.id.alpha_btn:
                AlphaAnimation aa = new AlphaAnimation(1, 0);
                aa.setDuration(3000);
                imageView.startAnimation(aa);
                break;
            case R.id.scale_alpha_btn:
                AlphaAnimation aa1 = new AlphaAnimation(1, 0);
                ScaleAnimation sa1 = new ScaleAnimation(1, 2, 1, 2);
                AnimationSet as = new AnimationSet(true);
                as.addAnimation(aa1);
                as.addAnimation(sa1);
                as.setDuration(3000);
                imageView.startAnimation(as);
                break;
        }
    }
}

效果
这里写图片描述

2.在资源中定义属性动画
1.在工程目录的res目录新建anim文件夹,在anim新建xml文件来定义animaion,xml文件名就该动画的资源id,一个xml可以上上面的四种view animation的一种或是一个animationSet。下面是定义的语法

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@[package:]anim/interpolator_resource"
    android:shareInterpolator=["true" | "false"] >
    <alpha
        android:fromAlpha="float"
        android:toAlpha="float" />
    <scale
        android:fromXScale="float"
        android:toXScale="float"
        android:fromYScale="float"
        android:toYScale="float"
        android:pivotX="float"
        android:pivotY="float" />
    <translate
        android:fromXDelta="float"
        android:toXDelta="float"
        android:fromYDelta="float"
        android:toYDelta="float" />
    <rotate
        android:fromDegrees="float"
        android:toDegrees="float"
        android:pivotX="float"
        android:pivotY="float" />
    <set>
        ...
    </set>
</set>

动画xml的根标签必须为<set>,<scale>,<translate>,<rotate>,<alpha>之一,xmlns:android=”http://schemas.android.com/apk/res/android”属性命名控件,上篇android自定义属性 有提到过。 android:interpolator可以指定一个在xml定义的插值器。shareInterpolator指定是否让animationSet下的动画使用同一插值器。下面scale,translate,rotate,alpha标签的属性含义和上面的各自构造函数的含义是一样的。如果跟标签是set则该标签下可以添加其他标签包括<set>标签。下面是定义的一些例子
这里写图片描述

scale.xml内容

<!--scale文件名是可以自己取的,真正起作用的是<scale>标签-->
<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXScale="1"
    android:toXScale="2"
    android:fromYScale="1"
    android:toYScale="2">
</scale>

如果其他xml文件(如布局文件)可以这样使用它:@anim/R.id.scale,在java代码中R.anim.scale就代表该动画id,下面java代码可以把它转换成Animation对象

//第一个参是Context对象
 Animation animation =  AnimationUtils.loadAnimation(this,R.anim.scale);

3.Interpolator简单介绍
Interpolator可以指定动画如何变化如减速变化,匀速变化,还可以指定变化的速率大小,目前android内部定义了下面的几种Interpolator,当然Interpolator也可自定义的算法。具体的有机再来研究。本文就到这。

Interpolator classResource ID
AccelerateDecelerateInterpolator@android:anim/accelerate_decelerate_interpolator
AccelerateInterpolator@android:anim/accelerate_interpolator
AnticipateInterpolator@android:anim/anticipate_interpolator
AnticipateOvershootInterpolator@android:anim/anticipate_overshoot_interpolator
BounceInterpolator@android:anim/bounce_interpolator
CycleInterpolator@android:anim/cycle_interpolator
DecelerateInterpolator@android:anim/decelerate_interpolator
LinearInterpolator@android:anim/linear_interpolator
OvershootInterpolator@android:anim/overshoot_interpolator
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值