Ecco un esempio di Udinic. Era oggetto ListView espandersi con l'animazione e richiedono livello API solo 4+ In sostanza è necessaria una classe di animazione
/**
* This animation class is animating the expanding and reducing the size of a view.
* The animation toggles between the Expand and Reduce, depending on the current state of the view
* @author Udinic
*
*/
public class ExpandAnimation extends Animation {
private View mAnimatedView;
private LayoutParams mViewLayoutParams;
private int mMarginStart, mMarginEnd;
private boolean mIsVisibleAfter = false;
private boolean mWasEndedAlready = false;
/**
* Initialize the animation
* @param view The layout we want to animate
* @param duration The duration of the animation, in ms
*/
public ExpandAnimation(View view, int duration) {
setDuration(duration);
mAnimatedView = view;
mViewLayoutParams = (LayoutParams) view.getLayoutParams();
// decide to show or hide the view
mIsVisibleAfter = (view.getVisibility() == View.VISIBLE);
mMarginStart = mViewLayoutParams.bottomMargin;
mMarginEnd = (mMarginStart == 0 ? (0- view.getHeight()) : 0);
view.setVisibility(View.VISIBLE);
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
if (interpolatedTime < 1.0f) {
// Calculating the new bottom margin, and setting it
mViewLayoutParams.bottomMargin = mMarginStart
+ (int) ((mMarginEnd - mMarginStart) * interpolatedTime);
// Invalidating the layout, making us seeing the changes we made
mAnimatedView.requestLayout();
// Making sure we didn't run the ending before (it happens!)
} else if (!mWasEndedAlready) {
mViewLayoutParams.bottomMargin = mMarginEnd;
mAnimatedView.requestLayout();
if (mIsVisibleAfter) {
mAnimatedView.setVisibility(View.GONE);
}
mWasEndedAlready = true;
}
}
}
E utilizzare questo:
View toolbar = view.findViewById(R.id.toolbar);
// Creating the expand animation for the item
ExpandAnimation expandAni = new ExpandAnimation(toolbar, 500);
// Start the animation on the toolbar
toolbar.startAnimation(expandAni);
ExpandAnimationExample
fonte
2012-09-21 02:12:32
Per downnotor, si dovrebbe lasciare il motivo per cui si scende la mia risposta. So che la mia risposta può essere considerata solo come risposta al collegamento, ma cosa vuoi? Ti aspetti che copi tutto il file in quel repository qui. Quindi il collegamento Github è affidabile –
Nota, questo esempio è ancora buggato. https://github.com/Udinic/SmallExamples/issues/6 Mentre si scorre la vista elenco su e giù, lo stato di espansione/compressione si rovinerà. –
@CheokYanCheng qualche soluzione agli stati incasinati? Apparentemente, c'è un problema quando eseguiamo lo scrollup in modo che la vista scatti giù ora per fare in modo che cambiamo la visibilità in modo che l'animazione. entra in azione ma c'è uno sfarfallio, la vista si chiude e poi si apre di nuovo verso il basso :( – beerBear