È possibile implementare questa animazione utilizzando animatore Android di serie con l'animazione libro come un set di più ImageViews
- ciascuno per la rappresentazione della pagina: Copertina, BackCover, FirstPage. E dopo che l'animazione è finita, avvia l'attività o mostra il frammento.
Animare con:
- x e la scala y,
ObjectAnimator.ofFloat(mCover, "scaleY", ...)
- xey trasformano,
ObjectAnimator.ofFloat(mCover, "x", ...)
- y-rotazione
ObjectAnimator.ofFloat(mCover, "rotationY", ...)
Vedi il mio esempio
(Sure , questo esempio richiede alcune ottimizzazioni/correzioni, ma per te nderstanding basterebbe):
public class MainActivity extends AppCompatActivity {
private ImageView mPage1;
private ImageView mCover;
private ImageView mCoverFullScreen;
private AnimatorSet mAnimIncrease;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCoverFullScreen = (ImageView) findViewById(R.id.cover_full_screen);
mPage1 = (ImageView) findViewById(R.id.page1);
mCover = (ImageView) findViewById(R.id.cover);
mCover.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mAnimIncrease.start();
}
});
mCoverFullScreen.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (mCover.getMeasuredHeight() <= 0 || mCoverFullScreen.getMeasuredHeight() <= 0) {
return;
}
if (Build.VERSION.SDK_INT >= 16) {
mCoverFullScreen.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
mCoverFullScreen.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
initAnimator(1000);
}
});
}
private void initAnimator(long animationDuration) {
mAnimIncrease = new AnimatorSet();
TimeInterpolator interpolator = new LinearInterpolator();
float deltaX = mCover.getMeasuredWidth()/2f;
float deltaY = mCoverFullScreen.getY() - mCover.getY();
float scale = mCoverFullScreen.getMeasuredHeight()/(float) mCover.getMeasuredHeight();
float scaleMiddle = (scale + 1)/2f;
float xStart = mCover.getX();
float xEnd = xStart + deltaX;
float xMiddle = xStart + deltaX * interpolator.getInterpolation(0.5f);
float xScaledEnd = xStart + deltaX * scale;
float yStart = mCover.getY();
float yEnd = yStart + deltaY;
float yMiddle = yStart + deltaY * interpolator.getInterpolation(0.5f);
AnimatorSet coverFrontSet = new AnimatorSet();
coverFrontSet.setDuration(animationDuration/2);
coverFrontSet.playTogether(
ObjectAnimator.ofFloat(mCover, "rotationY", 0f, -90f),
ObjectAnimator.ofFloat(mCover, "pivotX", 0f),
ObjectAnimator.ofFloat(mCover, "x", xStart, xMiddle),
ObjectAnimator.ofFloat(mCover, "y", yStart, yMiddle),
ObjectAnimator.ofFloat(mCover, "scaleY", 1, scaleMiddle),
ObjectAnimator.ofFloat(mCover, "scaleX", 1, scaleMiddle)
);
coverFrontSet.addListener(new AnimatorListenerStub() {
@Override
public void onAnimationStart(Animator animation) {
super.onAnimationStart(animation);
mCover.setImageResource(R.drawable.cover);
}
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
mCover.setImageResource(R.drawable.cover_back);
}
});
AnimatorSet coverBackSet = new AnimatorSet();
coverBackSet.setDuration(animationDuration/2);
coverBackSet.playTogether(
ObjectAnimator.ofFloat(mCover, "rotationY", -90f, -180f),
ObjectAnimator.ofFloat(mCover, "pivotX", 0f),
ObjectAnimator.ofFloat(mCover, "x", xMiddle, xEnd),
ObjectAnimator.ofFloat(mCover, "y", yMiddle, yEnd),
ObjectAnimator.ofFloat(mCover, "scaleY", scaleMiddle, scale),
ObjectAnimator.ofFloat(mCover, "scaleX", scaleMiddle, scale)
);
AnimatorSet coverSet = new AnimatorSet();
coverSet.play(coverBackSet).after(coverFrontSet);
AnimatorSet page1Set = new AnimatorSet();
page1Set.setDuration(animationDuration);
page1Set.playTogether(
ObjectAnimator.ofFloat(mPage1, "scaleX", 1, scale),
ObjectAnimator.ofFloat(mPage1, "scaleY", 1, scale),
ObjectAnimator.ofFloat(mPage1, "x", xStart, xScaledEnd)
);
mAnimIncrease.play(coverSet).with(page1Set);
mAnimIncrease.setInterpolator(interpolator);
}
}
e Layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
>
<ImageView
android:id="@+id/page1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/page1"
/>
<ImageView
android:id="@+id/cover"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/cover"
/>
<ImageView
android:id="@+id/cover_full_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
Grazie Alexander, è un buon modo, ma ho bisogno che funzioni su tablet, ma l'apertura della cover va fuori dallo schermo (completamente invisibile) per pochi secondi, in ogni modo per questo? – Haris
@Haris È come funziona l'animazione di rotazione standardY. Posso suggerirti 2 soluzioni: 1. (Più semplice) Dividi la tua animazione su due parti: [1] (aprendo e aumentando fino a metà) e [2] (semplicemente aumentando). 2. Implementare un'animazione personalizzata per applicare l'animazione di rotazione personalizzataY. Purtroppo non ho idea di come affrontare la seconda soluzione :). Spero che questo ti possa aiutare. – Oleksandr
Grazie, sì, sto cercando di implementare la prima soluzione – Haris