您的位置:首页 > 其它

三种动画的简单用法——学习笔记

2017-10-11 21:19 387 查看
AlphaAnimation——透明度渐变动画

ScaleAnimation ——缩放动画

RotateAnimation——旋转动画

①ScaleAnimation动画相关方法的参数:

ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

参数说明:

float fromX 动画起始时 X坐标上的伸缩尺寸

float toX 动画结束时 X坐标上的伸缩尺寸

float fromY 动画起始时Y坐标上的伸缩尺寸

float toY 动画结束时Y坐标上的伸缩尺寸

int pivotXType 动画在X轴相对于物件位置类型

float pivotXValue 动画相对于物件的X坐标的开始位置

int pivotYType 动画在Y轴相对于物件位置类型

float pivotYValue 动画相对于物件的Y坐标的开始位置

②RotateAnimation动画相关方法的参数

RotateAnimation (float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

参数说明:

float fromDegrees:旋转的开始角度。

float toDegrees:旋转的结束角度。

int pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。

float pivotXValue:X坐标的伸缩值。

int pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。

float pivotYValue:Y坐标的伸缩值。

以下是一个简单的例子

布局界面代码为:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/splash_bg"
android:id="@+id/splash_root">

<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_laugh"
android:layout_centerInParent="true"/>

</RelativeLayout>


Activity代码为:

public class SplashActivity extends AppCompatActivity {
private RelativeLayout splash_rl_root;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
splash_rl_root = (RelativeLayout) findViewById(R.id.splash_root);

//动画
AlphaAnimation aa = new AlphaAnimation(0, 1); //渐变动画
//aa.setDuration(500); //持续播放时间
aa.setFillAfter(true); //动画结束后保持状态

ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1, ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
ScaleAnimation.RELATIVE_TO_SELF, 0.5f); //缩放动画
//sa.setDuration(500);
sa.setFillAfter(true);

RotateAnimation ra = new RotateAnimation(0, 360, RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f); //旋转动画
// ra.setDuration(500);
ra.setFillAfter(true);

AnimationSet set = new AnimationSet(false); //是否加速
//添加三个动画没有先后顺序
set.addAnimation(aa);
set.addAnimation(sa);
set.addAnimation(ra);
set.setDuration(2000); //同时设置播放时间

splash_rl_root.startAnimation(set); //同时播放三个动画
set.setAnimationListener(new MyAnimationListener()); //给动画设置一个监听

}

class MyAnimationListener implements Animation.AnimationListener{

/*
当动画开始播放的时候调用
*/
@Override
public void onAnimationStart(Animation animation) {

}

/*
当动画播放结束的时候调用
*/
@Override
public void onAnimationEnd(Animation animation) {
Toast.makeText(SplashActivity.this, "动画播放完成!", Toast.LENGTH_SHORT).show();
}

/*
当动画重复播放的时候调用
*/
@Override
public void onAnimationRepeat(Animation animation) {

}
}
}


下面是运行图片

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: