AndroidSample之CardFlipActivity的学习

时间:2021-04-20 18:33:23
/**
 * 动画可以添加微妙的视觉线索,通知用户在你的应用程序中发生了什么,并改善你的应用程序的界面的心理模型。当屏幕改变状态时,动画是特别有用的,记住,这种过度使用动画或利用他们在错误的时间可以是有害的,如当他们造成延误。实现一些常见的类型的动画,可以增加可用性,而不会打扰您的用户。

 */

卡片翻转

AndroidSample之CardFlipActivity的学习

核心代码

    private void flipCard() {
if (mShowingBack) {
getFragmentManager().popBackStack();
return;
}
mShowingBack = true;
// 创建和提交一个新的fragment事务,增加回退card使用自定义的动画
getFragmentManager()
.beginTransaction()

// 设置替换这个默认fragment的动画和翻转到前面的card时候动画
/**
* int enter,
* int exit,
* int popEnter,
* int popExit
*/
.setCustomAnimations(
R.animator.card_flip_right_in, R.animator.card_flip_right_out,
R.animator.card_flip_left_in, R.animator.card_flip_left_out)

// 用这个fragment替换当前的容器来代表下一页
.replace(R.id.container, new CardBackFragment())
// 为这个回退栈增加这个变换,允许用户点击back得到前面的card
.addToBackStack(null)
//提交事务
.commit();

// 有时候在下面直接进行刷新菜单是无效的,因为commit操作是异步的,而是发送消息队列到主线程中
mHandler.post(new Runnable() {
@Override
public void run() {
invalidateOptionsMenu();
}
});
}
监听翻转改变的事件

        // 增加回退栈改变监听事件,用来动态改变ActionBar的图标
getFragmentManager().addOnBackStackChangedListener(this);
    @Override    public void onBackStackChanged() {        mShowingBack = (getFragmentManager().getBackStackEntryCount() > 0);        // 当这个回退栈发生改变,刷新Menu        invalidateOptionsMenu();    }

淡入淡出动画

核心代码

        // 为这个显示的View设置透明并设置可见
showView.setAlpha(0f);
showView.setVisibility(View.VISIBLE);

// 为showView设置从透明到不透明的动画效果,并可以通过ViewPropertyAnimator设置相应的监听
showView.animate()
.alpha(1f)
.setDuration(mShortAnimationDuration)
.setListener(null);

// 为hideView设置从不透明到透明的动画之后设置监听当动画结束将其隐藏
hideView.animate()
.alpha(0f)
.setDuration(mShortAnimationDuration)
.setListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
hideView.setVisibility(View.GONE);
}
});
对于SampleAnimation我们还学到:

(1)设置TextView可选择

android:textIsSelectable="true"
(2)得到系统简短动画的时间
<span style="font-size:12px;">        // 检索和缓存系统的默认动画时间        mShortAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);</span>
(3)为应用设置导航
                //设置导航,从源Activity到目的Activity,为Intent设置了FLAG_ACTIVITY_CLEAR_TOP,将任务栈目的Activity上面的Activity都finish掉                // 并将源Activity finish掉                NavUtils.navigateUpTo(this, new Intent(this, MainActivity.class));
布局改变动画

AndroidSample之CardFlipActivity的学习

添加很方便只需要在容器View添加android:animateLayoutChanges=true即可

<span style="font-size:12px;color:#000000;">    <ScrollView android:layout_width="match_parent"
android:layout_height="match_parent">

<!-- Note that this LinearLayout has the "animateLayoutChanges" property set to true.
This tells the framework to automatically animate child views (in this case, rows)
as they are added to and removed from the LinearLayout. -->
<LinearLayout android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:showDividers="middle"
android:divider="?android:dividerHorizontal"
android:animateLayoutChanges="true"
android:paddingLeft="16dp"
android:paddingRight="16dp" />

</ScrollView></span>
接下来直接在代码中直接调用即可

        // 因为容器View有android:animateLayoutChanges设置了true,添加这个View会有自动的动画
mContainerView.addView(newView, 0);