实现Android简单动画旋转案例

利用android实现了简单旋转动画,效果如下,从左到右,3个状态,最终图片旋转180度:

旋转动画利用的是RotateAnimation实现的。布局文件main.xml代码:

复制代码
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical" android:id="@+id/mContener"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
  <imageview android:id="@+id/picture_tiankong"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:src="@drawable/tiankong"
  android:onClick="showAnimation"
  />
   android:onClick="showAnimation" 代码作用是在点击ImageView的时候,调用MainActivity中的showAnimation方法,showAnimation方法如下:
   public void showAnimation(View view) {
  Log.v(TAG, "showContent>>>");
  final float centerX = mView.getWidth() / 2.0f;
  final float centerY = mView.getHeight() / 2.0f;
  RotateAnimation rotateAnimation = new RotateAnimation(0, 180, centerX,
  centerY);
  rotateAnimation.setDuration(1000 * 20);
  rotateAnimation.setFillAfter(true);
  mView.startAnimation(rotateAnimation);
  }
复制代码

解释一下:

new RotateAnimation(0, 180, centerX,centerY);

第一个参数表示动画的起始角度,第二个参数表示动画的结束角度,第三个表示动画的旋转中心x轴,第四个表示动画旋转中心y轴。

rotateAnimation.setDuration(1000 * 20);

表动画持续20s。

rotateAnimation.setFillAfter(true);

ture表示动画结束后停留在动画的最后位置,false表示动画结束后回到初始位置,默认为false。

mView.startAnimation(rotateAnimation);


源码下载

 

posted on   vus520  阅读(1335)  评论(0编辑  收藏  举报

编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架

导航

< 2012年2月 >
29 30 31 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 1 2 3
4 5 6 7 8 9 10
点击右上角即可分享
微信分享提示