view 跨activity的动画

来源:互联网 发布:java基础教程视频下载 编辑:程序博客网 时间:2024/06/03 19:42

如果有两个activity,现在有一个view想从A activity跨到B activity,如何实现?

实现方法的出发点就是不能单对一个view进行,需要将这个view保存为bitmap,然后将这个bitmap hold住,所有的动画效果都是对这个bitmap来进行的

下面就是部分代码:A activity

                    DisplayMetrics dm = new DisplayMetrics();                    getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);                    int statusBarHeight = dm.heightPixels - getView().getMeasuredHeight();                    int[] location = new int[2];                    view.getLocationInWindow(location);                    int mSelectItemX = location[0];                    int mSelectItemY = location[1] - statusBarHeight;                    view.setDrawingCacheEnabled(true);                    final Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());                    view.setDrawingCacheEnabled(false);                    Session.getInstance().memoryModel.setSessionBitmap(bitmap, mSelectItemX, mSelectItemY);                    getActivity().overridePendingTransition(0, 0);
计算view在A activity的位置,然后将view生成的bitmap和坐标保存下来,在B activity中进行处理:

        if (Session.hasMemoryModel() && sharedRootView != null) {            isEnableAnimation = true;            sharedRootView.setVisibility(View.VISIBLE);            sharedGroupView.setX(0);            final ImageView sharedStartView = (ImageView) sharedRootView.findViewById(R.id.shared_start_view);            sharedStartView.setImageBitmap(Session.memoryModel.getSessionBitmap());            final int realY = Session.memoryModel.y - DeviceUtil.getPixelFromDip(50);            ValueAnimator animator = ValueAnimator.ofFloat(0, 1);            animator.setDuration(600);            animator.addListener(new Animator.AnimatorListener() {                @Override                public void onAnimationStart(Animator animation) {                    sharedEndParentView.setAlpha(0f);                    sharedEndParentView.setVisibility(View.VISIBLE);                }                @Override                public void onAnimationEnd(Animator animation) {                    onTransitionEnd();                    sharedRootView.setVisibility(View.GONE);                    isEnterAnimationEnd = true;                    Session.clearMemoryModel();                }                @Override                public void onAnimationCancel(Animator animation) {                }                @Override                public void onAnimationRepeat(Animator animation) {                }            });            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {                @Override                public void onAnimationUpdate(ValueAnimator animation) {                    float factor = (Float) animation.getAnimatedValue();                    float transitionY = realY * (1 - factor);                    sharedGroupView.setTranslationY(transitionY);                    if (factor <= 0.5) {                        sharedStartView.setAlpha(1 - 2 * factor);                    } else {                        sharedEndParentView.setAlpha(2 * (factor - 0.5f));                    }                    onTransition(factor);                }            });            animator.start();        }

布局文件:B activity

<!-- 共享元素容器 --><RelativeLayout    xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@+id/flight_shared_rootview"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:layout_below="@id/title_view">    <FrameLayout        android:id="@+id/flight_shared_group_id"        android:layout_width="match_parent"        android:layout_height="wrap_content">        <!-- 第一块初始元素 -->        <ImageView            android:id="@+id/flight_shared_start_view"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_gravity="center_horizontal"/>        <!-- 第二块结束元素(共享元素中的信息 -->        <LinearLayout            android:id="@+id/flight_subshared_segment_layout"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginLeft="@dimen/dimen_8dp"            android:layout_marginRight="@dimen/dimen_8dp"            android:orientation="vertical"            android:visibility="invisible"            android:background="@drawable/flight_list_bg_normal">            </LinearLayout>    </FrameLayout></RelativeLayout>



0 0
原创粉丝点击