如何制作跟踪选中的单选按钮的选择边框?

Tho*_*mas 6 android android-layout

我有一套RadioButton带有自定义风格的s.我想在当前选中的按钮周围显示边框.这应该很容易使用XML,但现在我想要边框动画.如果选中了一个新的单选按钮,边框应该用一个奇特的动画"飞"到它的新位置:

+------+
|* btn1| o btn2
+------+

    +------+
 o b|n1  * |tn2
    +------+

        +------+
 o btn1 |* btn2|
        +------+
Run Code Online (Sandbox Code Playgroud)

因此,我决定将边框设置为单独的View对象,因此我可以正确地设置它的动画.问题在于跟踪屏幕上相应单选按钮的位置.

我试图让它在没有动画的情况下首先工作.我当前的尝试看起来像这样(只显示相关属性):

    <RelativeLayout
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true">
        <RadioGroup
            android:id="@+id/radio_group">
            <RadioButton/>
            <RadioButton/>
            <RadioButton/>
        </RadioGroup>
        <View
            android:id="@+id/selection_border"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"/>
    </RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

OnCheckedChangeListenerRadioGroup,我周围移动选择边框通过设置它的利润率(我可以设置的位置,但是这是一个有点困难了RelativeLayout):

View radioButton = findViewById(checkedId);
View selectionBorder = findViewById(R.id.selection_border);
ViewGroup radioGroup = (ViewGroup)findViewById(R.id.radio_group);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(selectionBorder.getLayoutParams());
params.leftMargin = radioGroup.getLeft() + radioButton.getLeft();
params.topMargin = radioGroup.getTop() + radioButton.getTop();
selection.setLayoutParams(params);

selection.requestLayout();
Run Code Online (Sandbox Code Playgroud)

但是,在初始化时会发生故障.由于尚未进行布局,因此边框的位置设置不正确.似乎不可能立即强制重新布局,并且在布局完成后似乎也不可能获得事件.

所有这些麻烦让我相信必须有一种更清洁的方式来实现我想要的.有什么好主意吗?

And*_*der 1

我认为最好的方法是将复选框视为图像。两种类型已检查/未检查。然后你基本上就是在屏幕上移动图像。如果用户将鼠标放在您知道他们选择的图像区域上,则使用 TouchEvents 和图像处理所有事情。这很容易,因为现在您只需处理图像、画布和标准 x,y 坐标。