android界面切换动画

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wu_zi/article/details/78318203

最近工作不是很紧张,所以下载了“小秋魔盒”的代码在研究。整个应用使用了很多的第三方框架,用很简单的代码实现了比较炫的效果。


首页列表的item点击跳转页面效果比较好,于是研究了一下。

跳转源码:

  //设置监听,监听是自己写的
        homeAdapter.setOnItemClickListener(new OnItemClickListener() {
            Intent intent = null;

            @Override
            public void onItemClick(View view, int position) {
//                Toast.makeText(context, "这是第" + position + "个item", Toast.LENGTH_SHORT).show();
                intent = new Intent(context, listActivity.get(position));

                ActivityOptionsCompat optionsCompat = ActivityOptionsCompat.makeSceneTransitionAnimation(
                        getActivity(),
                        view.findViewById(R.id.iv_logo),
                        getString(R.string.transition_wechat_img)
                );

                ActivityCompat.startActivity(getContext(), intent, optionsCompat.toBundle());
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });

关键代码就是ActivityOptionsCompat.makeSceneTransitionAnimation。

查看了一下这个静态方法的源码:

/**
     * Create an ActivityOptions to transition between Activities using cross-Activity scene
     * animations. This method carries the position of one shared element to the started Activity.
     * The position of <code>sharedElement</code> will be used as the epicenter for the
     * exit Transition. The position of the shared element in the launched Activity will be the
     * epicenter of its entering Transition.
     *
     * <p>This requires {@link android.view.Window#FEATURE_CONTENT_TRANSITIONS} to be
     * enabled on the calling Activity to cause an exit transition. The same must be in
     * the called Activity to get an entering transition.</p>
     * @param activity The Activity whose window contains the shared elements.
     * @param sharedElement The View to transition to the started Activity. sharedElement must
     *                      have a non-null sharedElementName.
     * @param sharedElementName The shared element name as used in the target Activity. This may
     *                          be null if it has the same name as sharedElement.
     * @return Returns a new ActivityOptions object that you can use to
     *         supply these options as the options Bundle when starting an activity.
     */
    public static ActivityOptionsCompat makeSceneTransitionAnimation(Activity activity,
            View sharedElement, String sharedElementName) {
        if (Build.VERSION.SDK_INT >= 24) {
            return new ActivityOptionsCompat.ActivityOptionsImpl24(
                    ActivityOptionsCompat24.makeSceneTransitionAnimation(activity,
                            sharedElement, sharedElementName));
        } else if (Build.VERSION.SDK_INT >= 23) {
            return new ActivityOptionsCompat.ActivityOptionsImpl23(
                    ActivityOptionsCompat23.makeSceneTransitionAnimation(activity,
                            sharedElement, sharedElementName));
        } else if (Build.VERSION.SDK_INT >= 21) {
            return new ActivityOptionsCompat.ActivityOptionsImpl21(
                    ActivityOptionsCompat21.makeSceneTransitionAnimation(activity,
                            sharedElement, sharedElementName));
        }
        return new ActivityOptionsCompat();
    }

注释大概的意思就是说,通过一个中间过渡的界面创造界面的中间过渡动画。

方法将一个共享元素的位置传递给要启动的activity。

共享的元素(就是界面中的某个view)将作为退出的界面的中心。

这个共享的view 也会是进入的新页面的中心。

哎,英语水平不佳,大概意思就是那样吧。

之前看过一个仿微信浏览照片的效果,也跟这个类似,

但是他的写法十分复杂还需要记录和计算位置,通过上面的代码非常简单。

只需要在退出和启动的两个界面中的某个view加上android:transitionName属性就行了。

属性的值要一样哦。

猜你喜欢

转载自blog.csdn.net/wu_zi/article/details/78318203