iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android动画之补间动画(Tween Animation)基础学习
  • 349
分享到

Android动画之补间动画(Tween Animation)基础学习

学习补间动画animationAndroid 2022-06-06 07:06:06 349人浏览 独家记忆
摘要

前言 之前说过了在Android中,动画Animation的实现有两种方式:Tween Animation(渐变动画)和Frame Animation(帧动画)。渐变动画是通过

前言

之前说过了在Android中,动画Animation的实现有两种方式:Tween Animation(渐变动画)和Frame Animation(帧动画)。渐变动画是通过对场景里的对象不断做图像变换(平移、缩放、旋转等)产生动画效果。帧动画则是通过顺序播放事先准备好的图像来产生动画效果,和电影类似。

小编也和大家分享了逐帧动画的基础知识,下面我们就来学习下Android中逐帧动画的基础知识。

原理 : 给出开始和结束两个关键帧,两个关键帧之间的插补帧是由计算机自动运算而得到的。

分类 :

AlphaAnimation
(透明度)
ScaleAnimation
(缩放)
TranslateAnimation
(位移)
RotateAnimation 
(旋转)
AnimationSet
(组合)

方式 :

1.在代码中

new

2.在anim文件夹下定义动画xml资源

效果

代码

第一步 :准备动画资源

目录


<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="Http://schemas.android.com/apk/res/android"
 android:duration="2000"
 android:fromAlpha="1.0"
 android:interpolator="@android:anim/linear_interpolator"
 android:toAlpha="0.3">
</alpha>

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:interpolator/linear"
 android:repeatCount="infinite"
 android:repeatMode="reverse"
 android:duration="2000"
 android:fromDegrees="0"
 android:toDegrees="1080">
 android:pivotX="50%"
 android:pivotY="50%"
</rotate>

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:anim/linear_interpolator"
 android:duration="2000"
 android:fillAfter="true"
 android:fromXScale="1.0"
 android:fromYScale="1.0"
 android:pivotX="50%"
 android:pivotY="50%"
 android:toXScale="0.3"
 android:toYScale="0.3">
</scale>

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
  android:interpolator="@android:anim/linear_interpolator"
  android:duration="2000"
  android:fromXDelta="10"
  android:fromYDelta="10"
  android:toXDelta="300"
  android:toYDelta="300">
</translate>

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
 android:interpolator="@android:anim/linear_interpolator"
 android:duration="2000">
 <alpha android:fromAlpha="0.3"
  android:toAlpha="1.0"/>
 <rotate android:fromDegrees="0"
  android:toDegrees="360"
  android:pivotX="0"
  android:pivotY="0"
  android:repeatMode="restart"
  android:repeatCount="infinite"/>
</set>

第二步 :activity_main.xml ( 略 )

第三步 :MainActivity.java


package com.lyp.anim;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
 private Button btnScale;
 private Button btnRotate;
 private Button btnTranslate;
 private Button btnAlpha;
 private Button btnAll;
 private ImageView mImage;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 initView();
 }
 private void initView() {
 btnScale= (Button) findViewById(R.id.btn_scale);
 btnRotate= (Button) findViewById(R.id.btn_rotate);
 btnTranslate= (Button) findViewById(R.id.btn_translate);
 btnAlpha= (Button) findViewById(R.id.btn_alpha);
 btnAll= (Button) findViewById(R.id.btn_all);
 mImage= (ImageView) findViewById(R.id.image);
 btnScale.setOnClickListener(this);
 btnRotate.setOnClickListener(this);
 btnTranslate.setOnClickListener(this);
 btnAlpha.setOnClickListener(this);
 btnAll.setOnClickListener(this);
 }
 @Override
 public void onClick(View v) {
 switch (v.getId()){
  case R.id.btn_scale:
  //加载缩放动画
  Animation scale = AnimationUtils.loadAnimation(this, R.anim.scale);
  scale.setFillAfter(true); //保留动画结束状态,在xml文件中设置无效!!
  mImage.startAnimation(scale);
  break;
  case R.id.btn_rotate:
  //加载旋转动画
  Animation rotate = AnimationUtils.loadAnimation(this, R.anim.rotate);
  mImage.startAnimation(rotate);
  break;
  case R.id.btn_translate:
  //加载位移动画
  Animation translate = AnimationUtils.loadAnimation(this, R.anim.translate);
  mImage.startAnimation(translate);
  break;
  case R.id.btn_alpha:
  //加载透明度渐变动画
  Animation alpha = AnimationUtils.loadAnimation(this, R.anim.alpha);
  mImage.startAnimation(alpha);
  break;
  case R.id.btn_all:
  //加载组合动画
  Animation all = AnimationUtils.loadAnimation(this, R.anim.all);
  mImage.startAnimation(all);
  break;
 }
 }
}

总结

以上Android中补间动画(Tween Animation)基础的全部内容了,动画Animation实现的两种方式小编现在已经都给大家分享了,希望能对各位Android开发者们有所帮助,如果有疑问大家可以留言交流。

您可能感兴趣的文章:Android补间动画基本使用(位移、缩放、旋转、透明)Android旋转、平移、缩放和透明度渐变的补间动画Android控件Tween动画(补间动画)实现方法示例android 帧动画,补间动画,属性动画的简单总结Android帧动画、补间动画、属性动画用法详解Android动画之补间动画(Tween Animation)实例详解Android动画学习笔记之补间动画


--结束END--

本文标题: Android动画之补间动画(Tween Animation)基础学习

本文链接: https://www.lsjlt.com/news/23832.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作