gpt4 book ai didi

Android Lollipop 在 Activity Enter 中转换特定 View

转载 作者:行者123 更新时间:2023-11-30 02:09:54 25 4
gpt4 key购买 nike

我正在尝试在我的应用程序中实现新的 Transitions API,但我遇到了一个特定问题。

在我的 Activity 中,我想对特定 View 进行进入幻灯片转换。这是我的布局

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.github.ksoichiro.android.observablescrollview.ObservableScrollView
android:id="@+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<FrameLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="180dp">

<ImageView
android:id="@+id/ivRestaurantBg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/background_poly" />

<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="70dp">

<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/cvRestaurantLogo"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dp"
android:transitionName="sharedRestaurantLogo"
android:src="@drawable/placeholder" />

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toEndOf="@+id/cvRestaurantLogo"
android:layout_toRightOf="@+id/cvRestaurantLogo"
android:gravity="center_vertical"
android:orientation="vertical">

<TextView
android:id="@+id/tvRestaurantName"
style="@style/HeaderText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:textSize="20sp" />

<TextView
android:id="@+id/tvRestaurantAddress"
style="@style/HeaderText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp" />

<TextView
android:id="@+id/tvRestaurantContactNo"
style="@style/HeaderText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp" />

</LinearLayout>

</RelativeLayout>

</FrameLayout>

<View
android:id="@+id/anchor"
android:layout_width="match_parent"
android:layout_height="180dp"
android:minHeight="180dp" />

<LinearLayout
android:id="@+id/body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/anchor"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="@android:color/white"
android:paddingTop="@dimen/activity_vertical_margin">

<include android:id="@+id/mapSegment" layout="@layout/card_map_lite" /> <!-- Layout 1 to transition -->

<include android:id="@+id/customerSegment" layout="@layout/card_customer_details" /> <!-- Layout 2 to transition -->

<include android:id="@+id/ordersSegment" layout="@layout/card_dish_details" /> <!-- Layout 3 to transition -->

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/accept_order"
android:id="@+id/btnAcceptOrder"
android:layout_marginTop="10dp"
android:background="@drawable/green_button"
android:minWidth="120dp"
android:minHeight="50dp"
android:textAllCaps="false"
android:textColor="@android:color/white"
android:layout_gravity="center_horizontal" />
</LinearLayout>
</RelativeLayout>

</com.github.ksoichiro.android.observablescrollview.ObservableScrollView>

<include layout="@layout/gradient_header" />

<include layout="@layout/toolbar" />

</FrameLayout>

在我的 Activity 中我这样做

@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_ACTIVITY_TRANSITIONS);
requestWindowFeature(Window.FEATURE_CONTENT_TRANSITIONS);

Transition transition = TransitionInflater.from(this)
.inflateTransition(R.transition.shared_element_transition);

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_order_details);
mMapSegment = findViewById(R.id.mapSegment);
mCustomerSegment = findViewById(R.id.customerSegment);
mOrdersSegment = findViewById(R.id.orderSegment);

TransitionSet set = new TransitionSet();

Slide mapSlide = new Slide();
mapSlide.setDuration(2000);
mapSlide.setSlideEdge(Gravity.BOTTOM);
mapSlide.addTarget(mMapSegment);

//others ignored

set.addTransition(mapSlide);

getWindow().setEnterTransition(set);
getWindow().setSharedElementEnterTransition(transition);
}

但是这似乎不起作用。共享元素转换按预期工作,但不是输入转换。如果我删除 slide.addTarget(mMapSegment) 行,我可以看到整个 Activity 的 View 都变成了动画,所以我很确定它与向 Transition 添加特定目标有关。难道我做错了什么?任何帮助将不胜感激。

最佳答案

我知道不久前有人问过这个问题,但我又遇到了同样的问题,所以我想我会分享对我有用的方法。我将试图滑动的 View 包装在新的 LinearLayout (match_parent/match_parent) 中。我不知道为什么这个(通常无用的)viewGroup 允许 addTarget 开始工作,但这已经帮了我好几次了。

为了将问题缩小到 addTarget,我通常用 addTarget(View.class) 替换我的目标。如果您看到整个屏幕转换,那么这是一个 addTarget 问题。否则其他东西可能没有正确连接。

我有时必须做的另一件事是将 android:transitionGroup="false"添加到我添加的新 LinearLayout 的父 viewGroup。为什么有时需要而不是其他,我不完全理解。

希望这对那些从事 Activity 转换的人有所帮助。

关于Android Lollipop 在 Activity Enter 中转换特定 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30252948/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com