Animation详解:
AnimationSet 继承自 Animation,是上面四种的组合容器管理类,没有自己特有的属性,他的属性继承自 Animation,所以特别注意, 当我们对 set 标签使用 Animation 的属性时会对该标签下的所有子控件都产生影响。
主要属性方法:
帧动画代码:
点击事件和获得布局控件用的是Xutils,所以需要导入依赖:implementation ‘org.xutils:xutils:3.5.0’
package com.example.animation_task;
import android.graphics.drawable.AnimationDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
@ContentView(R.layout.activity_fram)
public class Activity_fram extends AppCompatActivity {
@ViewInject(R.id.fram_img)
private ImageView imageView;
AnimationDrawable animationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
x.view().inject(this);//设置当前布局文件
initImage_xml();
}
private void initImage_xml() {
animationDrawable = new AnimationDrawable();
animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_01),200);
animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_02),200);
animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_03),200);
animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_04),200);
animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_05),200);
animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_06),200);
animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_07),200);
animationDrawable.addFrame(getResources().getDrawable(R.mipmap.progress_loading_image_08),200);
imageView.setImageDrawable(animationDrawable);
}
@Event(value = {R.id.start_animation,R.id.stop_animation},type = View.OnClickListener.class)
private void OnClickFram_Button(View v){
switch(v.getId()){
case R.id.start_animation:
if(!animationDrawable.isRunning()){
animationDrawable.start();//如果动画没有动 就开始
}
break;
case R.id.stop_animation:
if(animationDrawable.isRunning()){
animationDrawable.stop();//如果动画在运行则停止
}
break;
}
}
}
补间动画:
- alpha实现淡入淡出
/**
* 淡入淡出
*/
private void alphaAnimation() {
imageView.setVisibility(View.VISIBLE);
AlphaAnimation animation = new AlphaAnimation(0.0f,1.0f);//设置淡入淡出
animation.setDuration(2000);//两秒内的动画
animation.setFillAfter(true);//设置动画完成后 保持当前位置
imageView.startAnimation(animation);//启动动画
}
- rotate实现转圈:
/**
* 转圈
*/
private void MyRotateAnimation() {
RotateAnimation animation = new RotateAnimation(0,360,100,300);
animation.setDuration(300);
animation.setFillAfter(true);//设置 动画结束后位置不变
imageView.startAnimation(animation);
}
- Scale实现放大与缩小:
/**
* 缩放动画
*/
private void MyScaleAnimation() {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.my_scale);
animation.setFillBefore(false);
animation.setDuration(3000);
animation.setRepeatCount(5);
imageView.startAnimation(animation);
}
- 位移动画:
/**
* 位移动画
*/
private void MyTranslate() {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.my_translate);
animation.setFillAfter(true);
animation.setDuration(3000);
animation.setRepeatCount(5);
imageView.startAnimation(animation);
}
集合动画:
创建anim文件夹:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha android:fromAlpha="0.0" android:toAlpha="1.0"/>
<rotate android:pivotX="100" android:pivotY="100" android:fromDegrees="0" android:toDegrees="360"/>
<translate android:fromXDelta="100" android:fromYDelta="100" android:toXDelta="300" android:toYDelta="300"/>
</set>
java代码:
/**
* 集合动画
*/
private void allAnimation() {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.my_set);
animation.setDuration(5000);
animation.setFillAfter(true);
imageView.startAnimation(animation);
}