今天接到了一个任务就是实现点击中间按钮,两个图片分别向按钮的两边平移并且沿着自身移动。实现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示例。
<!--?xml version="1.0"
encoding="utf-8"?-->
<set xmlns:android="
http://schemas.android.com/apk/res/android">
<translate
android:fromxdelta="30"
android:toxdelta="-80"
android:fromydelta="30"
android:toydelta="300"
android:duration="2000">
<!-- translate
位置转移动画效果
整型值
:
fromXDelta
属性为动画起始时
X
坐标上的位置
toXDelta
属性为动画结束时
X
坐标上的位置
fromYDelta
属性为动画起始时
Y
坐标上的位置
toYDelta
属性为动画结束时
Y
坐标上的位置
注意
:
没有指定
fromXType toXType fromYType toYType
时候,
默认是以自己为相对参照物
长整型值:
duration
属性为动画持续时间
以毫秒为单位
-->
</translate></set>
下面是旋转动画的XML示例。
<!--?xml version="1.0" encoding="utf-8"?-->
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate android:interpolator="@android :anim/accelerate_decelerate_interpolator" android:fromdegrees="0" android:todegrees="+350" android:pivotx="50%" android:pivoty="50%" android:duration="3000">
<!-- rotate 旋转动画效果
属性:interpolator 指定一个动画的插入器
在试验过程中,使用android.res.anim中的资源时候发现
有三种动画插入器:
accelerate_decelerate_interpolator 加速-减速 动画插入器
accelerate_interpolator 加速-动画插入器
decelerate_interpolator 减速- 动画插入器
其他的属于特定的动画效果
浮点数型值:
fromDegrees 属性为动画起始时物件的角度
toDegrees 属性为动画结束时物件旋转的角度 可以大于360度
说明: 当角度为负数——表示逆时针旋转
当角度为正数——表示顺时针旋转
(负数from——to正数:顺时针旋转)
(负数from——to负数:逆时针旋转)
(正数from——to正数:顺时针旋转)
(正数from——to负数:逆时针旋转)
pivotX 属性为动画相对于物件的X坐标的开始位置
pivotY 属性为动画相对于物件的Y坐标的开始位置
说明: 以上两个属性值 从0%-100%中取值
50%为物件的X或Y方向坐标上的中点位置
长整型值:
duration 属性为动画持续时间 以毫秒为单位-->
</rotate></set>
但是在使用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;
}
}
}
}
以上就是主要代码,