Android 缩放动画:平移并缩放到父级的父级中心

Android Scaling Animation: translate and scale to parent's parent's centre

我正在尝试构建一个应用程序,如果您点击个人资料图片,该图片将扩展到屏幕中央,应用程序的背景会变暗(类似于您点击whatsapp 上的头像)。单击变暗区域的任意位置将反转动画,个人资料图片将重置回原始位置。

我有以下 xml 布局:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:id="@+id/profilepage"
android:layout_height="wrap_content">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/brown_400"
    android:id="@+id/profileBox">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/profilepic"
        android:src="@drawable/ic_person_black_24dp"
        />

    <TextView
        android:layout_toRightOf="@+id/profilepic"
        android:layout_alignTop="@+id/profilepic"
        android:textSize="20sp"
        android:textColor="@color/white"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/profileName"
        tools:text="Johnny Appleseed"/>

</RelativeLayout>
 ...other layout stuff like buttons etc to be put here...
</LinearLayout>

我想点击上面的小人,它应该平移并缩放到屏幕中间。

我在这里 Translate and Scale animation in parallel 找到了一些符合我目的的代码:

private void moveViewToScreenCenter( final View view ){
    view.bringToFront(); //brings the view to the front
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics( dm );

    int originalPos[] = new int[2];
    view.getLocationOnScreen( originalPos );

    int xDelta = (dm.widthPixels - view.getMeasuredWidth() - originalPos[0])/2;
    int yDelta = (dm.heightPixels - view.getMeasuredHeight() - originalPos[1])/2;

    AnimationSet animSet = new AnimationSet(true);
    animSet.setFillAfter(true);
    animSet.setDuration(1000);
    animSet.setInterpolator(new BounceInterpolator());
    TranslateAnimation translate = new TranslateAnimation( 0, xDelta , 0, yDelta);
    animSet.addAnimation(translate);
    ScaleAnimation scale = new ScaleAnimation(1f, 2f, 1f, 2f, ScaleAnimation.RELATIVE_TO_PARENT, .5f, ScaleAnimation.RELATIVE_TO_PARENT, .5f);
    animSet.addAnimation(scale);
    view.startAnimation(animSet);
}

但是,代码只会相对于配置文件图片 ImageView 的父级进行缩放,在我的例子中是 RelativeLayout (@+id/profileBox) 而不是父级的父级 (@+id/profilepage) 这就是我想要它做的。

另外,代码:view.bringToFront(); 并没有将个人资料图片带到LinearLayout(profilepage)的前面进行缩放和平移,而是将它带到RelativeLayout(profileBox)的前面。

我怎样才能把它放在个人资料页面的前面并适当地缩放它,以便它在我的应用程序屏幕上居中?

我找到了第一个问题的答案:Android View Disappearing When Go Outside Of Parent

我需要为 profileBox 设置这个:

    android:clipChildren="false"
    android:clipToPadding="false"

这是个人资料页面:

android:clipChildren="false"

但我开始意识到这可能不是我想要做的事情的正确策略,因为我希望在转换/缩放个人资料图片时背景变暗,而个人资料图片后面的按钮不可以访问,因为 activity 处于暂停状态。

我上面的代码只会平移/缩放图像,而不会暂停下面的 activity,因此单击 'Log Out' 将注销用户。