Android补间动画基本使用(位移、缩放、旋转、透明)

所属分类: 软件编程 / Android 阅读数: 61
收藏 0 赞 0 分享

本文讲述了Android补间动画基本使用(位移、缩放、旋转、透明)。分享给大家供大家参考,具体如下:

补间动画

原形态变成新形态时为了过渡变形过程,生成的动画就叫补间动画
位移、旋转、缩放、透明

位移:

参数10指的是X的起点坐标,但不是指屏幕x坐标为10的位置,而是imageview的 真实X + 10
参数150指的是X的终点坐标,它的值是imageview的 真实X + 150

//创建为位移动画对象,设置动画的初始位置和结束位置

TranslateAnimation ta = new TranslateAnimation(10, 150, 20, 140);

1.  x坐标的起点位置,如果相对于自己,传0.5f,那么起点坐标就是 真实X + 0.5 * iv宽度;

2.  x坐标的终点位置,如果传入2,那么终点坐标就是 真实X + 2 * iv的宽度;

3.  y坐标的起点位置,如果传入0.5f,那么起点坐标就是 真实Y + 0.5 * iv高度;

4.  y坐标的终点位置,如果传入2,那么终点坐标就是 真实Y + 2 * iv高度。

 TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2);

动画播放相关的设置

 //设置动画持续时间
ta.setDuration(2000);
 //动画重复播放的次数
ta.setRepeatCount(1);
 //动画重复播放的模式
ta.setRepeatMode(Animation.REVERSE);
 //动画播放完毕后,组件停留在动画结束的位置上
ta.setFillAfter(true);
 //播放动画
iv.startAnimation(ta);

缩放:

1.参数0.1f表示动画的起始宽度是真实宽度的0.1倍
2.参数4表示动画的结束宽度是真实宽度的4倍
3.缩放的中心点在iv左上角

ScaleAnimation sa = new ScaleAnimation(0.1f, 4, 0.1f, 4);

1.  参数0.1f和4意义与上面相同

2.  改变缩放的中心点:传入的两个0.5f,类型都是相对于自己,这两个参数改变了缩放的中心点

3.  中心点x坐标 = 真实X + 0.5 * iv宽度

4.  中心点Y坐标 = 真实Y + 0.5 * iv高度

 ScaleAnimation sa = new ScaleAnimation(0.1f, 4, 0.1f, 4, Animation.RELATIVETOSELF, 0.5f, Animation.RELATIVETOSELF, 0.5f);

透明:

0为完全透明,1为完全不透明

AlphaAnimation aa = new AlphaAnimation(0, 0.5f);

旋转:

1.  20表示动画开始时的iv的角度

2.  360表示动画结束时iv的角度

3.  默认旋转的圆心在iv左上角

RotateAnimation ra = new RotateAnimation(20, 360);

1.  20,360的意义和上面一样

2.  指定圆心坐标,相对于自己,值传入0.5,那么圆心的x坐标:真实X + iv宽度 * 0.5

3.  圆心的Y坐标:真实Y + iv高度 * 0.5

RotateAnimation ra = new RotateAnimation(20, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

所有动画一起飞

 //创建动画集合
 AnimationSet set = new AnimationSet(false);
 //往集合中添加动画
 set.addAnimation(aa);
 set.addAnimation(sa);
 set.addAnimation(ra);
 iv.startAnimation(set);

效果如图:

布局代码:

 xmlns:tools="http://schemas.android.com/tools" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 tools:context=".MainActivity"> 
 <ImageView 
 android:id="@+id/iv" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:src="@drawable/ic_launcher" 
 android:layout_centerVertical="true" 
 android:layout_centerHorizontal="true" /> 
 <LinearLayout 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:gravity="center_horizontal" 
 android:layout_alignParentBottom="true" 
 android:orientation="horizontal"> 
 <Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:onClick="translate" 
 android:text="平移" /> 
 <Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:onClick="scale" 
 android:text="缩放" /> 
 <Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:onClick="alpha" 
 android:text="透明" /> 
 <Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:onClick="rotate" 
 android:text="旋转" /> 
 <Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:onClick="fly" 
 android:text="一起飞" /> 
 </LinearLayout> 
</RelativeLayout> 

MainActivity.java

import android.app.Activity; 
import android.os.Bundle; 
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.ImageView; 
 public class MainActivity extends Activity { 
 private ImageView iv; 
 private TranslateAnimation ta; 
 private ScaleAnimation sa; 
 private AlphaAnimation aa; 
 private RotateAnimation ra; 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
 super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main); 
 iv = (ImageView) findViewById(R.id.iv); 
 } 
 //平移 
 public void translate(View view) { 
// ta = new TranslateAnimation(10, 100, 20, 200); 
 ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1, Animation.RELATIVE_TO_SELF, 2, 
 Animation.RELATIVE_TO_SELF, -0.5f, Animation.RELATIVE_TO_SELF, 1.5f); 
 //设置播放时间 
 ta.setDuration(2000); 
 //设置重复次数 
 ta.setRepeatCount(1); 
 //动画重复播放的模式 
 ta.setRepeatMode(Animation.REVERSE); 
 iv.startAnimation(ta); 
 } 
 //缩放 
 public void scale(View view) { 
// sa = new ScaleAnimation(2, 4, 2, 4, iv.getWidth() / 2, iv.getHeight() / 2); 
 sa = new ScaleAnimation(0.5f, 2, 0.1f, 3, Animation.RELATIVE_TO_SELF, 0.5f, 
 Animation.RELATIVE_TO_SELF, 0.5f); 
 //设置播放时间 
 sa.setDuration(2000); 
 //设置重复次数 
 sa.setRepeatCount(1); 
 //动画重复播放的模式 
 sa.setRepeatMode(Animation.ABSOLUTE); 
 //动画播放完毕后,组件停留在动画结束的位置上 
 sa.setFillAfter(true); 
 iv.startAnimation(sa); 
 } 
 //透明 
 public void alpha(View view) { 
 aa = new AlphaAnimation(0, 1); 
 //设置播放时间 
 aa.setDuration(2000); 
 //设置重复次数 
 aa.setRepeatCount(1); 
 //动画重复播放的模式 
 aa.setRepeatMode(Animation.REVERSE); 
 //动画播放完毕后,组件停留在动画结束的位置上 
// aa.setFillAfter(true); 
 iv.startAnimation(aa); 
 } 
 //旋转 
 public void rotate(View view) { 
 ra = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, 
 Animation.RELATIVE_TO_SELF, 0.5f); 
 //设置播放时间 
 ra.setDuration(2000); 
 //设置重复次数 
 ra.setRepeatCount(1); 
 //动画重复播放的模式 
 ra.setRepeatMode(Animation.REVERSE); 
 //动画播放完毕后,组件停留在动画结束的位置上 
 ra.setFillAfter(true); 
 iv.startAnimation(ra); 
 } 
 //位移、缩放、透明、旋转同时进行 
 public void fly(View view) { 
 AnimationSet set = new AnimationSet(false); 
 set.addAnimation(ta); 
 set.addAnimation(sa); 
 set.addAnimation(aa); 
 set.addAnimation(ra); 
 iv.startAnimation(set); 
 } 
} 

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多