Android中常用Animation代码片段

1.图片预览界面 点击任何地方 标题栏和底部menu都隐藏或者显示

private void switchMenu() {
        //mTitleBar是标题栏,mBottomView是底部菜单
        float titleBarY = mTitleBar.getTranslationY();
        int translationY = 0;
        if (titleBarY == 0) {
            translationY = -mTitleBar.getHeight();
        }
        mTitleBar.animate().translationY(translationY).setInterpolator(new DecelerateInterpolator()).start();

        float bottomY = mBottomView.getTranslationY();
        translationY = 0;
        if (bottomY == 0) {
            translationY = mBottomView.getHeight();
        }
        mBottomView.animate().translationY(translationY).setInterpolator(new DecelerateInterpolator()).start();
    }

2.微信图片选择器,图片文件夹弹出和隐藏效果

//文件夹布局,是盖在GridView上面的
<RelativeLayout
        android:id="@+id/photoFolderWraper"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/layout_titlebar"
        android:layout_above="@id/folderWraper"
        android:background="#CC000000"
        android:visibility="visible" >

        <ListView
            android:id="@+id/photoFolderList"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/white"
            android:paddingTop="5dp"
            android:layout_marginTop="100dp"
            android:cacheColorHint="@android:color/transparent"
            android:divider="#d9d9d9"
            android:dividerHeight="1dp"
            android:fadingEdge="none"
            android:fastScrollEnabled="false"
            android:footerDividersEnabled="false"
            android:headerDividersEnabled="false"
            android:listSelector="@android:color/transparent"
            android:scrollbarStyle="outsideOverlay"
            android:scrollbars="none"
            android:scrollingCache="false" >
        </ListView>
    </RelativeLayout>


private void togglePhotoFolder() {
//思路:让photoFolderWraper背景渐变的同时photoFolderList移动
photoFolderWraper = (RelativeLayout) findViewById(R.id.photoFolderWraper);
photoFolderList = (ListView) findViewById(R.id.photoFolderList);

        if (photoFolderWraper.getVisibility() == View.VISIBLE) {
            final AnimatorSet animatorSet = new AnimatorSet();
            ObjectAnimator listBgAnimator = ObjectAnimator.ofFloat(photoFolderWraper, "alpha", 1.0f, 0.0f);
            ObjectAnimator listAnimator = ObjectAnimator.ofFloat(photoFolderList, "translationY", 0, photoFolderList.getHeight());
            animatorSet.play(listAnimator).with(listBgAnimator);
            animatorSet.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    photoFolderWraper.setVisibility(View.GONE);
                }
            });
            animatorSet.start();
        } else {
            final AnimatorSet animatorSet = new AnimatorSet();
            ObjectAnimator listBgAnimator = ObjectAnimator.ofFloat(photoFolderWraper, "alpha", 0.0f, 1.0f);
            //这里有个问题 第一次photoFolderList是不可见的,photoFolderList.getHeight()=0,没有动画效果
            ObjectAnimator listAnimator = ObjectAnimator.ofFloat(photoFolderList, "translationY", photoFolderList.getHeight(), 0);
            animatorSet.play(listBgAnimator).with(listAnimator);
            animatorSet.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationStart(Animator animation) {
                    photoFolderWraper.setVisibility(View.VISIBLE);
                }
            });
            animatorSet.start();
        }
    }

转载于:https://my.oschina.net/bruces/blog/1571511

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值