// Somewhere in the code you get the height, so you can use that.
int getHeight() {
int height = 0;
Display display = getWindowManager().getDefaultDisplay();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
Point size = new Point();
display.getSize(size);
height = size.y;
} else {
height = display.getHeight(); // deprecated
}
return height;
}
// When you set alpha, use that method.
void setAlpha(View v, float alpha) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
v.setAlpha(alpha);
} else {
AlphaAnimation animation = new AlphaAnimation(alpha, alpha);
animation.setDuration(0);
animation.setFillAfter(true);
v.startAnimation(animation);
}
}
private void switchAccounts(final MaterialAccount newAccount) {
final ImageView floatingImage = new ImageView(this);
// si calcolano i rettangoli di inizio e fine
Rect startingRect = new Rect();
Rect finalRect = new Rect();
Point offsetHover = new Point();
// 64dp primary user image / 40dp other user image = 1.6 scale
float finalScale = 1.6f;
final int statusBarHeight;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
statusBarHeight = (int) (25 * density);
} else {
statusBarHeight = 0;
}
// si tiene traccia della foto cliccata
ImageView photoClicked;
if (newAccount.getAccountNumber() == MaterialAccount.SECOND_ACCOUNT) {
photoClicked = userSecondPhoto;
} else {
photoClicked = userThirdPhoto;
}
photoClicked.getGlobalVisibleRect(startingRect, offsetHover);
floatingImage.setImageDrawable(photoClicked.getDrawable());
// si aggiunge una view nell'esatta posizione dell'altra
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(photoClicked.getWidth(), photoClicked.getHeight());
params.setMargins(offsetHover.x, offsetHover.y - statusBarHeight, 0, 0);
drawer.addView(floatingImage, params);
// si setta la nuova foto di profilo sopra alla vecchia
photoClicked.setImageBitmap(currentAccount.getCircularPhoto());
// si setta la nuova immagine di background da visualizzare sotto la vecchia
usercoverSwitcher.setImageBitmap(newAccount.getBackground());
userphoto.getGlobalVisibleRect(finalRect);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
// Si calcola l'offset finale (LARGHEZZA DEL CONTAINER GRANDE - LARGHEZZA DEL CONTAINER PICCOLO / 2) e lo si applica
int offset = (((finalRect.bottom - finalRect.top) - (startingRect.bottom - finalRect.top)) / 2);
finalRect.offset(offset, offset - statusBarHeight);
startingRect.offset(0, -statusBarHeight);
// si animano le viste
AnimatorSet set = new AnimatorSet();
set
// si ingrandisce l'immagine e la si sposta a sinistra.
.play(ObjectAnimator.ofFloat(floatingImage, View.X, startingRect.left, finalRect.left))
.with(ObjectAnimator.ofFloat(floatingImage, View.Y, startingRect.top, finalRect.top))
.with(ObjectAnimator.ofFloat(floatingImage, View.SCALE_X, 1f, finalScale))
.with(ObjectAnimator.ofFloat(floatingImage, View.SCALE_Y, 1f, finalScale))
.with(ObjectAnimator.ofFloat(userphoto, View.ALPHA, 1f, 0f))
.with(ObjectAnimator.ofFloat(usercover, View.ALPHA, 1f, 0f))
.with(ObjectAnimator.ofFloat(photoClicked, View.SCALE_X, 0f, 1f))
.with(ObjectAnimator.ofFloat(photoClicked, View.SCALE_Y, 0f, 1f));
set.setDuration(USER_CHANGE_TRANSITION);
set.setInterpolator(new DecelerateInterpolator());
set.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
onSwitchAcountAnimationEnd(newAccount, floatingImage);
}
@Override
public void onAnimationCancel(Animator animation) {
// se si annulla l'animazione si conclude e basta.
onSwitchAcountAnimationEnd(newAccount, floatingImage);
}
});
set.start();
} else onSwitchAcountAnimationEnd(newAccount, floatingImage);
}
void onSwitchAcountAnimationEnd(MaterialAccount newAccount, ImageView floatingImage) {
// si carica la nuova immagine
setAlpha(userphoto, 1);
setFirstAccountPhoto(newAccount.getCircularPhoto());
// si cancella l'imageview per l'effetto
drawer.removeView(floatingImage);
// si cambiano i dati utente
setUserEmail(newAccount.getSubTitle());
setUsername(newAccount.getTitle());
// si cambia l'immagine soprastante
setDrawerBackground(newAccount.getBackground());
// si fa tornare il contenuto della cover visibile (ma l'utente non nota nulla)
setAlpha(usercover, 1);
// switch numbers
currentAccount.setAccountNumber(newAccount.getAccountNumber());
newAccount.setAccountNumber(MaterialAccount.FIRST_ACCOUNT);
// change pointer to newAccount
currentAccount = newAccount;
// si chiude il drawer
if (!deviceSupportMultiPane())
layout.closeDrawer(drawer);
}