Android does not provide animations that directly do 3D flips, so we need to implement the animation effects of 3D flips ourselves. So let’s first analyze Animation and Transformation.
The main interface of Animation animation, which mainly defines some attributes of the animation, such as the start time, duration, whether to play repeatedly, etc. Transformation contains a matrix and alpha values. The matrix is used for translation, rotation and scaling animations, while alpha values are used for alpha animations. To implement 3D rotation animation, we need to inherit from the Animation class to implement it. We need to overload getTransformation and applyTransformation. In getTransformation, Animation will generate a series of differences based on the properties of the animation, and then pass these differences to applyTransformation. This function will generate different transformations based on these points. Below is
Specific implementation:
package ; import ; import ; import ; import ; public class Rotate3dAnimation extends Animation { // Start angle private final float mFromDegrees; // End angle private final float mToDegrees; // Center point private final float mCenterX; private final float mCenterY; private final float mDepthZ; // Is it necessary to distort private final boolean mReverse; // Camera private Camera mCamera; 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) { (width, height, parentWidth, parentHeight); mCamera = new Camera(); } // Generate Transformation @Override protected void applyTransformation(float interpolatedTime, Transformation t) { final float fromDegrees = mFromDegrees; // Generate intermediate angles float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime); final float centerX = mCenterX; final float centerY = mCenterY; final Camera camera = mCamera; final Matrix matrix = (); (); if (mReverse) { (0.0f, 0.0f, mDepthZ * interpolatedTime); } else { (0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime)); } (degrees); // Get the transformed matrix (matrix); (); (-centerX, -centerY); (centerX, centerY); } }
This includes the start and end angles of rotation, the center point, whether to twist, and a Camera. Here we mainly analyze the applyTransformation function. The first parameter is the difference pointing passed by the getTransformation function. Then we calculate an intermediate angle degrees based on this difference through the linear difference algorithm. The Camera class is used to realize perspective projection after rotation about the Y axis. Therefore, we first obtain the current matrix through (), and then perform translation transformation operations on the matrix to rotate. In this way, we can easily achieve 3D rotation effect.
Here is the layout file:
<LinearLayout xmlns:andro xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/main_screen_bg" android:gravity="center_horizontal" android:orientation="vertical" tools:context=".MainActivity" > <Button android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dip" android:drawableTop="@drawable/qiangpiao_dropdown" android:text="Next" /> <TextView android: android:layout_width="300dip" android:layout_height="300dip" android:layout_gravity="center" android:background="@drawable/call_show_frame_safe" android:gravity="center" android:textColor="#ffffff" android:textSize="15sp" /> </LinearLayout>
The code of MainActivity is as follows:
package ; import ; import ; import ; import ; import ; import ; import ; import ; import ; public class MainActivity extends Activity { private TextView tv; private Button btn; private int count = 1; @Override protected void onCreate(Bundle savedInstanceState) { (savedInstanceState); setContentView(.activity_main); tv = (TextView) findViewById(); ((count)); btn = (Button) findViewById(.next_btn); applyRotation(0, 90); (new () { @Override public void onClick(View v) { applyRotation(0, 90); } }); } private void applyRotation(float start, float end) { // Calculate the center point final float centerX = () / 2.0f; final float centerY = () / 2.0f; final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end, centerX, centerY, 310.0f, true); (500); (true); (new AccelerateInterpolator()); // Set up monitoring (new DisplayNextView()); (rotation); } private final class DisplayNextView implements { public void onAnimationStart(Animation animation) { } // End of the animation public void onAnimationEnd(Animation animation) { (new SwapViews()); } public void onAnimationRepeat(Animation animation) { } } private final class SwapViews implements Runnable { public void run() { final float centerX = () / 2.0f; final float centerY = () / 2.0f; Rotate3dAnimation rotation = null; (); rotation = new Rotate3dAnimation(90, 0, centerX, centerY, 310.0f, false); (500); (true); (new DecelerateInterpolator()); // Start animation (rotation); ((count++)); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(.activity_main, menu); return true; } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.