以编程方式将transitionName添加到工具栏标题(Android)

时间:2016-02-10 19:20:22

标签: java android

是否可以通过编程方式将xml属性transitionName设置为Toolbar标题,然后使用ActivityOptionsCompat将其用于动画?

这就是我宣布Toolbar

的方式
        Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);
        myToolbar.setTitle("title");

这是我的xml声明:

<android.support.v7.widget.Toolbar
        android:id="@+id/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/primary_color"
        android:elevation="4dp"
        android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
        android:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>

为了添加动画,我必须使用以下代码:

ActivityOptionsCompat options =
ActivityOptionsCompat.makeSceneTransitionAnimation(activity,
    albumCoverImageView,   // The view which starts the transition (Should be myToolbar title)
    transitionName    // The transitionName of the view
    );
ActivityCompat.startActivity(activity, intent, options.toBundle());

如何将ToolbarTextView?)标题传递给makeSceneTransitionAnimation函数?

2 个答案:

答案 0 :(得分:9)

您可以通过以下代码获取TextView标题的Toolbar实例并设置转换:

private Toolbar mToolbar;
private TextView mTextViewToolbarTitle;

@Override
protected void onCreated(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.your_layout);
    mToolbar = (Toolbar) findViewById(R.id.your_toolbar_id);
    setSupportActionBar(mToolbar);

    mTextViewToolbarTitle = getTextViewTitle(mToolbar);
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mTextViewTitle.setTransitionName(getString(R.string.your_transition_name));
    }
}

在您的活动中复制此方法:

public static TextView getTextViewTitle(Toolbar toolbar){
    TextView textViewTitle = null;
    for(int i = 0; i<toolbar.getChildCount(); i++) {
        View view = toolbar.getChildAt(i);
        if(view instanceof TextView) {
            textViewTitle = (TextView) view;
            break;
        }
    }
    return textViewTitle;
}

然后通过以下方式开始您的新活动:

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    startActivity(intent, 
        ActivityOptionsCompat.makeSceneTransitionAnimation(this, 
                                            mTextViewToolbarTitle, 
                                            getString(R.string.your_transition_name))
                                               .toBundle());
} else {
    startActivity(intent,
        ActivityOptionsCompat.makeSceneTransitionAnimation(this).toBundle());
}

答案 1 :(得分:4)

是的,这是可能的:

扩展ViewCompat

类可以调用setTransitionName(String name)

例如:

myToolbar.setTransitionName("ToolbarTransition");

更新似乎您特意尝试分享标题,以便我会执行以下操作:

MainActivity的示例xml:

<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context="com.divshark.toolbaranimation.MainActivity"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light">

        <android.support.v7.widget.AppCompatTextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textColor="@android:color/white"
            android:text="Activity Title" />
    </android.support.v7.widget.Toolbar>

    <android.support.v7.widget.AppCompatButton
        android:id="@+id/btnNext"
        android:text="Next"
        android:layout_centerInParent="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>

然后在您的Activity引用中查看该视图:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
        final AppCompatTextView mTextView = (AppCompatTextView) mToolbar.findViewById(R.id.tv_title);

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mTextView.setTransitionName("SimpleTransition");
        }

        AppCompatButton mBtnNext = (AppCompatButton) findViewById(R.id.btnNext);
        mBtnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent secondActivity = new Intent(MainActivity.this, SecondActivity.class);
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(MainActivity.this, mTextView, mTextView.getTransitionName());
                    startActivity(secondActivity, options.toBundle());
                } else {
                    startActivity(secondActivity);
                }
            }
        });
    }
}

然后在你的SecondActivity中:

public class SecondActivity extends AppCompatActivity {

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

        AppCompatTextView mTextView = (AppCompatTextView) findViewById(R.id.tv_title);
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mTextView.setTransitionName("SimpleTransition");
        }
    }
}

使用这个xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="18sp"
        android:layout_centerInParent="true"
        android:textColor="@android:color/black"
        android:text="Activity Title" />
</RelativeLayout>

正如您所见,您可以使用ViewCompat视图以编程方式调用setTransitionName(String name)

祝你好运,快乐的编码!