Android Animation动画

简介: Android Animation动画

效果图

image.png


都在注释里了


补间动画

package com.yechaoa.viewanimationdemo;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView mImageView;
    private Button mBtnGone, mBtnVisible, mBtnMoveToLeft, mBtnMoveToRight,
            mBtnRotate, mBtnScale, mBtnAlpha, mBtnAnimGroup;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initListener();
    }
    private void initView() {
        mImageView = findViewById(R.id.imageView);
        mBtnGone = findViewById(R.id.btn_gone);
        mBtnVisible = findViewById(R.id.btn_visible);
        mBtnMoveToLeft = findViewById(R.id.btn_move_to_left);
        mBtnMoveToRight = findViewById(R.id.btn_move_to_right);
        mBtnRotate = findViewById(R.id.btn_rotate);
        mBtnScale = findViewById(R.id.btn_Scale);
        mBtnAlpha = findViewById(R.id.btn_alpha);
        mBtnAnimGroup = findViewById(R.id.btn_anim_group);
    }
    private void initListener() {
        mBtnGone.setOnClickListener(this);
        mBtnVisible.setOnClickListener(this);
        mBtnMoveToLeft.setOnClickListener(this);
        mBtnMoveToRight.setOnClickListener(this);
        mBtnRotate.setOnClickListener(this);
        mBtnScale.setOnClickListener(this);
        mBtnAlpha.setOnClickListener(this);
        mBtnAnimGroup.setOnClickListener(this);
    }
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_gone:
                //位移动画
                //4个数值分别表示坐标轴中以远点为中心的正负X轴Y轴 f表示float
                TranslateAnimation mHiddenAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF,
                        0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
                        Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                        -1.0f);
                //设置时长
                mHiddenAnimation.setDuration(500);
                mImageView.startAnimation(mHiddenAnimation);
                //0.0f 加一个渐变的效果
                mImageView.animate().alpha(0);
                mImageView.setVisibility(View.GONE);
                break;
            case R.id.btn_visible:
                TranslateAnimation mShowAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                        Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF,
                        -1.0f, Animation.RELATIVE_TO_SELF, 0.0f);
                mShowAnimation.setDuration(500);
                mImageView.startAnimation(mShowAnimation);
                mImageView.animate().alpha(1);
                mImageView.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_move_to_left:
                TranslateAnimation mLeftAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                        Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF,
                        0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
                mLeftAnimation.setDuration(500);
                mImageView.startAnimation(mLeftAnimation);
                mImageView.animate().alpha(0);
                mImageView.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_move_to_right:
                TranslateAnimation mRightAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
                        Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF,
                        0.0f, Animation.RELATIVE_TO_SELF, 0.0f);
                mRightAnimation.setDuration(500);
                mImageView.startAnimation(mRightAnimation);
                mImageView.animate().alpha(0);
                mImageView.setVisibility(View.VISIBLE);
                break;
            case R.id.btn_rotate:
                //旋转动画
                RotateAnimation mRotateAnimation = new RotateAnimation(0, 60, 0.5f, 0.5f);
                mRotateAnimation.setDuration(2000);
                mImageView.startAnimation(mRotateAnimation);
                break;
            case R.id.btn_Scale:
                /**
                 * 一共8个参数
                 * 前4个参数是分别是X轴、Y轴的起始位置和结束位置
                 * 后4个参数相对位置值,Animation.RELATIVE_TO_SELF表示自己,也就是以自己为中心点向四周缩放
                 */
                //缩放动画
                ScaleAnimation mScaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
                        Animation.RELATIVE_TO_SELF, 0.5f,
                        Animation.RELATIVE_TO_SELF, 0.5f);
                mScaleAnimation.setDuration(2000);
                //设置重复次数
                mScaleAnimation.setRepeatCount(2);
                //动画执行完后是否停留在执行完的状态
                mScaleAnimation.setFillAfter(true);
                //执行前的等待时间
                mScaleAnimation.setStartOffset(500);
                //取消动画
                //mScaleAnimation.cancel();
                mScaleAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                        Log.d("onAnimationStart", "动画开始");
                    }
                    @Override
                    public void onAnimationEnd(Animation animation) {
                        Log.d("onAnimationEnd", "动画结束");
                    }
                    @Override
                    public void onAnimationRepeat(Animation animation) {
                        Log.d("onAnimationRepeat", "动画重复");
                    }
                });
                mImageView.startAnimation(mScaleAnimation);
                break;
            case R.id.btn_alpha:
                //渐变动画
                AlphaAnimation mAlphaAnimation = new AlphaAnimation(1.0f, 0.0f);
                mAlphaAnimation.setDuration(2000);
                mAlphaAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {
                    }
                    @Override
                    public void onAnimationEnd(Animation animation) {
                        //淡出之后再淡入
                        //xml的方式引用,参数与代码设置一样
                        Animation aa = AnimationUtils.loadAnimation(MainActivity.this, R.anim.fade_in);
                        mImageView.startAnimation(aa);
                    }
                    @Override
                    public void onAnimationRepeat(Animation animation) {
                    }
                });
                mImageView.startAnimation(mAlphaAnimation);
                break;
            case R.id.btn_anim_group:
                /**
                 * 两种方式可以实现组合动画
                 * 1.给第一个动画设置监听,结束之后执行第二个动画
                 * 2.用AnimationSet,动画集合类
                 */
                RotateAnimation ra = new RotateAnimation(0, 360,
                        Animation.RELATIVE_TO_SELF, 0.5f,
                        Animation.RELATIVE_TO_SELF, 0.5f);
                ra.setDuration(2000);
                AlphaAnimation aa = new AlphaAnimation(0.0f, 1.0f);
                aa.setDuration(2000);
                ScaleAnimation sa = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
                        Animation.RELATIVE_TO_SELF, 0.5f,
                        Animation.RELATIVE_TO_SELF, 0.5f);
                sa.setDuration(2000);
                AnimationSet animationSet = new AnimationSet(true);
                animationSet.addAnimation(ra);
                animationSet.addAnimation(aa);
                animationSet.addAnimation(sa);
                //animationSet.setInterpolator(new DecelerateInterpolator(5));
                mImageView.startAnimation(animationSet);
                animationSet.start();
                /**
                 * Interpolator定义动画的速率
                 * AccelerateDecelerateInterpolator延迟减速,在动作执行到中间的时候才执行该特效。
                 * AccelerateInterpolator会使慢慢以(float)的参数降低速度
                 * CycleInterpolator曲线运动特效,要传递float型的参数。
                 * DecelerateInterpolator开头和结尾慢,中间快
                 * LinearInterpolator匀速
                 */
                break;
        }
    }
}


fade_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:duration="2000"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />
</set>


帧动画


<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item
        android:drawable="@mipmap/ic_launcher"
        android:duration="200" />
    <item
        android:drawable="@mipmap/ic_launcher"
        android:duration="200" />
    <item
        android:drawable="@mipmap/ic_launcher"
        android:duration="200" />
    <item
        android:drawable="@mipmap/ic_launcher"
        android:duration="200" />
    <item
        android:drawable="@mipmap/ic_launcher"
        android:duration="200" />
    <item
        android:drawable="@mipmap/ic_launcher"
        android:duration="200" />
</animation-list>

drawable是图片地址,oneshot=true表示只展示一遍,false表示循环

这个文件必须放在drawable文件下

目录
相关文章
|
Android开发 开发者
Android利用SVG实现动画效果
本文介绍了如何在Android中利用SVG实现动画效果。首先通过定义`pathData`参数(如M、L、Z等)绘制一个简单的三角形SVG图形,然后借助`objectAnimator`实现动态的线条绘制动画。文章详细讲解了从配置`build.gradle`支持VectorDrawable,到创建动画文件、关联SVG与动画,最后在Activity中启动动画的完整流程。此外,还提供了SVG绘制原理及工具推荐,帮助开发者更好地理解和应用SVG动画技术。
762 30
|
Android开发 UED 计算机视觉
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
本文介绍了一款受游戏“金铲铲之战”启发的Android自定义View——线条等待动画的实现过程。通过将布局分为10份,利用`onSizeChanged`测量最小长度,并借助画笔绘制动态线条,实现渐变伸缩效果。动画逻辑通过四个变量控制线条的增长与回退,最终形成流畅的等待动画。代码中详细展示了画笔初始化、线条绘制及动画更新的核心步骤,并提供完整源码供参考。此动画适用于加载场景,提升用户体验。
857 5
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
|
API Android开发 开发者
Android颜色渐变动画效果的实现
本文介绍了在Android中实现颜色渐变动画效果的方法,重点讲解了插值器(TypeEvaluator)的使用与自定义。通过Android自带的颜色插值器ArgbEvaluator,可以轻松实现背景色的渐变动画。文章详细分析了ArgbEvaluator的核心代码,并演示了如何利用Color.colorToHSV和Color.HSVToColor方法自定义颜色插值器MyColorEvaluator。最后提供了完整的源码示例,包括ColorGradient视图类和MyColorEvaluator类,帮助开发者更好地理解和应用颜色渐变动画技术。
534 3
|
存储 Shell Android开发
基于Android P,自定义Android开机动画的方法
本文详细介绍了基于Android P系统自定义开机动画的步骤,包括动画文件结构、脚本编写、ZIP打包方法以及如何将自定义动画集成到AOSP源码中。
1436 2
基于Android P,自定义Android开机动画的方法
|
Android开发 开发者
Android SVG动画详细例子
本文详细讲解了在Android中利用SVG实现动画效果的方法,通过具体例子帮助开发者更好地理解和应用SVG动画。文章首先展示了动画的实现效果,接着回顾了之前的文章链接及常见问题(如属性名大小写错误)。核心内容包括:1) 使用阿里图库获取SVG图形;2) 借助工具将SVG转换为VectorDrawable;3) 为每个路径添加动画绑定属性;4) 创建动画文件并关联SVG;5) 在ImageView中引用动画文件;6) 在Activity中启动动画。文末还提供了完整的代码示例和源码下载链接,方便读者实践操作。
720 65
|
XML Java Maven
Android线条等待动画JMWorkProgress(可添加依赖直接使用)
这是一篇关于Android线条等待动画JMWorkProgress的教程文章,作者计蒙将其代码开源至GitHub,提升可读性。文章介绍了如何通过添加依赖库使用该动画,并详细讲解了XML与Java中的配置方法,包括改变线条颜色、宽度、添加文字等自定义属性。项目已支持直接依赖集成(`implementation &#39;com.github.Yufseven:JMWorkProgress:v1.0&#39;`),开发者可以快速上手实现炫酷的等待动画效果。文末附有GitHub项目地址,欢迎访问并点赞支持!
499 26
|
XML Android开发 数据格式
Android中SlidingDrawer利用透明动画提示效果
本文介绍了在Android中使用`SlidingDrawer`实现带有透明动画提示效果的方法。通过XML布局配置`SlidingDrawer`的把手(handle)和内容(content),结合Activity中的代码实现动态动画效果。最终实现了交互性强、视觉效果良好的滑动抽屉功能。
255 1
Android中SlidingDrawer利用透明动画提示效果
|
XML Java Android开发
Android 动画之帧动画 + 补间动画 + 属性动画
本文介绍了Android开发中的三种动画类型:帧动画、补间动画和属性动画。帧动画通过依次播放一系列静态图片实现动态效果,支持Java代码与XML两种实现方式。补间动画基于起始和结束位置自动生成过渡效果,涵盖透明度、位移、旋转、缩放及组合动画等多种形式,并可搭配插值器优化动画过程。属性动画则通过改变对象属性实现动画,支持透明度、位移、旋转、缩放及组合动画,灵活性更高且适用于更复杂的场景。文中提供了详细的代码示例,帮助开发者快速上手。
683 15
|
Android开发 开发者
Android自定义view之围棋动画(化繁为简)
本文介绍了Android自定义View的动画实现,通过两个案例拓展动态效果。第一个案例基于`drawArc`方法实现单次动画,借助布尔值控制动画流程。第二个案例以围棋动画为例,从简单的小球直线运动到双向变速运动,最终实现循环动画效果。代码结构清晰,逻辑简明,展示了如何化繁为简实现复杂动画,帮助读者拓展动态效果设计思路。文末提供完整源码,适合初学者和进阶开发者学习参考。
307 0
Android自定义view之围棋动画(化繁为简)

热门文章

最新文章