android中利用animation实现Image的循环移动
来源:互联网 发布:使命召唤高级战争优化 编辑:程序博客网 时间:2024/06/03 12:42
本文利用TranslateAnimation实现图片的移动动画效果,利用setAnimationListener实现图片的循环移动!
思想很简单,就是在第一个动画结束的时候开始下一个动画。做这个的过程让我感觉很像是在做ppt。
现在感觉很多东西原理真的都是相通的。
首先是设置四个动画效果,利用TranlateAnimation,然后在每个animation的setAnimationListener中启动下一个动画效果。
效果图片如下:
布局文件如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <RelativeLayout android:id="@+id/newIcon" android:layout_centerInParent="true" android:layout_width="match_parent" android:layout_height="80dp"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginLeft="40dp" android:layout_marginRight="20dp" android:orientation="horizontal"> <RelativeLayout android:id="@+id/dynamicView" android:layout_marginTop="10dp" android:layout_width="60dp" android:layout_height="60dp"> <ImageView android:id="@+id/pic_0" android:src="@drawable/pic1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:alpha="1.0"/> <ImageView android:id="@+id/pic_1" android:src="@drawable/pic2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:alpha="0.0"/> <ImageView android:id="@+id/pic_2" android:src="@drawable/pic2" android:layout_toRightOf="@id/pic_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:alpha="0.0" /> <ImageView android:id="@+id/pic_3" android:src="@drawable/pic2" android:layout_below="@id/pic_2" android:layout_toRightOf="@id/pic_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:alpha="0.0"/> <ImageView android:id="@+id/pic_4" android:src="@drawable/pic2" android:layout_toLeftOf="@id/pic_3" android:layout_below="@id/pic_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:alpha="0.0"/> </RelativeLayout> <TextView android:id="@+id/shuaxin" android:paddingLeft="10dp" android:text="正在刷新数据..." android:layout_marginTop="22dp" android:textSize="27sp" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> </RelativeLayout></RelativeLayout>
Java代码如下:
public class MainActivity extends Activity { private ImageView pictureDong,pictureJing_1,pictureJing_2,pictureJing_3,pictureJing_4; private static Boolean occurFlag = true; private static float offsetValue ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); pictureDong = (ImageView) findViewById(R.id.pic_0); pictureJing_1 = (ImageView) findViewById(R.id.pic_1); pictureJing_2 = (ImageView) findViewById(R.id.pic_2); pictureJing_3 = (ImageView) findViewById(R.id.pic_3); pictureJing_4 = (ImageView) findViewById(R.id.pic_4); offsetValue = getResources().getDimension(R.dimen.offsetValue); pictureJing_1.setAlpha(1.0f); final TranslateAnimation animation0 = new TranslateAnimation(0,offsetValue,0,0); animation0.setDuration(700); animation0.setFillAfter(true); final TranslateAnimation animation1 = new TranslateAnimation(offsetValue, offsetValue, 0, offsetValue); animation1.setDuration(700); animation1.setFillAfter(true); final TranslateAnimation animation2 = new TranslateAnimation(offsetValue,0,offsetValue,offsetValue); animation2.setDuration(700); animation2.setFillAfter(true); final TranslateAnimation animation3 = new TranslateAnimation(0,0,offsetValue,0); animation3.setDuration(700); animation3.setFillAfter(true); animation0.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { if (occurFlag == true){ pictureJing_2.setAlpha(1.0f); }else { pictureJing_2.setAlpha(0.0f); } pictureDong.startAnimation(animation1); } @Override public void onAnimationRepeat(Animation animation) { } }); animation1.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { if (occurFlag == true){ pictureJing_3.setAlpha(1.0f); }else { pictureJing_3.setAlpha(0.0f); } pictureDong.startAnimation(animation2); } @Override public void onAnimationRepeat(Animation animation) { } }); animation2.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { if (occurFlag == true){ pictureJing_4.setAlpha(1.0f); }else { pictureJing_4.setAlpha(0.0f); } pictureDong.startAnimation(animation3); } @Override public void onAnimationRepeat(Animation animation) { } }); animation3.setAnimationListener(new Animation.AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { if (occurFlag == true){ occurFlag = false; }else { occurFlag = true; } if (occurFlag == false){ pictureJing_1.setAlpha(0.0f); }else { pictureJing_1.setAlpha(1.0f); } pictureDong.startAnimation(animation0); } @Override public void onAnimationRepeat(Animation animation) { } }); pictureDong.startAnimation(animation0); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); }}
这里在dimes.xml文件中还要定义一个offsetValue 为30dp !
大家可以试试最终的实现效果!有什么好的建议和改进也欢迎评论!
1 0
- android中利用animation实现Image的循环移动
- Android中利用Drawable实现图片循环
- android 中Animation translate动画移动学习
- Animation插值器:解决Android Animation 循环执行的停顿问题
- android-利用Animation制作的Menu
- Android动画的实现Animation
- Android Animation的实现原理
- javascript中利用数组实现的循环队列
- android中Animation的使用!
- android中Animation的使用!
- android实现Animation图片边变小变移动动画
- android中实现图片的上下移动
- 按钮的animation移动
- Android开发--利用animation-list实现帧动画
- 利用CSS3中animation属性实现雪花飘落功能
- 怎么实现div左右移动|animate animation hover的区别
- 利用Handler循环调用自身,实现在线程中无限循环的功能
- Android Animation无限循环动画
- spring MVC Controller 实现接口后,不能解析@Controller注解问题
- python之路-------字符串与正则表达式
- 关于SMS短信滞后阅读的实现方案
- android权限
- celery beat实战
- android中利用animation实现Image的循环移动
- iOS终止函数exit
- Python 学习之<Requests入门>
- 如何构建一个持续发展的项目
- ijkplayer阅读笔记02-创建音视频读取,解码,播放线程
- linux下将自己写的shell脚本做成系统命令
- POJ 1961 Period(KMP最小循环节)
- AsyncTask分析
- 使用移动版AWS控制台管理资源