我有一个相对布局,其中包含一些 TextView 和一个(圆形) ImageView , ImageView 居中。
有一个从相对布局的中心发出的展开环的动画,当 ImageView 也居中时,它也从 ImageView 的中心发出。
但是现在需要将 ImageView 垂直向上移动一点,这样它就不再位于布局的中心,因此动画的原点也需要垂直移动相同的程度。
这是动画如何实现的草图,它只显示了一小部分代码来展示其实现的主要特征。
public class RippleBackground extends RelativeLayout {
private ArrayList<RippleView> rippleViewList=new ArrayList<RippleView>();
private Paint paint;
private AnimatorSet animatorSet;
private ArrayList<Animator> animatorList;
...
paint = new Paint();
rippleParams=new LayoutParams((int)(2*(rippleRadius+rippleStrokeWidth)),(int)(2*(rippleRadius+rippleStrokeWidth)));
rippleParams.addRule(CENTER_IN_PARENT, TRUE);
animatorSet = new AnimatorSet();
animatorSet.setInterpolator(new AccelerateDecelerateInterpolator());
animatorList=new ArrayList<Animator>();
addView(rippleView,rippleParams);
rippleViewList.add(rippleView);
...
animatorSet.playTogether(animatorList);
private class RippleView extends View {
@Override
protected void onDraw(Canvas canvas) {
int radius=(Math.min(getWidth(),getHeight()))/2;
canvas.drawCircle(radius,radius,radius-rippleStrokeWidth,paint);
}
}
这就是它目前的实现方式,我想知道我是否可以做一些事情,比如用其他规则替换 rippleParams.addRule(CENTER_IN_PARENT, TRUE)
,但我看不到任何其他规则那将是适用的。
我想知道是否可以使用规则来对齐动画 View ,使其不再以父 RelativeLayout 为中心,而是以 RelativeLayout 内的 imageView 为中心?
或者是否可以指定坐标,或者我可以将动画的原点向上移动以匹配 imageView 中心的其他方式?
最佳答案
像你一样,我看不出你如何用一个简单的规则来做到这一点,但你可以做的是包装你的 RippleView
在FrameLayout
与一些paddingBottom
在它上面匹配你的数量 ImageView
从中心向上移动了。
例如,如果您的 ImageView
现在是100dp
从你的中心向上 RelativeLayout
,您还可以设置 paddingBottom
至 100dp
关于新FrameLayout
包装你的 RippleView
为了更清楚地表达我的意思,这里是实现它的 XML 布局(我只使用 XML,因为我认为它更清楚发生了什么,我相信你能够轻松地将它翻译成以编程方式在你的代码):
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0f0"
android:layout_centerInParent="true"
android:paddingBottom="100dp"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:background="#f00"/>
</FrameLayout>
</RelativeLayout>
我已经为 FrameLayout
添加了背景颜色和 ImageView
(在你的情况下就是你的 RippleView
)所以你可以清楚地看到它们的界限 - 请注意 ImageView
完全包含在 FrameLayout
中:
另一种选择是将您的 ImageView
和你的 RippleView
在FrameLayout
设置为 wrap_content
对于宽度和高度,然后定位 FrameLayout
你会把 ImageView
放在哪里并设置 layout_gravity="center" on your
波纹 View , so that the
波纹 View ends up centred on the
ImageView by virtue of the fact the bounds of the
框架布局 match the bounds of the
ImageView` - 例如:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyActivity">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#0f0"
android:layout_centerVertical="true"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher"
android:background="#f00"/>
<View
android:id="@+id/rippleView"
android:layout_width="20dp"
android:layout_height="20dp"
android:background="#00f"
android:layout_gravity="center"
/>
</FrameLayout>
</RelativeLayout>
关于android - 是否可以将相对布局的子项对齐到另一个子项的顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31659914/