上一篇博客当中,为大家提到了补间动画的特点和可以实现的效果,我们通过xml文件当中编写动画行为,可以对于view控件进行设置,然后我们也提到了,补间动画除了可以在xml布局当中编写,同样可以在java代码当中设计。那么在这篇博客当中,一起看看如何在java代码当中描述补间动画的各个行为。
直接写代码:
1.编写布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/btn_alpha"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="透明度"/>
<Button
android:id="@+id/btn_scale"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="缩放"/>
<Button
android:id="@+id/btn_translate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="位移"/>
<Button
android:id="@+id/btn_rotate"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="旋转"/>
<Button
android:id="@+id/btn_set"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClick"
android:text="集合"/>
</LinearLayout>
<ImageView
android:id="@+id/tween_iv"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@mipmap/image_info11"
android:layout_centerInParent="true"/>
</RelativeLayout>
public class TweenActivity extends AppCompatActivity {
private ImageView tweenIv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tween);
tweenIv = (ImageView) findViewById(R.id.tween_iv);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_alpha:
// 实例化透明度动画的对象
AlphaAnimation alphaAnimation = new AlphaAnimation(0.2f, 1.0f);
alphaAnimation.setDuration(3000);
alphaAnimation.setFillAfter(true); //设置保存改变后的状态
alphaAnimation.setRepeatCount(1); //设置重复的次数
alphaAnimation.setRepeatMode(Animation.RESTART); //设置重复的模式
tweenIv.startAnimation(alphaAnimation);
break;
case R.id.btn_scale:
// 实例化放缩动画的对象
// float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue
ScaleAnimation scaleAnimation = new ScaleAnimation(1f, 2f, 1f, 2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
scaleAnimation.setDuration(3000);
scaleAnimation.setFillAfter(true);
scaleAnimation.setRepeatCount(1);
scaleAnimation.setRepeatMode(Animation.REVERSE);
tweenIv.startAnimation(scaleAnimation);
break;
case R.id.btn_translate:
TranslateAnimation translateAnimation = new TranslateAnimation(0, 200, 0, 200);
translateAnimation.setDuration(3000);
translateAnimation.setFillAfter(true);
tweenIv.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.setRepeatCount(1);
rotateAnimation.setRepeatMode(Animation.REVERSE);
tweenIv.startAnimation(rotateAnimation);
break;
case R.id.btn_set:
AnimationSet set = new AnimationSet(true);
AlphaAnimation alphaAnime = new AlphaAnimation(0.2f, 1.0f);
RotateAnimation rotateAnime = new RotateAnimation(0, 900, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
// 因为补间动画集合指的是同时可以播放两种以上的动画,所以要把动画添加到集合中
set.addAnimation(alphaAnime);
set.addAnimation(rotateAnime);
set.setDuration(3000);
set.setFillAfter(true);
tweenIv.startAnimation(set);
break;
}
}
}