源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

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

  • 时间:2022-12-11 21:44 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:Android动画之补间动画(Tween Animation)基础学习
[b]前言[/b] 之前说过了在Android中,动画Animation的实现有两种方式:Tween Animation(渐变动画)和Frame Animation(帧动画)。渐变动画是通过对场景里的对象不断做图像变换(平移、缩放、旋转等)产生动画效果。帧动画则是通过顺序播放事先准备好的图像来产生动画效果,和电影类似。 小编也和大家分享了[url=http://www.1sucai.cn/article/93580.htm]逐帧动画[/url]的基础知识,下面我们就来学习下Android中逐帧动画的基础知识。 [b]原理 : [/b]给出开始和结束两个关键帧,两个关键帧之间的插补帧是由计算机自动运算而得到的。 [b]分类 : [/b][code]AlphaAnimation[/code](透明度) [code]ScaleAnimation[/code](缩放) [code]TranslateAnimation[/code](位移) [code]RotateAnimation [/code](旋转) [code]AnimationSet[/code](组合) [b]方式 : [/b] 1.在代码中[code]new[/code] 2.在anim文件夹下定义动画xml资源 [b]效果[/b] [img]http://files.jb51.net/file_images/article/201609/2016927100116854.gif?201682710124[/img] [b]代码[/b] [b]第一步 :准备动画资源[/b] 目录 [img]http://files.jb51.net/file_images/article/201609/2016927100152172.png?20168271023[/img]
<?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>
[b]第二步 :activity_main.xml ( 略 )[/b] [b]第三步 :MainActivity.java[/b]
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开发者们有所帮助,如果有疑问大家可以留言交流。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部