个人练习代码库/Material Design Part3/Ac过渡动画Transition

1.主MainAc

import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;

public class TransitionActivity extends AppCompatActivity {
    private Intent intent;

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

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void explode(View view){
        intent=new Intent(this,TransitionActivityTo.class);
        intent.putExtra("flag",0);
        startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void slide(View view){
        intent=new Intent(this,TransitionActivityTo.class);
        intent.putExtra("flag",1);
        startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void fade(View view){
        intent=new Intent(this,TransitionActivityTo.class);
        intent.putExtra("flag",2);
        startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this).toBundle());
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void share(View view){
        View fab=findViewById(R.id.fab_button);
        intent=new Intent(this,TransitionActivityTo.class);
        intent.putExtra("flag",3);
        startActivity(intent,ActivityOptions.makeSceneTransitionAnimation(this
                      , Pair.create(fab,"fab")
                      , Pair.create(view,"share")).toBundle());
    }
}

辅MainAc

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.transition.Explode;
import android.transition.Fade;
import android.transition.Slide;
import android.view.Window;

import java.util.Objects;

public class TransitionActivityTo extends AppCompatActivity {

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
        int flag = Objects.requireNonNull(getIntent().getExtras()).getInt("flag");
        // 设置不同的动画效果
        switch (flag) {
            case 0:
                getWindow().setEnterTransition(new Explode());
                getWindow().setExitTransition(new Explode());
                break;
            case 1:
                getWindow().setEnterTransition(new Slide());
                getWindow().setExitTransition(new Slide());
                break;
            case 2:
                getWindow().setEnterTransition(new Fade());
                getWindow().setExitTransition(new Fade());
                break;
            case 3:

                break;
        }
        setContentView(R.layout.activity_transition_to);
    }
}

主MainAc.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">


    <Button
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:onClick="explode"
        android:text="explode" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:onClick="slide"
        android:text="slide" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:onClick="fade"
        android:text="fade" />

    <Button
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:transitionName="share"
        android:onClick="share"
        android:text="share"
        tools:targetApi="lollipop" />

    <Button
        android:id="@+id/fab_button"
        android:layout_width="56dp"
        android:transitionName="fab"
        android:layout_height="56dp"
        android:background="@drawable/ripple_round"
        android:elevation="5dp"
        tools:targetApi="lollipop" />
</LinearLayout>

副MainAc.xml

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin">

    <View
        android:id="@+id/holder_view"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:transitionName="share"
        android:background="@color/colorPrimary"
        tools:targetApi="lollipop" />

    <Button
        android:id="@+id/fab_button"
        android:transitionName="fab"
        android:layout_width="56dp"
        android:layout_height="56dp"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:background="@drawable/ripple_round"
        android:elevation="5dp"
        android:layout_below="@+id/holder_view"
        android:layout_marginTop="-26dp"
        android:layout_alignParentRight="true"
        tools:targetApi="lollipop"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="@dimen/activity_horizontal_margin" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="10dp"
        android:layout_below="@id/holder_view">

        <Button
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:id="@+id/button"
            android:layout_below="@+id/button4"
            android:layout_marginTop="10dp" />

        <Button
            android:layout_width="match_parent"
            android:layout_height="60dp"
            android:layout_marginTop="10dp"
            android:id="@+id/button4"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />


    </RelativeLayout>

</RelativeLayout>
Tips:(补学)https://www.jianshu.com/p/692284dc3646  Transition类源码分析

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值