3D Cube Transition in Android

how to implement cube Transition effect animation on ViewPager in android?

Check out here

Snapshot:

Sample Image

There you can see the library and related source codes.

MainActivity.java:

package com.ToxicBakery.viewpager.transforms.example;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.PageTransformer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.ToxicBakery.viewpager.transforms.AccordionTransformer;
import com.ToxicBakery.viewpager.transforms.BackgroundToForegroundTransformer;
import com.ToxicBakery.viewpager.transforms.CubeInTransformer;
import com.ToxicBakery.viewpager.transforms.CubeOutTransformer;
import com.ToxicBakery.viewpager.transforms.DefaultTransformer;
import com.ToxicBakery.viewpager.transforms.DepthPageTransformer;
import com.ToxicBakery.viewpager.transforms.FlipHorizontalTransformer;
import com.ToxicBakery.viewpager.transforms.FlipVerticalTransformer;
import com.ToxicBakery.viewpager.transforms.ForegroundToBackgroundTransformer;
import com.ToxicBakery.viewpager.transforms.RotateDownTransformer;
import com.ToxicBakery.viewpager.transforms.RotateUpTransformer;
import com.ToxicBakery.viewpager.transforms.StackTransformer;
import com.ToxicBakery.viewpager.transforms.TabletTransformer;
import com.ToxicBakery.viewpager.transforms.ZoomInTransformer;
import com.ToxicBakery.viewpager.transforms.ZoomOutSlideTransformer;
import com.ToxicBakery.viewpager.transforms.ZoomOutTranformer;
import com.ToxicBakery.viewpager.transforms.example.R;

public class MainActivity extends Activity implements OnNavigationListener {

private static final String KEY_SELECTED_PAGE = "KEY_SELECTED_PAGE";
private static final String KEY_SELECTED_CLASS = "KEY_SELECTED_CLASS";
private static final ArrayList<TransformerItem> TRANSFORM_CLASSES;

static {
TRANSFORM_CLASSES = new ArrayList<TransformerItem>();
TRANSFORM_CLASSES.add(new TransformerItem(DefaultTransformer.class));
TRANSFORM_CLASSES.add(new TransformerItem(AccordionTransformer.class));
TRANSFORM_CLASSES.add(new TransformerItem(BackgroundToForegroundTransformer.class));
TRANSFORM_CLASSES.add(new TransformerItem(CubeInTransformer.class));
TRANSFORM_CLASSES.add(new TransformerItem(CubeOutTransformer.class));
TRANSFORM_CLASSES.add(new TransformerItem(DepthPageTransformer.class));
TRANSFORM_CLASSES.add(new TransformerItem(FlipHorizontalTransformer.class));
TRANSFORM_CLASSES.add(new TransformerItem(FlipVerticalTransformer.class));
TRANSFORM_CLASSES.add(new TransformerItem(ForegroundToBackgroundTransformer.class));
TRANSFORM_CLASSES.add(new TransformerItem(RotateDownTransformer.class));
TRANSFORM_CLASSES.add(new TransformerItem(RotateUpTransformer.class));
TRANSFORM_CLASSES.add(new TransformerItem(StackTransformer.class));
TRANSFORM_CLASSES.add(new TransformerItem(TabletTransformer.class));
TRANSFORM_CLASSES.add(new TransformerItem(ZoomInTransformer.class));
TRANSFORM_CLASSES.add(new TransformerItem(ZoomOutSlideTransformer.class));
TRANSFORM_CLASSES.add(new TransformerItem(ZoomOutTranformer.class));
}

private int mSelectedItem;
private ViewPager mPager;
private PageAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

int selectedPage = 0;
if (savedInstanceState != null) {
mSelectedItem = savedInstanceState.getInt(KEY_SELECTED_CLASS);
selectedPage = savedInstanceState.getInt(KEY_SELECTED_PAGE);
}

final ArrayAdapter<TransformerItem> actionBarAdapter = new ArrayAdapter<TransformerItem>(
getApplicationContext(), android.R.layout.simple_list_item_1, android.R.id.text1, TRANSFORM_CLASSES);

final ActionBar actionBar = getActionBar();
actionBar.setListNavigationCallbacks(actionBarAdapter, this);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);

//noinspection ResourceType
actionBar.setDisplayOptions(actionBar.getDisplayOptions() ^ ActionBar.DISPLAY_SHOW_TITLE);

setContentView(R.layout.activity_main);

mAdapter = new PageAdapter(getFragmentManager());

mPager = (ViewPager) findViewById(R.id.container);
mPager.setAdapter(mAdapter);
mPager.setCurrentItem(selectedPage);

actionBar.setSelectedNavigationItem(mSelectedItem);
}

@Override
public boolean onNavigationItemSelected(int position, long itemId) {
mSelectedItem = position;
try {
mPager.setPageTransformer(true, TRANSFORM_CLASSES.get(position).clazz.newInstance());
} catch (Exception e) {
throw new RuntimeException(e);
}

return true;
}

protected void onSaveInstanceState(Bundle outState) {
outState.putInt(KEY_SELECTED_CLASS, mSelectedItem);
outState.putInt(KEY_SELECTED_PAGE, mPager.getCurrentItem());
}

public static class PlaceholderFragment extends Fragment {

private static final String EXTRA_POSITION = "EXTRA_POSITION";
private static final int[] COLORS = new int[] { 0xFF33B5E5, 0xFFAA66CC, 0xFF99CC00, 0xFFFFBB33, 0xFFFF4444 };

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final int position = getArguments().getInt(EXTRA_POSITION);
final TextView textViewPosition = (TextView) inflater.inflate(R.layout.fragment_main, container, false);
textViewPosition.setText(Integer.toString(position));
textViewPosition.setBackgroundColor(COLORS[position - 1]);

return textViewPosition;
}

}

private static final class PageAdapter extends FragmentStatePagerAdapter {

public PageAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}

@Override
public Fragment getItem(int position) {
final Bundle bundle = new Bundle();
bundle.putInt(PlaceholderFragment.EXTRA_POSITION, position + 1);

final PlaceholderFragment fragment = new PlaceholderFragment();
fragment.setArguments(bundle);

return fragment;
}

@Override
public int getCount() {
return 5;
}

}

private static final class TransformerItem {

final String title;
final Class<? extends PageTransformer> clazz;

public TransformerItem(Class<? extends PageTransformer> clazz) {
this.clazz = clazz;
title = clazz.getSimpleName();
}

@Override
public String toString() {
return title;
}

}

}

3D cube animation between Activities in Android

Import this Project and mark as Library in project property and add it to your project

Create your activity like this:

package com.example.testcube;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.jfeinstein.jazzyviewpager.JazzyViewPager;
import com.jfeinstein.jazzyviewpager.JazzyViewPager.TransitionEffect;

public class MainActivity extends Activity {

private JazzyViewPager vpage;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Set window fullscreen and remove title bar, and force landscape orientation
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
setupJazziness(TransitionEffect.CubeOut);
}

private void setupJazziness(TransitionEffect effect) {
vpage = (JazzyViewPager) findViewById(R.id.jazzy_pager);
vpage.setTransitionEffect(effect);
vpage.setAdapter(new MainAdapter());
vpage.setPageMargin(0);
}

private class MainAdapter extends PagerAdapter {
@Override
public Object instantiateItem(ViewGroup container, final int position) {
TextView text = new TextView(MainActivity.this);
text.setGravity(Gravity.CENTER);
text.setTextSize(30);
text.setTextColor(Color.WHITE);
text.setText("Page " + position);
text.setPadding(30, 30, 30, 30);
int bg = Color.rgb((int) Math.floor(Math.random()*128)+64,
(int) Math.floor(Math.random()*128)+64,
(int) Math.floor(Math.random()*128)+64);
text.setBackgroundColor(bg);
container.addView(text, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
vpage.setObjectForPosition(text, position);
return text;
}
@Override
public void destroyItem(ViewGroup container, int position, Object obj) {
container.removeView((View) obj);
}
@Override
public int getCount() {
return 10;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
}

}

and in your activity XML should be

<com.jfeinstein.jazzyviewpager.JazzyViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/jazzy_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Image

3d Cube Animation in Android

Import this project and mark as Library in project property and add it to your project.

Create your activity like this:

package com.example.testcube;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.jfeinstein.jazzyviewpager.JazzyViewPager;
import com.jfeinstein.jazzyviewpager.JazzyViewPager.TransitionEffect;

public class MainActivity extends Activity {

private JazzyViewPager vpage;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Set window fullscreen and remove title bar, and force landscape orientation
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
setupJazziness(TransitionEffect.CubeOut);
}

private void setupJazziness(TransitionEffect effect) {
vpage = (JazzyViewPager) findViewById(R.id.jazzy_pager);
vpage.setTransitionEffect(effect);
vpage.setAdapter(new MainAdapter());
vpage.setPageMargin(0);
}

private class MainAdapter extends PagerAdapter {
@Override
public Object instantiateItem(ViewGroup container, final int position) {
TextView text = new TextView(MainActivity.this);
text.setGravity(Gravity.CENTER);
text.setTextSize(30);
text.setTextColor(Color.WHITE);
text.setText("Page " + position);
text.setPadding(30, 30, 30, 30);
int bg = Color.rgb((int) Math.floor(Math.random()*128)+64,
(int) Math.floor(Math.random()*128)+64,
(int) Math.floor(Math.random()*128)+64);
text.setBackgroundColor(bg);
container.addView(text, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
vpage.setObjectForPosition(text, position);
return text;
}
@Override
public void destroyItem(ViewGroup container, int position, Object obj) {
container.removeView((View) obj);
}
@Override
public int getCount() {
return 10;
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
}

}

And in your activity XML should be

<com.jfeinstein.jazzyviewpager.JazzyViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/jazzy_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />

Sample Image

How to apply 3d transition between two activities in android?

I had used 3D Cubic Transition between activities.Credit goes to Robert Heim who is developer of this program.

Below is snippet

Activity1.java

package org.vipul;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Activity1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity1);

Button switchActivityBtn = (Button) findViewById(R.id.bSwitchActivity);
switchActivityBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animatedStartActivity();
}
});
}

@Override
protected void onResume() {
// animateIn this activity
ActivitySwitcher.animationIn(findViewById(R.id.container),
getWindowManager());
super.onResume();
}

private void animatedStartActivity() {
// we only animateOut this activity here.
// The new activity will animateIn from its onResume() - be sure to
// implement it.
final Intent intent = new Intent(getApplicationContext(),
Activity2.class);
// disable default animation for new intent
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
ActivitySwitcher.animationOut(findViewById(R.id.container),
getWindowManager(),
new ActivitySwitcher.AnimationFinishedListener() {
@Override
public void onAnimationFinished() {
startActivity(intent);
}
});
}
}

Activity2.java

package org.vipul;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Activity2 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);

Button switchActivityBtn = (Button) findViewById(R.id.bSwitchActivity);
switchActivityBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animatedStartActivity();
}
});
}

@Override
protected void onResume() {
// animateIn this activity
ActivitySwitcher.animationIn(findViewById(R.id.container),
getWindowManager());
super.onResume();
}

private void animatedStartActivity() {
// we only animateOut this activity here.
// The new activity will animateIn from its onResume() - be sure to
// implement it.
final Intent intent = new Intent(getApplicationContext(),
Activity1.class);
// disable default animation for new intent
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
ActivitySwitcher.animationOut(findViewById(R.id.container),
getWindowManager(),
new ActivitySwitcher.AnimationFinishedListener() {
@Override
public void onAnimationFinished() {
startActivity(intent);
}
});
}
}

ActivitySwitcher.java

package org.vipul;

import android.view.Display;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;

/**
* This ActivitySwitcher uses a 3D rotation to animate an activity during its
* start or finish.
*
* see: http://blog.robert-heim.de/karriere/android-startactivity-rotate-3d-
* animation-activityswitcher/
*
* @author Robert Heim
*
*/
public class ActivitySwitcher {

private final static int DURATION = 300;
private final static float DEPTH = 400.0f;

/* ----------------------------------------------- */

public interface AnimationFinishedListener {
/**
* Called when the animation is finished.
*/
public void onAnimationFinished();
}

/* ----------------------------------------------- */

public static void animationIn(View container, WindowManager windowManager) {
animationIn(container, windowManager, null);
}

public static void animationIn(View container, WindowManager windowManager,
AnimationFinishedListener listener) {
apply3DRotation(90, 0, false, container, windowManager, listener);
}

public static void animationOut(View container, WindowManager windowManager) {
animationOut(container, windowManager, null);
}

public static void animationOut(View container,
WindowManager windowManager, AnimationFinishedListener listener) {
apply3DRotation(0, -90, true, container, windowManager, listener);
}

/* ----------------------------------------------- */

private static void apply3DRotation(float fromDegree, float toDegree,
boolean reverse, View container, WindowManager windowManager,
final AnimationFinishedListener listener) {
Display display = windowManager.getDefaultDisplay();
final float centerX = display.getWidth() / 2.0f;
final float centerY = display.getHeight() / 2.0f;

final Rotate3dAnimation a = new Rotate3dAnimation(fromDegree, toDegree,
centerX, centerY, DEPTH, reverse);
a.reset();
a.setDuration(DURATION);
a.setFillAfter(true);
a.setInterpolator(new AccelerateInterpolator());
if (listener != null) {
a.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}

@Override
public void onAnimationRepeat(Animation animation) {
}

@Override
public void onAnimationEnd(Animation animation) {
listener.onAnimationFinished();
}
});
}
container.clearAnimation();
container.startAnimation(a);
}
}

Rotate3dAnimation.java

package org.vipul;

import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;

/**
* An animation that rotates the view on the Y axis between two specified
* angles. This animation also adds a translation on the Z axis (depth) to
* improve the effect.
*/
public class Rotate3dAnimation extends Animation {
private final float mFromDegrees;
private final float mToDegrees;
private final float mCenterX;
private final float mCenterY;
private final float mDepthZ;
private final boolean mReverse;
private Camera mCamera;

/**
* Creates a new 3D rotation on the Y axis. The rotation is defined by its
* start angle and its end angle. Both angles are in degrees. The rotation
* is performed around a center point on the 2D space, definied by a pair of
* X and Y coordinates, called centerX and centerY. When the animation
* starts, a translation on the Z axis (depth) is performed. The length of
* the translation can be specified, as well as whether the translation
* should be reversed in time.
*
* @param fromDegrees
* the start angle of the 3D rotation
* @param toDegrees
* the end angle of the 3D rotation
* @param centerX
* the X center of the 3D rotation
* @param centerY
* the Y center of the 3D rotation
* @param reverse
* true if the translation should be reversed, false otherwise
*/
public Rotate3dAnimation(float fromDegrees, float toDegrees, float centerX,
float centerY, float depthZ, boolean reverse) {
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
mCenterX = centerX;
mCenterY = centerY;
mDepthZ = depthZ;
mReverse = reverse;
}

@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
mCamera = new Camera();
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees
+ ((mToDegrees - fromDegrees) * interpolatedTime);

final float centerX = mCenterX;
final float centerY = mCenterY;
final Camera camera = mCamera;

final Matrix matrix = t.getMatrix();

camera.save();
if (mReverse) {
camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
} else {
camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
}
camera.rotateY(degrees);
camera.getMatrix(matrix);
camera.restore();

matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}

Activity1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#003300"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />

<Button
android:id="@+id/bSwitchActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="switch activity" />

</LinearLayout>

Activity2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
android:id="@+id/bSwitchActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="back" />

</LinearLayout>

Manifest entries

        <activity
android:name=".Activity1"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Activity2"
android:label="Activity 2" >
</activity>

Android 3d cube animation

Probably you should take a look at samples that come with default android installation (>>>Views >>> Animation >>> 3D transition) and searching for some others on google



Related Topics



Leave a reply



Submit