【发布时间】: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