Android 平台提供了两类动画。 一类是Tween动画,就是对场景里的对象不断的进行图像变化来产生动画效果(旋转、平移、放缩和渐变)。

下面就讲一下Tweene Animations。

主要类:

Animation   动画

AlphaAnimation 渐变透明度

RotateAnimation 画面旋转

ScaleAnimation 渐变尺寸缩放

TranslateAnimation 位置移动

AnimationSet  动画集

一.AlphaAnimation

其中AlphaAnimation类第一个参数fromAlpha表示动画起始时的透明度, 第二个参数toAlpha表示动画结束时的透明度。

setDuration用来设置动画持续时间。

二.RotateAnimation

其中RotateAnimation类第一个参数fromDegrees表示动画起始时的角度, 第二个参数toDegrees表示动画结束时的角度。

另外还可以设置伸缩模式pivotXType、pivotYType, 伸缩动画相对于x,y 坐标的开始位置pivotXValue、pivotYValue等。

三.ScaleAnimation

ScaleAnimation类中

第一个参数fromX ,第二个参数toX:分别是动画起始、结束时X坐标上的伸缩尺寸。

第三个参数fromY ,第四个参数toY:分别是动画起始、结束时Y坐标上的伸缩尺寸。

另外还可以设置伸缩模式pivotXType、pivotYType, 伸缩动画相对于x,y 坐标的开始位置pivotXValue、pivotYValue等。

四.TranslateAnimation

第一个参数fromXDelta ,第二个参数toXDelta:分别是动画起始、结束时X坐标。

第三个参数fromYDelta ,第四个参数toYDelta:分别是动画起始、结束时Y坐标。

下面我实现的这个例子是使得图片有上述四个动画效果,且其中第五实现的是两个效果的重叠,具体的实现截图如下:

点击各个按钮会做出相应的反应。

本实例用到的布局文件如下:

<?xml version="1.0" encoding="utf-8"?><AbsoluteLayout android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"><Button android:id="@+id/button_scale" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="scale" android:layout_x="5dp" android:layout_y="383dp" /><Button android:id="@+id/button_rotate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="rotate" android:layout_x="158dp" android:layout_y="383dp" /><Button android:id="@+id/button_alpha" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="alpha" android:layout_x="5dp" android:layout_y="331dp" /><Button android:id="@+id/button_translate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="translate" android:layout_x="160dp" android:layout_y="329dp" /><Button android:id="@+id/button_alpha_translate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="alpha_translate" android:layout_x="84dp" android:layout_y="265dp" /><ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="105dp" android:layout_y="133dp"  android:src="@drawable/ic_launcher" /></AbsoluteLayout>

实现本实例的源代码如下:

public class Animations_Activity extends Activity { private Button button1; private Button button2; private Button button3; private Button button4; private Button button5; private ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_animations_);  button1=(Button)findViewById(R.id.button_alpha);  button2=(Button)findViewById(R.id.button_rotate);  button3=(Button)findViewById(R.id.button_scale);  button4=(Button)findViewById(R.id.button_translate);  button5=(Button)findViewById(R.id.button_alpha_translate);  imageView=(ImageView)findViewById(R.id.imageview);  button1.setOnClickListener(new MyButton());  button2.setOnClickListener(new MyButton());  button3.setOnClickListener(new MyButton());  button4.setOnClickListener(new MyButton());  button5.setOnClickListener(new MyButton()); } class MyButton implements OnClickListener{  @Override  public void onClick(View arg0) {   // TODO Auto-generated method stub   switch (arg0.getId()) {   case R.id.button_alpha:    Alpha();    break;   case R.id.button_rotate:    Rotata();    break;   case R.id.button_scale:    Scale();    break;   case R.id.button_translate:    Translate();    break;   case R.id.button_alpha_translate:    Alpha_Translate();    break;   default:    break;   }  }   }  /*  * 1.创建一个AnimationSet对象,该对象存储的是动画的集合  * 2.根据需要创建相应的Animation对象  * 3.根据动画的需求,为Animation对象设置相应的数据(即执行效果)  * 4.奖Animation对象添加到AnimationSet对象当中  * 5.使用控件对象开始执行AnimationSet  */ public void Alpha() {  AnimationSet animationSet=new AnimationSet(true);  AlphaAnimation alphaAnimation=new AlphaAnimation(1, 0);  alphaAnimation.setDuration(2000);  animationSet.addAnimation(alphaAnimation);  imageView.startAnimation(animationSet); } public void Rotata(){  AnimationSet animationSet=new AnimationSet(true);  //后面的四个参数定义的是旋转的圆心位置  RotateAnimation rotateAnimation=new RotateAnimation(    0, 360,     Animation.RELATIVE_TO_PARENT, 1f,    Animation.RELATIVE_TO_PARENT, 0f);  rotateAnimation.setDuration(2000);  animationSet.addAnimation(rotateAnimation);  imageView.startAnimation(animationSet); } public void Scale() {  AnimationSet animationSet=new AnimationSet(true);  ScaleAnimation scaleAnimation=new ScaleAnimation(    1, 0.1f, 1, 0.1f,     Animation.RELATIVE_TO_SELF, 0.5f,     Animation.RELATIVE_TO_SELF, 0.5f);  scaleAnimation.setDuration(2000);  animationSet.addAnimation(scaleAnimation);  imageView.startAnimation(scaleAnimation); } public void Translate() {  AnimationSet animationSet=new AnimationSet(true);  TranslateAnimation translateAnimation=new TranslateAnimation(    Animation.RELATIVE_TO_SELF, 0f,  //X轴的开始位置    Animation.RELATIVE_TO_SELF, 0.5f,  //X轴的结束位置    Animation.RELATIVE_TO_SELF, 0f,  //Y轴的开始位置    Animation.RELATIVE_TO_SELF, 1.0f);  //Y轴的结束位置  translateAnimation.setDuration(2000);  animationSet.addAnimation(translateAnimation);    /*   * 第一行的设置如果为true,则动画执行完之后效果定格在执行完之后的状态   * 第二行的设置如果为false,则动画执行完之后效果定格在执行完之后的状态   * 第三行设置的是一个long类型的值,是指动画延迟多少毫秒之后执行   * 第四行定义的是动画重复几次执行   */  animationSet.setFillAfter(true);  animationSet.setFillBefore(false);  animationSet.setStartOffset(2000);  animationSet.setRepeatCount(3);    imageView.startAnimation(animationSet); } public void Alpha_Translate() {  AnimationSet animationSet=new AnimationSet(true);  AlphaAnimation alphaAnimation=new AlphaAnimation(1, 0);  alphaAnimation.setDuration(2000);  animationSet.addAnimation(alphaAnimation);  TranslateAnimation translateAnimation=new TranslateAnimation(    Animation.RELATIVE_TO_SELF, 0f,  //X轴的开始位置    Animation.RELATIVE_TO_SELF, 0.5f,  //X轴的结束位置    Animation.RELATIVE_TO_SELF, 0f,  //Y轴的开始位置    Animation.RELATIVE_TO_SELF, 1.0f);  //Y轴的结束位置  translateAnimation.setDuration(2000);  animationSet.addAnimation(translateAnimation);  imageView.startAnimation(animationSet); } @Override public boolean onCreateOptionsMenu(Menu menu) {  // Inflate the menu; this adds items to the action bar if it is present.  getMenuInflater().inflate(R.menu.activity_animations_, menu);  return true; }}

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

Android开发--图形图像与动画(二)--Animation实现图像的 渐变 缩放 位移 旋转相关推荐

  1. 图像算法二:【图像几何变换】平移、镜像、转置、缩放、旋转、插值

    作为一个强大的科学计算软件,MATLAB广泛运用于较多领域,以其简单的编程风格著称.这篇文章便通过matlab语言来讲述如何进行图像的各种几何变换. 图像几何变换又称为图像空间变换,它是将一幅图像中的 ...

  2. Android开发--图形图像与动画(三)--Animation效果的XML实现

    使用XML来定义Tween Animation 动画的XML文件在工程中res/anim目录,这个文件必须包含一个根元素,可以使<alpha><scale> <trans ...

  3. 动画---图形图像与动画(三)Animation效果的XML实现

    使用XML来定义Tween Animation  动画的XML文件在工程中res/anim目录,这个文件必须包含一个根元素,可以使<alpha><scale> <tran ...

  4. Android开发笔记(一百七十四)图像解码器ImageDecoder

    早期的Android只支持三种图像格式,分别是JPEG.PNG和GIF,虽然这三类图片都能在ImageView上显示,但对于GIF格式来说,图像视图仅能显示动图的初始画面,无法直接播放动画效果.此外, ...

  5. android开发笔记之属性动画

    属性动画简单介绍 作用对象:任意 Java 对象 不再局限于 视图View对象 实现的动画效果:可自定义各种动画效果 不再局限于4种基本变换:平移.旋转.缩放 & 透明度 特点 作用对象进行了 ...

  6. Android开发中遇到的问题(二)——新建android工程的时候eclipse没有生成MainActivity和layout布局...

    2019独角兽企业重金招聘Python工程师标准>>> 一.新建android工程的时候eclipse没有生成MainActivity和layout布局 最近由于工作上的原因,开始学 ...

  7. Android开发笔记(一百零二)统计图表

    AChartEngine AChartEngine是Android平台上的图表绘制引擎,提供了包括折线图.柱状图.饼状图等图表显示.它的官网地址是http://achartengine.org/,源码 ...

  8. Android开发笔记(八十二)SDK版本兼容

    统一主题与风格 Android控件很多属性都有默认值,比如文字默认黑色.编辑框默认透明背景等等,但因为android是开源的,各厂商都会自行修修补补,所以很多时候默认值并不靠谱.举例如下: 1.在某些 ...

  9. Android开发笔记(四十二)Broadcast的生命周期

    Broadcast是什么 广播的特性 广播(Broadcast)用于Android组件之间的灵活通信,它与Activity和Service的区别在于: 1.Activity和Service都只能一对一 ...

最新文章

  1. [实现] 利用 Seq2Seq 预测句子后续字词 (Pytorch)2
  2. 负数分解质因数java_Java经典案例之-“分解质因数”
  3. C#编写最小化时隐藏为任务栏图标的Window appllication
  4. jQuery Mobile 笔记(1)- jQuery Mobile页面结构
  5. vue_组件_非prop特性
  6. linux如何做bond4,linux系统做bond
  7. HarmonyOS 正式登陆华为手机,4月陆续升级!
  8. swagger 基础入门
  9. apache-commons之BeanUtils、ConvertUtils、PropertyUtils、CollectionUtils的基本使用
  10. VOIP电话中的SIP信令返回码的详细解释
  11. 全国教师计算机职称考试题库,全国信息技术教师考试题库及答案(2016最新版)
  12. 百度翻译API的调用
  13. 演唱会舞台怎么设计?不妨看看这位建筑大师怎么玩!
  14. C语言简易程序设计————11、打印楼梯与笑脸
  15. 职场生涯规划中必须学会的十种能力
  16. mysql 查询最多的_MySQL中用通用查询日志找出查询次数最多的语句的教程
  17. 原癌基因和抑癌基因总结
  18. 有没有什么惊艳的微信头像?Python一键下载1000张!
  19. 群辉+picgo+typora搭建自有图床
  20. 用 JavaScript 实现寻路算法 —— 编程训练

热门文章

  1. 方法重载-Overload
  2. 【2781】二分练习 sdutOJ
  3. robot framework环境搭建
  4. 咖啡,一种心情的境界
  5. 如何周密规划决策制定过程
  6. 使用Axis开发Web Service程序
  7. SQL Server 将指定的数据库中的所有表都列出来
  8. 11月1日数据结构讨论班 【杂】
  9. 模拟实现智能指针auto_ptr,scoped_ptr,shared_ptr
  10. QT QSqlTabModel 学习,用于从数据库中存取修改等操作。