How to enable and disable viewing in android

I am trying to pretend (Linear view with some buttons in R.id.playerControl ) to insert and delete depending on the context of other events in the activity.

For this purpose, I have a selectMediaItem method that should show or hide the view when the user selects or deselects the item accordingly.

I am new to animation in android, and I am having problems with its operation for two reasons:

  • The view remains on the screen outside the animation time, so when he has finished crawling, he comes back - when he is asked to move, he jumps out to go back.

  • Permanent black space appears on the screen when the view disappears. I would like the view to fill the space when it is visible, and GONE when not. To do this, I would like the layout to also change with the animation, so that it seems to push away other things.

My code is:

 protected void selectMediaItem( ItemHandle item ) { if (item != null) { if (toPlay == null) { View playerControl = findViewById(R.id.playerControl); Animation slideInAdmination = AnimationUtils.loadAnimation(this, R.anim.slide_in); playerControl.startAnimation(slideInAdmination); } } else { if (toPlay != null) { View playerControl = findViewById(R.id.playerControl); Animation slideInAdmination = AnimationUtils.loadAnimation(this, R.anim.slide_out); playerControl.startAnimation(slideInAdmination); } } toPlay = item; } 

slide_in.xml

  <translate android:duration="1000" android:fromYDelta="100%p" android:toYDelta="0" /> </set> 

Is there an easy way to move the view in place and bring it back out?

+7
source share
2 answers

I would suggest you use Property Animations . Your sample code will be similar.

 mSlidInAnimator = ObjectAnimator.ofFloat(mSlidingView, "translationY", 0); mSlidInAnimator.setDuration(200); mSlidInAnimator.start(); mSlidOutAnimator = ObjectAnimator.ofFloat(mSlidingView, "translationY", newPosOfView); mSlidOutAnimator.setDuration(200); mSlidOutAnimator.start(); 

"translationY" means up / down animation. Use "translationX" for the left-left animation.

Here newPosOfView will be the position relative to your default position. For example, if you want to move your 50dp down, it will be 50dp in pixels. In slideInAnimator pos is 0 because you want to go to the orignal view position.

Read the documentation carefully, its very useful.

+4
source

You need to add:

 slideInAdmination.setFillerAfter(true) 

before calling startAnimation() . This causes the view to remain in a new position after the animation.

0
source

All Articles