1.主函数
public class TweenActivity02 extends AppCompatActivity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tween02);
iv = (ImageView) findViewById(R.id.icon);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_alpha:
// 透明度动画:1,实例化透明度动画的对象
// 第一个参数:表示起初的透明度 第二个参数:表示结尾的透明度
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.2f);
// 设置持续的持续的时间
alphaAnimation.setDuration(3000);
// 设置保存的状态
alphaAnimation.setFillAfter(true);
// 设置重复的次数
alphaAnimation.setRepeatCount(1);
// 设置重复的模式
alphaAnimation.setRepeatMode(Animation.REVERSE);
// 为控件启动动画
iv.startAnimation(alphaAnimation);
break;
case R.id.btn_scale:
/**fromX :x轴起始位置
* toX :x轴结束位置
* fromY :y轴起始位置
* toY :y轴结束位置
* pivotXType : x轴缩放中心点类型
* Animation.RELATIVE_TO_SELF
* Animation.RELATIVE_TO_PARENT
* pivotXValue : x轴缩放中心点值
* pivotYType . : y轴缩放中心点类型
* pivotYValue : y轴缩放中心点值
*/
// 1.实例化对象
ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 3f, 1f, 3f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(3000);
scaleAnimation.setFillAfter(true);
iv.startAnimation(scaleAnimation);
break;
case R.id.btn_tranlate:
TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 200);
translateAnimation.setDuration(2000);
translateAnimation.setFillAfter(false);
translateAnimation.setRepeatMode(Animation.REVERSE);
translateAnimation.setRepeatCount(1);
iv.startAnimation(translateAnimation);
break;
case R.id.btn_rotate:
RotateAnimation rotateAnimation = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(3000);
rotateAnimation.setFillAfter(true);
iv.startAnimation(rotateAnimation);
break;
case R.id.btn_set:
// 1.实例化集合动画对象
/**
* 参数: 分享插值器
* if true :使用set默认的插值器
* if false :使用动画自己的插值器
* */
AnimationSet set = new AnimationSet(true);
AlphaAnimation alphaAnim = new AlphaAnimation(1.0f, 0.1f);
RotateAnimation rotateAnim = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
// 2将动画放入到动画集当中
set.addAnimation(alphaAnim);
set.addAnimation(rotateAnim);
set.setDuration(5000);
set.setFillAfter(true);
iv.startAnimation(set);
break;
}
}
}
2.主布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.animee.day02.TweenActivity02">
<Button
android:id="@+id/btn_alpha"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="透明度动画"
android:onClick="onClick"/>
<Button
android:id="@+id/btn_scale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="缩放渐变动画"
android:onClick="onClick"/>
<Button
android:id="@+id/btn_tranlate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="移动动画"
android:onClick="onClick"/>
<Button
android:id="@+id/btn_rotate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="旋转动画"
android:onClick="onClick"/>
<Button
android:id="@+id/btn_set"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="集合动画"
android:onClick="onClick"/>
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="@+id/icon"
android:src="@mipmap/h2"
android:layout_gravity="center"/>
</LinearLayout>