Android中常用Animation代码片段

2017-11-15 11:32:18来源:oschina作者:GalaxyBruce人点击

分享

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上面的
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" >
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" >

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();
}
}

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台