KShareViewActivityManager

Introduction: 一个兼容 Android 5.0 以下 Shared Element Transition (页面间共享元素位移动画)库
More: Author   ReportBugs   
Tags:

一个兼容 Android 5.0 以下 Shared Element Transition (页面间共享元素位移动画)库

精简到了一个类

2016 1 25 更新 支持 TextView 的子类颜色渐变了

效果图: 将就看吧

图片地址:http://image17-c.poco.cn/mypoco/myphoto/20160413/11/17425403720160413113956013.gif?415x620_110

最近开发了一个 APP 名字叫番茄 ToDo,是一个番茄钟和 ToDoList 结合的 APP(对,我就是嫌番茄土豆太难用太反人类了自己写的)喜欢的朋友可以支持一下:D,各大应用商店都可以下载。

先说缺点吧:在第一个 Activity 中的目标 View 是 wrap_content 时,位移会偏移一点点

使用方法:

A->B

1.在第一个 Activity 中:

  img = (ImageView) findViewById(R.id.img);
  title = (TextView) findViewById(R.id.title);
  KShareViewActivityManager.getInstance(MainActivity.this).startActivity(MainActivity.this, SecondActivity.class,R.layout.activity_main,R.layout.activity_second, img, title);

第三个参数是要进行动画的 View 所在的 layout 的 xml ,举例:如果要对 ListView 的 Item 进行动画,请传入 item 布局的 xml。

第四个参数 R.layout.activity_second 传入的是第二个 Activity 的布局 layout id,之后的参数是指需要共享元素动画的 View

如何让 Manager 知道是哪两个 View 在页面之间对应呢?tag 一样即可,如:activity_main 中:

 <ImageView
         android:id="@+id/img"
         android:layout_width="100dp"
         android:layout_height="100dp"
         android:layout_marginLeft="25dp"
         android:layout_marginTop="25dp"
         android:background="@drawable/splash"
         android:tag="img" />

在 activity_second 中:

 <ImageView
         android:id="@+id/img_two"
         android:layout_width="250dp"
         android:layout_height="250dp"
         android:layout_centerInParent="true"
         android:background="@drawable/splash"
         android:tag="img" />

tag 一样即可

B->A (回退)

请在 ActivityB 中添加:

@Override
public void onBackPressed() {
    KShareViewActivityManager.getInstance(SecondActivity.this).finish(SecondActivity.this);
}

注意事项

因为是直接操作的 Content 显示区域,所以支持 Activity ,Fragment 互相跳,只要是在屏幕中显示的 View,都可以进行动画

由于进行动画的 View 并不是原来的 View,而是重新 inflate 出来的,因此部分自定义属性并没有被赋值,Manager 对常见的 TextView 和 ImageView 的部分属性参照原 View 进行了复制,但如果是其他 View 的其他属性,还请在 changeViewProperty(View view) 这个回调函数中对其自定义属性进行设置。 如:

  KShareViewActivityManager.getInstance(this).withAction(new KShareViewActivityAction() {
                    @Override
                    public void onAnimatorStart() {
                    }
                    @Override
                    public void onAnimatorEnd() {
                    }
                    @Override
                    public void changeViewProperty(View view) {
                        if(view.getTag().equals("text")){
                            ((MyTextView)view).setOtherProperty(oringinTextView.getOtherProperty);
                        }
                        if(view.getTag().equals("xxx")){
                            ...
                        }
                    }
                });

好啦

其他用法(添加监听器,设置动画时间),以下是第二张效果图的代码:

  KShareViewActivityManager.getInstance(this).withAction(new KShareViewActivityAction() {
                    @Override
                    public void onAnimatorStart() {
                    }
                    @Override
                    public void onAnimatorEnd() {
                    }
                    @Override
                    public void changeViewProperty(View view) {
                        view.setBackgroundColor(0x00ffffff);
                    }
                }).setDuration(1000).startActivity(this, CountDownActivity.class, R.layout.list_item, R.layout.activity_count_down,
                                 (View) ((CommandEvent) event).obj);

差点忘了,还可以 withIntent 来自定义 Intent ,同时 withIntentAndRequestCode(Intent intent,int requestCode) 也可以达到 startActivityForResult 的目的。

License

Copyright 2015 Kot32

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Apps
About Me
GitHub: Trinea
Facebook: Dev Tools