前言:上一章《Android开发之安卓属性动画大总结》大致介绍了安卓的属性动画以及使用时的注意点,今天我们就来一个小综合案列,实现类似于苹果浏览器的跳转页面!
----------------------分割线------------------------
来看下效果图:
----------------------分割线------------------------
分析:1.购买详情页面设置成INVISIBLE。
2.首页面同时做翻转、透明、缩放然后往上平移动画
3.详情页面开始显示出来。
4.退出的时候,动画反过来再做一遍。
----------------------分割线------------------------
代码奉上:
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
private LinearLayout first;
private ImageView second;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
first = (LinearLayout) findViewById(R.id.first);
second = (ImageView) findViewById(R.id.second);
button = (Button) findViewById(R.id.bt);
}
public void startFirstAnimation(View view) {
/**
* 1)first_View动画:1.翻转动画;2.透明度动画;3.缩放动画
*/
//翻转
ObjectAnimator firstRotationAnim = ObjectAnimator.ofFloat(first, "rotationX", 0f, 25f, 0f);
firstRotationAnim.setDuration(400);
//透明度
ObjectAnimator firstAlphaAnim = ObjectAnimator.ofFloat(first, "alpha", 1f, 0.5f);
firstAlphaAnim.setDuration(300);
//缩放
ObjectAnimator firstScaleXAnim = ObjectAnimator.ofFloat(first, "scaleX", 1f, 0.8f);
firstScaleXAnim.setDuration(300);
ObjectAnimator firstScaleYAnim = ObjectAnimator.ofFloat(first, "scaleY", 1f, 0.8f);
firstScaleYAnim.setDuration(300);
//平移上去
ObjectAnimator firstTranslationYAnim = ObjectAnimator.ofFloat(first, "translationY", 0f, -0.1f * first.getHeight());
firstTranslationYAnim.setDuration(300);
//把隐藏的给显示出来
ObjectAnimator sencondTranslationYAnim = ObjectAnimator.ofFloat(second, "translationY", second.getHeight(), 0);
sencondTranslationYAnim.setDuration(300);
sencondTranslationYAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
second.setVisibility(View.VISIBLE);
button.setClickable(false);
}
});
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(firstRotationAnim, firstAlphaAnim, firstScaleXAnim, firstScaleYAnim, firstTranslationYAnim, sencondTranslationYAnim);
animatorSet.start();
}
public void startSecondAnimation(View view) {
//翻转
ObjectAnimator firstRotationAnim = ObjectAnimator.ofFloat(first, "rotationX", 0f, 25f, 0f);
firstRotationAnim.setDuration(400);
//透明度
ObjectAnimator firstAlphaAnim = ObjectAnimator.ofFloat(first, "alpha", 0f, 1f);
firstAlphaAnim.setDuration(300);
//缩放
ObjectAnimator firstScaleXAnim = ObjectAnimator.ofFloat(first, "scaleX", 0.8f, 1f);
firstScaleXAnim.setDuration(300);
ObjectAnimator firstScaleYAnim = ObjectAnimator.ofFloat(first, "scaleY", 0.8f, 1);
firstScaleYAnim.setDuration(300);
//把first平移下去
ObjectAnimator firstTranslationYAnim = ObjectAnimator.ofFloat(first, "translationY", -0.1f * first.getHeight(), 0f);
firstTranslationYAnim.setDuration(300);
//把第二个view下移
ObjectAnimator sencondTranslationYAnim = ObjectAnimator.ofFloat(second, "translationY", 0f, second.getHeight());
sencondTranslationYAnim.setDuration(300);
sencondTranslationYAnim.start();
sencondTranslationYAnim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
second.setVisibility(View.INVISIBLE);
}
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
button.setClickable(true);
}
});
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.playTogether(firstRotationAnim, firstAlphaAnim, firstScaleXAnim, firstScaleYAnim, sencondTranslationYAnim, firstTranslationYAnim);
animatorSet.start();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/first"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/img01"
android:orientation="vertical">
<Button
android:id="@+id/bt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f00"
android:onClick="startFirstAnimation"
android:text="显示" />
</LinearLayout>
<ImageView
android:id="@+id/second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:clickable="true"
android:onClick="startSecondAnimation"
android:scaleType="fitEnd"
android:src="@drawable/img02"
android:visibility="invisible" />
</RelativeLayout>
----------------------完------------------------