【问题标题】:Android Scaling Animation: translate and scale to parent's parent's centreAndroid Scaling Animation:翻译并缩放到父母的父母中心
【发布时间】:2015-08-08 17:36:24
【问题描述】:

我正在尝试构建一个应用程序,如果您单击个人资料图片,该图片将扩展到屏幕中心,应用程序的背景会变暗(类似于您单击个人资料时的体验)图片在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 android-layout android-animation


    【解决方案1】:

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

    我需要为 profileBox 设置这个:

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

    这是个人资料页面:

    android:clipChildren="false"
    

    但我开始意识到这可能不是我想要做的正确策略,因为我希望在个人资料图片正在翻译/缩放时背景变暗并且个人资料图片后面的按钮无法访问因为活动处于暂停状态。

    我上面的代码只会翻译/缩放图像而不暂停下面的活动,因此点击“注销”将注销用户。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多