How to make a selection border that tracks the set radio button?

I have a custom style RadioButton suite. I want to display a frame around the button that is currently being tested. This should be fairly straightforward using XML, but now I want the border to be animated. If a new switch is set, the border should "fly" to a new location using fantastic animation:

 +------+ |* btn1| o btn2 +------+ +------+ ob|n1 * |tn2 +------+ +------+ o btn1 |* btn2| +------+ 

Because of this, I decided to turn the border into a separate View object so that I can animate it properly. The problem is tracking the location of the corresponding switch on the screen.

I am trying to make it work without animation. My current attempt looks something like this (only with the corresponding attributes):

  <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> 

In OnCheckedChangeListener RadioGroup , I move the selection frame by setting its field (I could set the position, but a little more complicated with 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(); 

However, a problem occurs during initialization. Since the layout has not yet been completed, the position of the border is set incorrectly. It seems that it is impossible to immediately turn off the relay, and it is not possible to receive the event after the layout has been completed.

All these troubles make me think that there should be a cleaner way to achieve what I want. Any bright ideas?

+6
android android-layout
source share
2 answers

The best way, I think, is to check the treatment as an image. Two types are checked / not checked. Then you basically move the image around the screen. Handle all TouchEvents things and images if custom mice go down with the area emitting the image you know you have selected. This is easy because now you are dealing with images, hash coordinates and stardard x, y.

+1
source share

To solve your initialization problem (in the past I had problems with myself): If I need to know when the layout of the view is complete, I give it runnable in Activity.onCreate (), then it will call runnable from View.onSizeChanged (). I came to use this template quite a bit, and I found it very reliable. You just need to remember only the runnable call from onSizeChanged () if the new width and height are greater than 0 and if they are not equal to the old width and height.

So, in your activity:

 public void onCreate() { ... yourView.setLayoutRunnable(new Runnable() { @Override public void run() { //do post-layout stuff... } }); ... } 

and in your opinion:

 public Runnable layoutRunner; public void setLayoutRunnable(Runnable runner) { layoutRunner = runner; } @Override public void onSizeChanged(int w, int h, int oldw, int oldh) { if (w > 0 && h > 0 && (w != oldw || h != oldh)) { if (layoutRunner != null) layoutRunner.run(); } } 

If you have several views that everyone needs to have in order for their layouts to be completed before you do your things, you should save an account when you receive notifications so you know when they will be done.

And in relation to your topic as a whole, I implemented something similar to your switch concept in the game that I wrote, but I decided to redefine View from scratch to do this. I had all the options for dialing in one view, so I replaced the RadioGroup view, not the RadioButton view. Each option mattered and degree. I always used text, but images would work just as easily. Then I just tracked the taps and dragged relative to the extents of the options and moved the โ€œselectorโ€ to the time handler (a la animation in javascript).

+1
source share

All Articles