如何在网格recyclelerview中的android中进行反向爆炸过渡动画

时间:2017-01-05 07:57:21

标签: android animation android-transitions

我已经发布了我的代码,用于在android中使用过渡动画在网格recyclelerview中爆炸内容。如何实现反向爆炸,就像我进入ExplodeAnimationActivity时,recyclerview中的所有项目都应该反过来爆炸。

enter image description here

public class ExplodeAnimationActivity extends AppCompatActivity {
private final String android_version_names[] = {
        "Donut",
        "Eclair",
        "Froyo",
        "Gingerbread",
        "Honeycomb",
        "Ice Cream Sandwich",
        "Jelly Bean",
        "KitKat",
        "Lollipop",
        "Marshmallow"
};

private final String android_image_urls[] = {
        "http://api.learn2crack.com/android/images/donut.png",
        "http://api.learn2crack.com/android/images/eclair.png",
        "http://api.learn2crack.com/android/images/froyo.png",
        "http://api.learn2crack.com/android/images/ginger.png",
        "http://api.learn2crack.com/android/images/honey.png",
        "http://api.learn2crack.com/android/images/icecream.png",
        "http://api.learn2crack.com/android/images/jellybean.png",
        "http://api.learn2crack.com/android/images/kitkat.png",
        "http://api.learn2crack.com/android/images/lollipop.png",
        "http://api.learn2crack.com/android/images/marshmallow.png"
};

RecyclerView recyclerView;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_animations);
    initViews();
}

private void initViews() {
    recyclerView = (RecyclerView) findViewById(R.id.card_recycler_view);
    recyclerView.setHasFixedSize(true);
    RecyclerView.LayoutManager layoutManager = new GridLayoutManager(getApplicationContext(), 2);
    recyclerView.setLayoutManager(layoutManager);

    ArrayList<AndroidVersion> androidVersions = prepareData();
    ExplodeAnimationAdapter adapter = new ExplodeAnimationAdapter(getApplicationContext(), androidVersions);
    recyclerView.setAdapter(adapter);

    recyclerView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // save rect of view in screen coordinates
            final Rect viewRect = new Rect();
            v.getGlobalVisibleRect(viewRect);

            // create Explode transition with epicenter
            Transition explode = new Explode();
            explode.setEpicenterCallback(new Transition.EpicenterCallback() {
                @Override
                public Rect onGetEpicenter(Transition transition) {
                    return viewRect;
                }
            });
            explode.setDuration(1000);
            TransitionManager.beginDelayedTransition(recyclerView, explode);

            // remove all views from Recycler View
            recyclerView.setAdapter(null);
            return false;
        }
    });

}
 private ArrayList<AndroidVersion> prepareData() {

    ArrayList<AndroidVersion> android_version = new ArrayList<>();
    for (int i = 0; i < android_version_names.length; i++) {
        AndroidVersion androidVersion = new AndroidVersion();
        androidVersion.setAndroid_version_name(android_version_names[i]);
        androidVersion.setAndroid_image_url(android_image_urls[i]);
        android_version.add(androidVersion);
    }
    return android_version;
}
}

1 个答案:

答案 0 :(得分:0)

最新答案,也许您自己找到了答案。我尝试了与您相同的示例,但进行了一些更改,并且仅通过设置“过渡”模式就可以反转过渡。我还没有在回收站视图中做到这一点,但是它也应该起作用。

pivot_longer()

如果您的转换要撤消,则可以进行以下更改:

private Explode makeInExplodeTransition() {
    Explode explode = new Explode();
    explode.setDuration(3000);
    explode.setMode(Visibility.MODE_OUT); 
    explode.setInterpolator(new OvershootInterpolator());
    return explode;
}

就我而言,它似乎没有用。我开始了活动,直接制作了动画,视图在那里没有动画。因此,我开始延迟播放动画。我认为问题在于动画是在活动完全可见之前完成的,可能是几毫秒的时间。