1

我尝试使用共享的 TextView 在两个活动之间进行转换,如Google I/O 2016 中所示,但它的工作方式很奇怪,因为文本似乎不适合某个容器,并且在动画播放时它的边缘会被切断,尽管没有什么会干扰它的缩放。我将它放在其他容器中并尝试了不同的组合,但结果完全相同。也就是说,当我从第二个活动回到第一个活动时,一切正常。用于缩放文本的类TextResize.java也已添加到项目中,并包含在两个活动之间的共享元素转换集中。

导航到第二个活动时如何修复不正确的文本缩放动画?

现在如何工作的视频(慢动作):链接

主要活动:

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    public static final String SHARED_TEXT_SIZE = "textSize";
    public static final String PADDING = "textPadding";
    public static final String TEXT_COLOR = "textColor";

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

        textView = findViewById(R.id.textView1);

        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent sharedIntent = new Intent(MainActivity.this, SharedActivity.class);

                sharedIntent.putExtra(SHARED_TEXT_SIZE, textView.getTextSize());
                sharedIntent.putExtra(PADDING,
                        new Rect(textView.getPaddingLeft(),
                                textView.getPaddingTop(),
                                textView.getPaddingRight(),
                                textView.getPaddingBottom()));
                sharedIntent.putExtra(TEXT_COLOR, textView.getCurrentTextColor());

                Pair[] pairs = new Pair[1];
                pairs[0] = new Pair<View, String>(textView, "textTransition");

                ActivityOptions activityOptions = ActivityOptions.makeSceneTransitionAnimation(MainActivity.this, pairs);

                startActivity(sharedIntent, activityOptions.toBundle());
            }
        });
    }
}

活动主.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView1"
        android:layout_alignParentTop="true"
        android:transitionName="textTransition"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginStart="20dp"
        android:text="Hello World!" />

</RelativeLayout>

第二个活动:

public class SharedActivity extends AppCompatActivity {

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

        final TextView textView = findViewById(R.id.textView1);

        setEnterSharedElementCallback(new SharedElementCallback() {

            private float targetTextSize;
            private ColorStateList targetTextColors;
            private Rect targetPadding;

            @Override
            public void onSharedElementStart(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots) {
                super.onSharedElementStart(sharedElementNames, sharedElements, sharedElementSnapshots);

                targetTextSize = textView.getTextSize();
                targetTextColors = textView.getTextColors();
                targetPadding = new Rect(textView.getPaddingLeft(),
                        textView.getPaddingTop(),
                        textView.getPaddingRight(),
                        textView.getPaddingBottom());

                textView.setTextColor(getIntent().getIntExtra(MainActivity.TEXT_COLOR, Color.BLACK));
                float textSize = getIntent().getFloatExtra(MainActivity.SHARED_TEXT_SIZE, targetTextSize);
                textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, textSize);
                Rect padding = getIntent().getParcelableExtra(MainActivity.PADDING);
                assert padding != null;
                textView.setPadding(padding.left, padding.top, padding.right, padding.bottom);
            }

            @Override
            public void onSharedElementEnd(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots) {
                super.onSharedElementEnd(sharedElementNames, sharedElements, sharedElementSnapshots);

                textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, targetTextSize);
                if (targetTextColors != null) {
                    textView.setTextColor(targetTextColors);
                }
                if (targetPadding != null) {
                    textView.setPadding(targetPadding.left, targetPadding.top,
                            targetPadding.right, targetPadding.bottom);
                }
            }
        });
    }
}

活动共享.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SharedActivity">

    <TextView
        android:id="@+id/textView1"
        android:transitionName="textTransition"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:layout_centerHorizontal="true"
        android:layout_centerInParent="true"
        android:textSize="36sp" />

</RelativeLayout>

样式.xml:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <item name="android:windowSharedElementEnterTransition">
            @transition/shared_main_detail
        </item>
    </style>

</resources>

shared_main_detail.xml:

<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
    <transitionSet>
        <targets>
            <target android:targetId="@id/textView1" />
        </targets>
        <transition class="com.example.sharedanimation.TextResize" />
        <changeBounds />
    </transitionSet>
    <recolor>
        <targets>
            <target android:targetId="@android:id/statusBarBackground" />
            <target android:targetId="@android:id/navigationBarBackground" />
        </targets>
    </recolor>
</transitionSet>
4

0 回答 0