Android的三种动画详解(帧动画,View动画,属性动画)

Android的三种动画详解(帧动画、View动画、属性动画)_android动画效果大全-CSDN博客

1、帧动画

缺点是:占用内存较高,播放的是一帧一帧的图片,很少使用

顺序播放预先定义的图片,类似于播放视频。

步骤:

1).在drawable文件夹下创建一个animation_picture.xml文件,Root element选择为animation-list.

具体为:右键点击drawable文件夹->New→Drawable Resource File

2)配置自己需要播放的图片

<?xml version="1.0" encoding="utf-8"?>

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"

    android:oneshot="false">

    <item android:drawable="@drawable/food1" android:duration="500"/>

    <item android:drawable="@drawable/food2" android:duration="500"/>

    <item android:drawable="@drawable/food3" android:duration="500"/>

    <item android:drawable="@drawable/laojunshan1" android:duration="500"/>

    <item android:drawable="@drawable/laojunshan2" android:duration="500"/>

</animation-list>

3).将animation_picture.xml设置为imageview的播放资源

package com.example.animationtest;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.drawable.AnimationDrawable;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private ImageView mImageView;

    private AnimationDrawable animationDrawable = null;

    private boolean flag = true;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.animationButton);

        mImageView = (ImageView)findViewById(R.id.image_view);

        mImageView.setBackgroundResource(R.drawable.animation_picture);//设置资源文件

        animationDrawable = (AnimationDrawable) mImageView.getBackground();

        button.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                if (flag)

                {

                    animationDrawable.start();//开启动画

                    flag = false;

                }else{

                    animationDrawable.stop();

                    flag = true;

                }

            }

        });

    }

}

2、View动画

View动画是补间动画,设定起始和终止位置,中间会自动补齐,有平移、缩放、旋转、透明四种选择。对应的类为TranslateAnimation、ScaleAnimation、RotateAnimation、AlphaAnimation。

优点:效率高,使用方便。

缺点:交互性差,当动画结束后会回到初始位置,对于交互性要求较高的使用属性动画。

可以使用xml配置资源文件实现,也可以用代码实现,这里用代码实现。其中包含按钮控制动画和默认显示组合动画。

Bar.java

package com.example.viewanimationtest;

import androidx.annotation.NonNull;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;

import android.os.Bundle;

import android.os.Handler;

import android.os.Looper;

import android.os.Message;

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.RotateAnimation;

import android.view.animation.ScaleAnimation;

import android.view.animation.TranslateAnimation;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.Toast;

/*

*Since the child thread cannot directly modify the main UI,

*it is implemented using the Handler mechanism.

* */

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ImageView mImageView;

    public static final int TRANSLATE_ANI = 1;

    public static final int ROTATE_ANI = 2;

    public static final int SCALE_ANI = 3;

    public static final int ALPHA_ANI = 4;

    private Handler mHandler = new Handler(Looper.getMainLooper()){

        @Override

        public void handleMessage(@NonNull Message msg) {

            switch(msg.what)

            {

                case TRANSLATE_ANI:

                    mImageView.clearAnimation();//When setting a new animation, first clear the previous animation.

                    Animation translateAnimation = new TranslateAnimation(0,500,

                            0,500);//The animation moves from(0,0)to (500,500).

                    translateAnimation.setDuration(2000);

                    mImageView.setAnimation(translateAnimation);

                    break;

                case ROTATE_ANI:

                    mImageView.clearAnimation();

                    Animation rotateAnimation = new RotateAnimation(0,360,

                            0,0);//The animation rotates from 0° to 360° around(0,0).

                    rotateAnimation.setDuration(2000);

                    mImageView.setAnimation(rotateAnimation);

                    break;

                case SCALE_ANI:

                    mImageView.clearAnimation();

                    Animation scaleAnimation = new ScaleAnimation(0,1,0,1);

                    scaleAnimation.setDuration(2000);

                    mImageView.setAnimation(scaleAnimation);

                    break;

                case ALPHA_ANI:

                    mImageView.clearAnimation();

                    Animation alphaAnimation = new AlphaAnimation(0,1);

                    alphaAnimation.setDuration(2000);

                    mImageView.setAnimation(alphaAnimation);

                    break;

                default:

                    Log.d("1111","default");

                    break;

            }

        }

    };

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        mImageView =(ImageView)findViewById(R.id.image_view);

        Button translateButton = (Button) findViewById(R.id.translate_button);

        Button scaleButton = (Button) findViewById(R.id.scale_button);

        Button rotateButton = (Button) findViewById(R.id.rotate_button);

        Button alphaButton = (Button) findViewById(R.id.alpha_button);

        translateButton.setOnClickListener(this);

        scaleButton.setOnClickListener(this);

        rotateButton.setOnClickListener(this);

        alphaButton.setOnClickListener(this);

        mImageView.clearAnimation();//If some animations exist, clear them first.

        //set mix animation

        AnimationSet animationSet = new AnimationSet(true);

        //set default alpha animation

        Animation alphaAnimation = new AlphaAnimation(0,1);//The animation is from fully transparent to  fully opaque.

        alphaAnimation.setDuration(2000);//The animation lasts 2 seconds.

        //set default scale animation

        Animation scaleAnimation = new ScaleAnimation(0,1,0,1);//The animation is from 0% to 100%.

        scaleAnimation.setDuration(2000);

        //add sub-animations to combined animation

        animationSet.addAnimation(alphaAnimation);

        animationSet.addAnimation(scaleAnimation);

        //show the combined animation

        mImageView.setAnimation(animationSet);

    }

    @SuppressLint("NonConstantResourceId")

    @Override

    public void onClick(View v) {

        switch (v.getId())

        {

            case R.id.translate_button:

                new Thread(new Runnable() {

                    @Override

                    public void run() {

                        Message msg = new Message();

                        msg.what = TRANSLATE_ANI;

                        mHandler.sendMessage(msg);

                    }

                }).start();

                break;

            case R.id.rotate_button:

                new Thread(new Runnable() {

                    @Override

                    public void run() {

                        Message msg = new Message();

                        msg.what = ROTATE_ANI;

                        mHandler.sendMessage(msg);

                    }

                }).start();

                break;

            case R.id.scale_button:

                new Thread(new Runnable() {

                    @Override

                    public void run() {

                        Message msg = new Message();

                        msg.what = SCALE_ANI;

                        mHandler.sendMessage(msg);

                    }

                }).start();

                break;

            case R.id.alpha_button:

                new Thread(new Runnable() {

                    @Override

                    public void run() {

                        Message msg = new Message();

                        msg.what = ALPHA_ANI;

                        mHandler.sendMessage(msg);

                    }

                }).start();

                break;

            default:

                break;

        }

    }

}

具体效果

​编辑view.mp4

3.属性动画

跟补间动画类似。具体内容可以参考文章:Android的三种动画详解(帧动画、View动画、属性动画)

优点:交互性强,动画结束时的位置就是最终位置。详细使用可参考:Android进阶之光  书籍

代码实现

Bar.java

package com.example.propertyanimation;

import androidx.appcompat.app.AppCompatActivity;

import android.animation.Animator;

import android.animation.AnimatorSet;

import android.animation.ObjectAnimator;

import android.os.Bundle;

import android.view.animation.AnimationSet;

import android.widget.ImageView;

import java.util.Set;

public class MainActivity extends AppCompatActivity {

    private ImageView mImageview;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        mImageview = (ImageView) findViewById(R.id.image_view);

        //use AnimatorSet to show the combined animation

        AnimatorSet animatorSet = new AnimatorSet();

        //Use the ofFloat function to construct an ObjectAnimator object and set the alpha parameter.

        ObjectAnimator animator1 = ObjectAnimator.ofFloat(mImageview,

                "alpha",0f,1.0f);

        //Set the translationY parameter to move to a certain position along the Y axis

        ObjectAnimator animator2 = ObjectAnimator.ofFloat(mImageview,

                "translationY"200);

        //Set the translationX parameter to move to a certain position along the X axis

        ObjectAnimator animator3 = ObjectAnimator.ofFloat(mImageview,

                "translationX"200);

        //Set the rotation parameter to rotate a certain angle

        ObjectAnimator animator4 = ObjectAnimator.ofFloat(mImageview,

                "rotation"180);

        //Magnify 0.5 times

        ObjectAnimator animator5 = ObjectAnimator.ofFloat(mImageview,

                "scaleX"0.5f);

        //The animation lasts 2 seconds.

        animatorSet.setDuration(2000);

        //set the combined animation

        animatorSet.playTogether(

                animator1,

                animator2,

                animator3,

                animator4,

                animator5

        );

        //start the animation

        animatorSet.start();

    }

}

效果同上

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/540209.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

WPS 相较于其他办公软件有哪些优势?

WPS Office 是一款流行的办公软件套件&#xff0c;与其他办公软件相比&#xff0c;它具有以下优势&#xff1a; 1. **兼容性强**&#xff1a;WPS Office 可以很好地与 Microsoft Office 兼容&#xff0c;能够打开、编辑和保存 Microsoft Office 格式的文档&#xff0c;如 Word…

生成器模式(软考uml C++版)

按照软考中级软件设计师中指定的生成器模式uml图&#xff0c;可编写对应的C&#xff0b;&#xff0b;代码&#xff1a; #include<iostream> #include<vector> #include<string> using namespace std;/*创建者模式&#xff0c;又名生成器模式意图&#xff1a…

中宣部防沉迷系统PHP版本(管局防沉迷验证-PHP-全版本-接口测试样例)

现在对接游戏&#xff0c;无论是登录还是支付都是要去对接防沉迷实名认证接口&#xff0c;但前期的话你要登录网络游戏防沉迷实名认证系统进行接口测试&#xff0c;$appid &#xff0c;$bizId&#xff0c;$key去接口测试页面找&#xff08;正式上线在密钥管理&#xff09;&…

mac输入su命令报错如何重置密码

diannao1xiejiandeMacBook-Air ~ % su Password: su: Sorry输入 sudo passwd 命令重置密码即可。

【数据分析】数据分析介绍

专栏文章索引&#xff1a;【数据分析】专栏文章索引 目录 一、介绍 二、生活中的数据分析 1.无处不在的数据 2.为什么要进行数据分析&#xff1f; 三、数据挖掘案例 1.案例分析 一、介绍 数据采集&#xff1a;数据采集是指从不同来源收集原始数据的过程&#xff0c;包括…

Qt+FFmpeg+opengl从零制作视频播放器-3.解封装

解封装&#xff1a;如下图所示&#xff0c;就是将FLV、MKV、MP4等文件解封装为视频H.264或H.265压缩数据&#xff0c;音频MP3或AAC的压缩数据&#xff0c;下图为常用的基本操作。 ffmpeg使用解封装的基本流程如下&#xff1a; 在使用FFmpeg API之前&#xff0c;需要先注册API&a…

记某次HVV:文件上传打入内网

免责声明 本文仅用于参考和学习交流&#xff0c;对于使用本文所提供的信息所造成的任何直接或间接的后果和损失&#xff0c;使用者需自行承担责任。本文的作者对此不承担任何责任。请在使用本文内容时谨慎评估风险并做出独立判断。谢谢&#xff01; 前言 某次地市hvv发现一个…

2024三轮车行业发展现状

环洋市场咨询&#xff08;Global Info Research&#xff09;的三轮车市场调研报告提供三轮车市场的基本概况&#xff0c;包括定义&#xff0c;分类&#xff0c;应用和产业链结构&#xff0c;同时还讨论发展政策和计划以及制造流程和成本结构&#xff0c;分析三轮车市场的发展现…

Ubuntu系统的安装及基础操作

目录 一、VMware虚拟机安装Ubuntu20.04过程 1、安装前的准备工作 2、VMware虚拟机创建Ubuntu操作系统 步骤一&#xff1a;以管理员的身份运行VMware虚拟机 步骤二&#xff1a;新建虚拟机 步骤三&#xff1a;选择类型配置 步骤四&#xff1a;选择安装客户机操作系统 步骤…

PlayBook 详解

4&#xff09;Playbook 4.1&#xff09;Playbook 介绍 PlayBook 与 ad-hoc 相比&#xff0c;是一种完全不同的运用 Ansible 的方式&#xff0c;类似与 Saltstack 的 state 状态文件。ad-hoc 无法持久使用&#xff0c;PlayBook 可以持久使用。 PlayBook 剧本是 由一个或多个 “…

Jmeter入参问题小记

表单入参的时候&#xff0c;这个地方需要勾选&#xff0c;如果不☑️选的话&#xff0c;会提示errorMsg":"Required String parameter code is not present",

【学习笔记】红队视角下的windows应急响应

1. 上线的方法 exe上线→开360晶核的情况比较困难 2&#xff09;白加黑 接下来的讲解就是基于白加黑上线&#xff0c;看如何应对应急 2. 演示 360环境启动 shell whoami →死 -beacon 如何去查杀 看外联&#xff1a; netstat -ano 提取IP 威胁情报api调用→查是否是恶意…