我正在尝试使用 Android 中的 SharedElements 和 Content Transitions。我试图结合内容转换和共享元素并创建一个特定的动画,但是我遇到了一个问题。在共享元素 (ImageView) 上的过渡结束后,我正在使用内容过渡来使用幻灯片过渡将文本视图设置为动画位置。然而,当 textViews 从底部滑动到它们在图像视图顶部的位置时,文本会位于 ImageView(共享元素)下方,然后仅出现在 ImageView 顶部。有没有办法做到这一点?
我在这里分享一个简短的代码和一些解释这一点的截图:
Activity_A.java
package com.example.mehulp.sharedelementsviewheirarchy;
import android.app.Activity;
import android.app.ActivityOptions;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class Activity_a extends Activity {
ImageView imageView_a;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_a);
imageView_a = (ImageView) findViewById(R.id.imgView_a);
imageView_a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
imageClickListener();
}
});
}
private void imageClickListener() {
Intent intent = new Intent(Activity_a.this, Activity_b.class);
startActivity(intent, ActivityOptions.makeSceneTransitionAnimation(this, imageView_a, imageView_a.getTransitionName()).toBundle());
}
}
活动_a.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<ImageView
android:id="@+id/imgView_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:transitionName="fImage"
android:padding="30dp"
android:src="@drawable/female_result"/>
<LinearLayout
android:id="@+id/ll_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignBottom="@id/imgView_a">
<TextView
android:id="@+id/myName_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MyName: Lorem Ipsum Ipsum"
android:padding="10dp"/>
<TextView
android:id="@+id/myAge_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="My Age: abc years, xyz months, pqr days"
android:padding="10dp"/>
</LinearLayout>
</RelativeLayout>
Activity_B.java
package com.example.mehulp.sharedelementsviewheirarchy;
import java.util.List;
public class Activity_b extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
setEnterSharedElementCallback(new SharedElementCallback() {
@Override
public void onSharedElementStart(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots) {
TransitionSet transitionSet = new TransitionSet();
transitionSet.setDuration(3000);
Slide slideFromBottom = new Slide(Gravity.BOTTOM);
slideFromBottom.addTarget(R.id.myAge_b);
slideFromBottom.addTarget(R.id.myName_b);
transitionSet.addTransition(slideFromBottom);
getWindow().setEnterTransition(transitionSet);
super.onSharedElementStart(sharedElementNames, sharedElements, sharedElementSnapshots);
}
@Override
public void onSharedElementEnd(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots) {
super.onSharedElementEnd(sharedElementNames, sharedElements, sharedElementSnapshots);
}
});
}}
活动_b.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="@+id/imgView_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:transitionName="fImage"
android:padding="30dp"
android:scaleType="fitXY"
android:src="@drawable/female_result"/>
<LinearLayout
android:id="@+id/ll_b"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/myName_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MyName: Lorem Ipsum Ipsum"
android:padding="10dp"/>
<TextView
android:id="@+id/myAge_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="My Age: abc years, xyz months, pqr days"
android:padding="10dp"/>
</LinearLayout>