Animation动画平移和旋转的结合使用
今天接到了一个任务就是实现点击中间按钮,两个图片分别向按钮的两边平移并且沿着自身移动。实现Android中的动画效果主要有两个途径一个是代码实现,一个是在XML中定义好,然后取Activity中去加载便可,
首先先介绍一下重要的XML动画属性:
android:duration 动画持续时间,时间以毫秒为单位。
android:startOffset 动画之间的时间间隔,从上次动画停多少时间开始执行下个动画。
android:interpolator 指定一个动画的插入器。
android:fillAfter 当设置为true ,该动画转化在动画结束后被应用。
android:repeatMode 定义重复的行为。
android:repeatCount 动画的重复次数。
移动动画的使用,首先在res目录中新建anim的文件夹,在anim中新建需要的动画xml资源文件。xml资源文件创建完成之后,接下来就是调用这些资源文件。你可以使用AnimationUtils类的静态方法loadAnimation()来加载XML中的动画XML文件:
Animation animation=AnimationUtils.loadAnimation(this, R.anim.alpha);
view.startAnimation(animation);
下面是移动动画的XML示例。
">
android:fromxdelta="30"
android:toxdelta="-80"
android:fromydelta="30"
android:toydelta="300"
android:duration="2000">
下面是旋转动画的XML示例。
但是在使用XML中我只实现了平移效果,当加入旋转的效果时,图片并不是围绕自身旋转。所以我用代码实现了文中所说的效果。(如果有人用XML实现了的话请指教,多谢。)
package com.cxbingo.lenovo.myanimationdemo;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.view.animation.AnimationSet;import android.view.animation.RotateAnimation;import android.view.animation.TranslateAnimation;import android.widget.ImageView;public class MainActivity extends AppCompatActivity { private ImageView imageView; private ImageView imageViewtwo; private ImageView buttonstart; private boolean flag = true; public void init(){ imageView = (ImageView) findViewById(R.id.imageviewdemo); imageViewtwo = (ImageView) findViewById(R.id.imageviewdemotwo); buttonstart = (ImageView) findViewById(R.id.ButtonofStart); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); } public void onClick(View view){ switch (view.getId()){ case R.id.ButtonofStart: if (flag==true){ imageView.setVisibility(View.VISIBLE); imageViewtwo.setVisibility(View.VISIBLE); AnimationSet animationSet = new AnimationSet(false); AnimationSet animationSetleft = new AnimationSet(false); // 把旋转动画加入到集合中 RotateAnimation rotateAnimation = new RotateAnimation(0, 360, imageView.getWidth() / 2, imageView.getHeight() / 2); RotateAnimation rotateAnimationleft = new RotateAnimation(0, -360, imageViewtwo.getWidth() / 2, imageViewtwo.getHeight() / 2); rotateAnimation.setDuration(2000); rotateAnimationleft.setDuration(2000); // rotateAnimation.setRepeatCount(10); animationSet.addAnimation(rotateAnimation); animationSetleft.addAnimation(rotateAnimationleft); int w = getResources().getDisplayMetrics().widthPixels; int h = getResources().getDisplayMetrics().heightPixels; TranslateAnimation translateAnimation = new TranslateAnimation(0, w - 4.5f*imageView.getWidth(), 0, 0); TranslateAnimation translateAnimationleft = new TranslateAnimation(0, - 3f*imageViewtwo.getWidth(), 0, 0); translateAnimation.setDuration(1000); translateAnimationleft.setDuration(1000); animationSet.addAnimation(translateAnimation); animationSetleft.addAnimation(translateAnimationleft); animationSet.setFillAfter(true); animationSetleft.setFillAfter(true); imageView.startAnimation(animationSet); imageViewtwo.startAnimation(animationSetleft); flag=false; break; }else if (flag==false){ AnimationSet animationSet = new AnimationSet(false); AnimationSet animationSetleft = new AnimationSet(false); // 把旋转动画加入到集合中 RotateAnimation rotateAnimation = new RotateAnimation(0, 360, imageView.getWidth() / 2, imageView.getHeight() / 2); RotateAnimation rotateAnimationleft = new RotateAnimation(0, -360, imageViewtwo.getWidth() / 2, imageViewtwo.getHeight() / 2); rotateAnimation.setDuration(2000); rotateAnimationleft.setDuration(2000); // rotateAnimation.setRepeatCount(10); animationSet.addAnimation(rotateAnimation); animationSetleft.addAnimation(rotateAnimationleft); int w = getResources().getDisplayMetrics().widthPixels; int h = getResources().getDisplayMetrics().heightPixels; TranslateAnimation translateAnimation = new TranslateAnimation(w - 4.5f*imageView.getWidth(), 0, 0, 0); TranslateAnimation translateAnimationleft = new TranslateAnimation(- 3f*imageViewtwo.getWidth(), 0, 0, 0); translateAnimation.setDuration(1000); translateAnimationleft.setDuration(1000); animationSet.addAnimation(translateAnimation); animationSetleft.addAnimation(translateAnimationleft); animationSet.setFillAfter(true); animationSetleft.setFillAfter(true); imageView.startAnimation(animationSet); imageViewtwo.startAnimation(animationSetleft); imageView.setVisibility(View.INVISIBLE); imageViewtwo.setVisibility(View.INVISIBLE); flag=true; break; } } }}
以上就是主要代码, 转载于:https://my.oschina.net/FrancisBingo/blog/654648
更多相关文章
- Android(安卓)API 实验记录(一)
- Bitmaps加载之内存管理
- 四、Android中控件的继承 通用行为和属性
- PreferenceActivity详解
- Activity从屏幕底部滑出、滑入、处理黑色背景和状态栏
- Android(安卓)自定义 View 之使用 SurfaceView 实现动画
- android 布局长度单位深入研究
- android中xml tools属性详解
- [有梦想的IT人] Android优秀的动画库