android - 是否可以将相对布局的子项对齐到另一个子项的顶部?

标签 android android-layout android-animation

我有一个相对布局,其中包含一些 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 中心的其他方式?

最佳答案

像你一样,我看不出你如何用一个简单的规则来做到这一点,但你可以做的是包装你的 RippleViewFrameLayout与一些paddingBottom在它上面匹配你的数量 ImageView从中心向上移动了。

例如,如果您的 ImageView现在是100dp从你的中心向上 RelativeLayout ,您还可以设置 paddingBottom100dp关于新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 中:

Screen Shot

另一种选择是将您的 ImageView和你的 RippleViewFrameLayout设置为 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>

Screen Shot 2

关于android - 是否可以将相对布局的子项对齐到另一个子项的顶部?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31659914/

相关文章:

android - Android 中的自定义按钮动画

android - ListView 项中的 MapView

堆外的android内存管理

android - 我可以为我的所有应用程序使用一个 Google Maps API key ,而无需注册它们的包名称吗?

android - 如果所有 subview 都不能水平放置,如何将布局方向更改为垂直

android - 如何为新设计支持库的 FloatingActionButton 制作动画

android - 任务 ':transformDexArchiveWithExternalLibsDexMergerForDebug' 执行失败

android-layout - 如何在单个应用程序中使用应用程序小部件创建 Android 应用程序

Android Spinner 选项值就像选择标签一样

android - 如何在Android中使用xml中的object animator做旋转动画时设置一个轴心点?