android动画Rotate
2019独角兽企业重金招聘Python工程师标准>>>
项目有一个需求,有一个刷新按钮,上面放着一个常见的静止的刷新圆圈,如下图:
一旦用户按了刷新按钮,需要让这个刷新圆圈转动起来,让用户感觉到程序还在运行着,而不是卡死了。
有两个思路,一是将这个图按照旋转时间不同旋转成不同旋转角度的图片,就像要做一张gif图片一样,例如我要每次旋转30度,就需要360\30=12张图片,然后再anim文件夹下新建xml文件,内容如下:
- <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
- android:oneshot="true">
- <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
- <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
- <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
- </animation-list>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"android:oneshot="true"><item android:drawable="@drawable/rocket_thrust1" android:duration="200" /><item android:drawable="@drawable/rocket_thrust2" android:duration="200" /><item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>
在代码中这样写:
- AnimationDrawable rocketAnimation;
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
- rocketImage.setBackgroundResource(R.anim.rocket_thrust);
- rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
- }
- public boolean onTouchEvent(MotionEvent event) {
- if (event.getAction() == MotionEvent.ACTION_DOWN) {
- rocketAnimation.start();
- return true;
- }
- return super.onTouchEvent(event);
- }
AnimationDrawable rocketAnimation;public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);rocketImage.setBackgroundResource(R.anim.rocket_thrust);rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
}public boolean onTouchEvent(MotionEvent event) {if (event.getAction() == MotionEvent.ACTION_DOWN) {rocketAnimation.start();return true;}return super.onTouchEvent(event);
}
具体代码含义参考:http://www.cnblogs.com/feisky/archive/2010/01/11/1644482.html
这种做法其实就是将每一帧图片都显示了一次,但是由于需要更多图片,文件体积会上升。
于是想到用rotate做单帧图片旋转,查到的资料:http://rainbowsu.iteye.com/blog/766608
但是作者没能实现循环旋转,我尝试了下,修改了下anim文件的格式,成功了
- <?xml version="1.0" encoding="utf-8"?>
- <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android :anim/linear_interpolator"
- android:fromDegrees="0" android:toDegrees="+360" android:duration="1000"
- android:pivotX="50%" android:pivotY="50%" android:repeatCount="infinite" />
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator"android:fromDegrees="0" android:toDegrees="+360" android:duration="1000"android:pivotX="50%" android:pivotY="50%" android:repeatCount="infinite" />
其中android:duration="1000"表示旋转速率是1秒钟。
代码:
- package info.wegosoft;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.animation.Animation;
- import android.view.animation.AnimationUtils;
- public class LoadingAnimationTest extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Animation anim = AnimationUtils.loadAnimation(this, R.anim.round_loading);
- findViewById(R.id.loadingBtn).startAnimation(anim);
- }
- }
package info.wegosoft;import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;public class LoadingAnimationTest extends Activity {/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);Animation anim = AnimationUtils.loadAnimation(this, R.anim.round_loading); findViewById(R.id.loadingBtn).startAnimation(anim); }
}
布局文件:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <Button android:id="@+id/loadingBtn" android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:background="@drawable/refresh_normal"></Button>
- </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical" android:layout_width="fill_parent"android:layout_height="fill_parent"><Button android:id="@+id/loadingBtn" android:layout_width="wrap_content"android:layout_height="wrap_content" android:background="@drawable/refresh_normal"></Button>
</LinearLayout>
工程见附件。
最后提供官方文档相关说明的链接:http://developer.android.com/guide/topics/resources/animation-resource.html
注意其中的匀速插值器LinearInterpolator似乎不能设置速率,我在这浪费了很多时间。
转载于:https://my.oschina.net/u/586684/blog/170406
android动画Rotate相关推荐
- android 流星动画,android动画Rotate
项目有一个需求,有一个刷新按钮,上面放着一个常见的静止的刷新圆圈,如下图: 一旦用户按了刷新按钮,需要让这个刷新圆圈转动起来,让用户感觉到程序还在运行着,而不是卡死了. 有两个思路,一是将这个图按照旋 ...
- Android动画效果translate、scale、alpha、rotate详解
动画类型 Android的animation由四种类型组成 XML中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动画效果 rotate 画面 ...
- Android动画效果 translate、scale、alpha、rotate 切换Activity动画 控件位置调整
2011.10.28注:如果需要控件停在动画后的位置,需要设置android:fillAfter属性为true,在set节点中.默认在动画结束后回到动画前位置.设置android:fillAfter后 ...
- android rotate 动画,Android 动画使用 scale、alpha、translate、rotate、set
scale:尺寸伸缩动画 alpha:渐变透明动画 translate:位移动画 rotate:旋转动画 调用动画文件 给View执行动画 动画文件可以存放在res下的anim目录 Android S ...
- android 淡入位移动画,Android动画 translate(位移)、scale(缩放)、alpha(淡入淡出)、rotate(旋转)...
一.Android动画类型 Android的animation由四种类型组成 在xml文件中 alpha 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动画 ...
- Android动画之帧动画和补间动画
Android系统提供三种动画:帧动画.补间动画和属性动画.这里先分析总结帧动画和补间动画. FrameAnimation 帧动画,通俗来说就是按照图片动作顺序依次播放来形成动画,创建帧动画可以用 x ...
- android 动画引擎,一个使用openGL渲染的炫丽Android动画库
这是一个 android 动画特效库 可以实现各种炫酷动画. github地址: ht t ps:// gith u b.co m/g pl ib s/an dro id- ma gic-s ur ...
- Android 动画分析学习笔记
一:分类: Android动画分三种:view动画(对场景中的对象不断做图像变换<平移,缩放,旋转,透明度>).帧动画(顺序播放一系列图像产生动画效果).属性动画(动态改变对象属性). 二 ...
- Android动画之Tween动画实战
Android动画分为Tween动画和Frame动画,上一节通过一个实例介绍了Frame动画,本节将介绍Tween动画.Tween可以把对象进行缩小.放大.旋转和渐变等操作. Tween动画有四个主要 ...
最新文章
- c detail of macro
- cmake重新编译matlab,ubuntu系统下cmake 编译matlab中mex文件
- 简单的复数运算(类和对象)_JAVA
- 每个zone的low memory是怎么计算出来的
- Linux 命令之 unset -- 删除指定的shell变量或函数
- 一个div 上下两行_web前端工程师如何理解 CSS 布局和块级格式化上下文
- mysql+性能+计算列_MYSQL性能优化的最佳20+条经验
- 轻松逃脱某防火墙对ss的探测
- java打开的文件过多_显示打开文件数目过多的解决方法.
- scratch编程作品展示
- 广州华龙展贸考场——科目三
- linux ov7725模块驱动,stm32f4 驱动ov7725摄像头,使用dcmi一直无法产生中断
- Excel做多元线性回归
- Thinkpad8在Windows10下假关机的解决办法
- 《Real-Time Rendering 4th Edition》读书笔记--简单粗糙翻译 第六章 纹理 Texturing
- linux配置虚拟IP地址方法
- vue加载图片出现404用默认图片替换的方法
- java毕业设计中山乡村文化旅游网络平台Mybatis+系统+数据库+调试部署
- matlab—赋值具有的非单一 rhs 维度多于非单一下标数
- 特斯拉供应商同意接手一家通用汽车在韩国的电动汽车制造厂
热门文章
- printf函数输出详解
- .Net 中的序列化与反序列化 (转)
- 用PHP写出显示客户端IP与服务器IP的代码
- linux查看并发连接数
- hashmap,hashTable concurrentHashMap 是否为线程安全,区别,如何实现的
- linux下ls出现文件的后缀有@,* ,/之类的解释
- Js里面IF(var)表示什么意思?js中if的写法、含义
- 关于页面之间传参时有空格,中文及点击页面后退按钮的问题
- WEB前端学习四 js什么是原始类型
- 计算机专业科普知识图文并茂,科普下计算机专业需要什么基础知识?