【问题标题】:I have a fragment in NavigationDrawer Activity with a custom toolbar.How to animate image from fragment to toolbar in Activity我在导航抽屉活动中有一个带有自定义工具栏的片段。如何在活动中将图像从片段动画到工具栏
【发布时间】:2018-03-28 09:53:33
【问题描述】:

我在 Activity 中成功地制作了动画(在同一个 Activity 布局中设置和目的地)。但我希望它从子片段过渡到其父 Activity 工具栏中的布局。下面给出我用来制作动画的代码。

工具栏 xml 布局

<android.support.v7.widget.Toolbar android:id="@+id/products_toolbar"
android:layout_width="wrap_content"
android:layout_height="?attr/actionBarSize"
android:background="@android:color/transparent"
app:layout_collapseMode="pin"
app:titleTextColor="@android:color/white"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
    <RelativeLayout
        android:id="@+id/cartRelativeLayout"
        android:layout_width="0dp"
        android:layout_weight=".15"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:clickable="true"
        android:paddingRight="3dip">

        <ImageButton
            android:id="@+id/cartButtonIV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@android:color/transparent"
            android:clickable="false"
            android:src="@drawable/cart" />

        <TextView
            android:id="@+id/textNotify"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignRight="@id/cartButtonIV"
            android:layout_alignTop="@id/cartButtonIV"
            android:layout_marginRight="5dip"
            android:layout_marginTop="5dip"
            android:background="@drawable/notification_circle"
            android:gravity="center"
            android:minHeight="20dip"
            android:minWidth="20dip"
            android:padding="3dip"
            android:text="0"
            android:textColor="#424242"
            android:textSize="12sp" />
    </RelativeLayout>

Fragment 中启动动画的方法

private void makeFlyAnimation(ImageView targetView) {

    toolbar = getActivity().findViewById(R.id.toolbar);

    RelativeLayout destView = (RelativeLayout)toolbar.findViewById(R.id.cartRelativeLayout);

    ImageButton dest = (ImageButton)toolbar.findViewById(R.id.cartButtonIV);

    new CircleAnimationUtil().attachActivity(getActivity()).setTargetView(targetView).setMoveDuration(1000).setDestView(dest).setAnimationListener(new Animator.AnimatorListener() {
        @Override
        public void onAnimationStart(Animator animation) {

        }

        @Override
        public void onAnimationEnd(Animator animation) {
            addItemToCart();

        }

        @Override
        public void onAnimationCancel(Animator animation) {

        }

        @Override
        public void onAnimationRepeat(Animator animation) {

        }
    }).startAnimation();


}

private void addItemToCart() {

    ((NavHomeActivity)getActivity()).addItemToCart2("2");

}

在以下 CircleAnimationUtil.class 方法中,即使我从 makeFlyAnimation 函数将活动传递给它,我也会将视图设为 null。

public CircleAnimationUtil setTargetView(View view) {

    Log.e("view here",""+view);
    mTarget = view;
    setOriginRect(mTarget.getWidth(), mTarget.getHeight());
    return this;
}

任何人请给我一个解决方案。

【问题讨论】:

标签: android animation fragment android-toolbar


【解决方案1】:

使用 Target Fragment 来实现这一点,如

 public class First extends Fragment{

    public void startSecondFragmentForResult(){
         Fragment second = Second.newInstance()
         second.setTargetFragment(this);
         fm.beginTranstaction().replace(second).commit();
    }

      @Override
      public void onActivityResult(int reqCode, int resCode, Intent data){
         // do something on result
      }

    }

第二个片段

public class Second extends Fragment{

  public void postResultToCaller(){
     Intent intent = new Intent();
     getTargetFragment().onActivityResult(getTargetRequestCode(), resultCode, intent);
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-09
    • 2022-10-17
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多