安卓开发---10 Android动画

  • animation-list帧动画
  • AnimationUtils与anim补间动画

1.animation-list帧动画

https://www.cnblogs.com/sishuiliuyun/p/5132713.html
第一步:先上图片素材,以下素材放到res/mipmap目录下:
第二步:上动画Animation-list帧布局文件,有2个,一个是按顺序显示动画,文件存放在res/drawable目录下
顺序显示动画文件:animation1.xml

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">      <!—是否只执行一次-->
    <item android:drawable="@mipmap/icon1" android:duration="800"></item>
    <item android:drawable="@mipmap/icon2" android:duration="800"></item>
    <item android:drawable="@mipmap/icon3" android:duration="800"></item>
    <item android:drawable="@mipmap/icon4" android:duration="800"></item>
    <item android:drawable="@mipmap/icon5" android:duration="800"></item>
    <item android:drawable="@mipmap/icon6" android:duration="800"></item>
</animation-list>

第三步主布局放控件

 <ImageView android:id="@+id/im"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="5px"
            android:src="@drawable/animation1"/>
放两个按钮开始、停止

第四步主页面代码:

private ImageView im;
private AnimationDrawable animationDrawable;
im=findViewById(R.id.img);
        Button btn=findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                im.setImageResource(R.drawable.animation1);
                animationDrawable = (AnimationDrawable) im.getDrawable();
                animationDrawable.start();
            }   });
        Button btn1=findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                animationDrawable = (AnimationDrawable) im.getDrawable();
                animationDrawable.stop();
            } });

结束监听:https://www.jianshu.com/p/dc66b371cd3b
getCurrent() 获取当前帧,得到的是一个 Drawable
然后想到能够获取指定帧的 Drawable,那么我们直接拿到最后一帧的 Drawable和当前帧做比较不就行了?就是如此
getFrame(int i) 获取指定帧的 Drawable
getNumberOfFrames() 获取总帧数
最终组合成核心代码
getFrame(getNumberOfFrames() -1) == getCurrent()

2.anim补间动画(指定开始结束其他由系统操作)
补间动画是一种设定动画开始状态、结束状态,其中间的变化由系统计算补充。这也是他叫做补间动画的原因。

补间动画由Animation类来实现具体效果,包括平移(TranslateAnimation)、缩放(ScaleAnimation)、旋转(RotateAnimation)、透明度(AlphaAnimation)四个子类,四种变化

平移:https://blog.csdn.net/qq_40881680/article/details/82255459
在res下创建anim文件夹,在这个文件夹(anim)下新建xml文件。

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fillAfter="false"
    android:fillBefore="true"
    android:fromXDelta="0"
android:fromYDelta="0"
android:toXDelta="520"
    android:toYDelta="520"	
    android:repeatCount="0"
    android:repeatMode="restart"
    android:startOffset="1000"
   
    >
</translate>

在这里插入图片描述
主布局放一个imageView

<ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/flower1"/>

主页面代码:

private ImageView im;
    private Animation animation;
im=findViewById(R.id.img);
        animation = AnimationUtils.loadAnimation(this,R.anim.anim_move);
        im.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                im.startAnimation(animation);
            }
        });

缩放:https://blog.csdn.net/qq_40881680/article/details/82260914
旋转:https://blog.csdn.net/qq_40881680/article/details/82261557
透明度:https://blog.csdn.net/qq_40881680/article/details/82261869
组合动画:https://blog.csdn.net/qq_40881680/article/details/82285987

扫描二维码关注公众号,回复: 10712603 查看本文章
发布了19 篇原创文章 · 获赞 27 · 访问量 1350

猜你喜欢

转载自blog.csdn.net/qq_44534541/article/details/105461722