共享元素从活动转换为另一个活动中的片段

时间:2015-09-22 10:32:17

标签: android android-layout android-fragments android-activity android-transitions

我正在开展一个项目,我必须在其中进行共享转换。 我想在我的splash Activity [First Activity]上执行ImageView的转换到Login Activity [Second Activity],它在Fragment中有ImageView。我的第一个活动中的图像和我的第二个活动的片段中的图像是相同的并且共享相同的转换名称。我无法执行转换。

我的启动活动代码是这个

public class Splash extends Activity implements View.OnClickListener{

private ImageView imageView;
private String SharedElementname;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //getWindow().setExitTransition(R.transition.shared_element_transition_a);
    if (Build.VERSION.SDK_INT >= 21) {
        getWindow().setSharedElementExitTransition(TransitionInflater.from(this).inflateTransition(R.transition.shared_element_transition_a));
    }
    setContentView(R.layout.activity_splash);
    imageView = (ImageView)findViewById(R.id.splashimage);
    SharedElementname = getString(R.string.sharedname);
    imageView.setOnClickListener(this);

}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_splash, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressLint("NewApi")
@Override
public void onClick(View v) {
    ActivityOptionsCompat compat = ActivityOptionsCompat.makeSceneTransitionAnimation(this,v,SharedElementname);
    Intent intent = new Intent(this, LoginActivity.class);
    ActivityCompat.startActivity(this, intent, compat.toBundle());
}

我的登录活动代码     @覆盖 protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);
if(BuildConfig.MINT_API_ENABLED) {
    Mint.initAndStartSession(LoginActivity.this, BuildConfig.MINT_API_KEY);
}
if (Build.VERSION.SDK_INT >= 21) {
    getWindow().setSharedElementEnterTransition(TransitionInflater.from(this).inflateTransition(R.transition.shared_element_transition_a));
}
setContentView(R.layout.activity_login);
} 
Some More Codes….
*****************************************************************************

我在Login Activty中的片段代码

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loginTab = new LoginTab();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    setSharedElementReturnTransition(TransitionInflater.from(
            getActivity()).inflateTransition(R.transition.shared_element_transition_a));
    setExitTransition(TransitionInflater.from(
            getActivity()).inflateTransition(android.R.transition.fade));

    loginTab.setSharedElementEnterTransition(TransitionInflater.from(
            getActivity()).inflateTransition(R.transition.shared_element_transition_a));
    loginTab.setEnterTransition(TransitionInflater.from(
            getActivity()).inflateTransition(android.R.transition.fade));
}

}

Some More Codes….
*************************************************************************************

我的Splash Activity XML文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background_new"
android:orientation="vertical"
android:weightSum="1">
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:id="@+id/SplashLayout"
    >

    <ImageView
        android:id="@+id/splashimage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:src="@drawable/eventifyd_logo"
        android:transitionName="@string/sharedname"
        />



</RelativeLayout>
</LinearLayout>

我的转换XML文件[Shared_element_transition]

<?xml version="1.0" encoding="utf-8"?>
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android" android:duration="5000">
<changeTransform />
<changeBounds />
<!—
I also tied ChangeImageTranform But Nothing Happened
-->
</transitionSet>

我的片段XML文件

<LinearLayout
 android:layout_width="match_parent"
 android:layout_marginTop="20dp"
 android:layout_weight="1"
 android:layout_height="match_parent"
 android:orientation="vertical"
 >



    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical"
        >

        <ImageView
            android:id="@+id/profile_pic_login"
            android:layout_above="@+id/login_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:src="@drawable/eventifyd_logo"
            android:transitionName="@string/sharedname"
            />

        <TextView
            android:layout_marginTop="30dp"
            android:id="@+id/login_text"
            android:layout_alignParentBottom="true"

            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/login_text"
            style="@style/thin.white.large"/>

    </RelativeLayout>

</LinearLayout> 

1 个答案:

答案 0 :(得分:0)

我无法按照我的方式去做。我将片段更改为活动,它作为一个魅力。我认为从活动过渡到另一个活动的片段是不可能的。无论如何,我的这种方法有效。