Android實現(xiàn)3D翻轉(zhuǎn)動畫效果
Android中并沒有提供直接做3D翻轉(zhuǎn)的動畫,所以關(guān)于3D翻轉(zhuǎn)的動畫效果需要我們自己實現(xiàn),那么我們首先來分析一下Animation 和 Transformation。
Animation動畫的主要接口,其中主要定義了動畫的一些屬性比如開始時間,持續(xù)時間,是否重復播放等等。而Transformation中則包含一個矩陣和alpha值,矩陣是用來做平移,旋轉(zhuǎn)和縮放動畫的,而alpha值是用來做alpha動畫的,要實現(xiàn)3D旋轉(zhuǎn)動畫我們需要繼承自Animation類來實現(xiàn),我們需要重載getTransformation和applyTransformation,在getTransformation中Animation會根據(jù)動畫的屬性來產(chǎn)生一系列的差值點,然后將這些差值點傳給applyTransformation,這個函數(shù)將根據(jù)這些點來生成不同的Transformation。下面是
具體實現(xiàn):
package com.example.textviewtest; import android.graphics.Camera; import android.graphics.Matrix; import android.view.animation.Animation; import android.view.animation.Transformation; public class Rotate3dAnimation extends Animation { // 開始角度 private final float mFromDegrees; // 結(jié)束角度 private final float mToDegrees; // 中心點 private final float mCenterX; private final float mCenterY; private final float mDepthZ; // 是否需要扭曲 private final boolean mReverse; // 攝像頭 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) { super.initialize(width, height, parentWidth, parentHeight); mCamera = new Camera(); } // 生成Transformation @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); } }
其中包括了旋轉(zhuǎn)的開始和結(jié)束角度,中心點、是否扭曲、和一個Camera,這里我們主要分析applyTransformation函數(shù),其中第一個參數(shù)就是通過getTransformation函數(shù)傳遞的差指點,然后我們根據(jù)這個差值通過線性差值算法計算出一個中間角度degrees,Camera類是用來實現(xiàn)繞Y軸旋轉(zhuǎn)后透視投影的,因此我們首先通過t.getMatrix()取得當前的矩陣,然后通過camera.translate來對矩陣進行平移變換操作,camera.rotateY進行旋轉(zhuǎn)。這樣我們就可以很輕松的實現(xiàn)3D旋轉(zhuǎn)效果了。
下面是布局文件main.xml:
<LinearLayout 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" android:background="@drawable/main_screen_bg" android:gravity="center_horizontal" android:orientation="vertical" tools:context=".MainActivity" > <Button android:id="@+id/next_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="20dip" android:drawableTop="@drawable/qiangpiao_dropdown" android:text="下一個" /> <TextView android:id="@+id/tv" 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>
MainActivity的代碼如下:
package com.example.textviewtest; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.view.animation.AccelerateInterpolator; import android.view.animation.Animation; import android.view.animation.DecelerateInterpolator; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { private TextView tv; private Button btn; private int count = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) findViewById(R.id.tv); tv.setText(String.valueOf(count)); btn = (Button) findViewById(R.id.next_btn); applyRotation(0, 90); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { applyRotation(0, 90); } }); } private void applyRotation(float start, float end) { // 計算中心點 final float centerX = tv.getWidth() / 2.0f; final float centerY = tv.getHeight() / 2.0f; final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end, centerX, centerY, 310.0f, true); rotation.setDuration(500); rotation.setFillAfter(true); rotation.setInterpolator(new AccelerateInterpolator()); // 設(shè)置監(jiān)聽 rotation.setAnimationListener(new DisplayNextView()); tv.startAnimation(rotation); } private final class DisplayNextView implements Animation.AnimationListener { public void onAnimationStart(Animation animation) { } // 動畫結(jié)束 public void onAnimationEnd(Animation animation) { tv.post(new SwapViews()); } public void onAnimationRepeat(Animation animation) { } } private final class SwapViews implements Runnable { public void run() { final float centerX = tv.getWidth() / 2.0f; final float centerY = tv.getHeight() / 2.0f; Rotate3dAnimation rotation = null; tv.requestFocus(); rotation = new Rotate3dAnimation(90, 0, centerX, centerY, 310.0f, false); rotation.setDuration(500); rotation.setFillAfter(true); rotation.setInterpolator(new DecelerateInterpolator()); // 開始動畫 tv.startAnimation(rotation); tv.setText(String.valueOf(count++)); } } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android實現(xiàn)圖片反轉(zhuǎn)、翻轉(zhuǎn)、旋轉(zhuǎn)、放大和縮小
- Android動畫之3D翻轉(zhuǎn)效果實現(xiàn)函數(shù)分析
- Android圖片翻轉(zhuǎn)動畫簡易實現(xiàn)代碼
- Android實現(xiàn)Flip翻轉(zhuǎn)動畫效果
- Android實現(xiàn)文字翻轉(zhuǎn)動畫的效果
- android使用FlipAnimation實現(xiàn)3D垂直翻轉(zhuǎn)動畫
- Android利用Camera實現(xiàn)中軸3D卡牌翻轉(zhuǎn)效果
- Android實現(xiàn)卡片翻轉(zhuǎn)動畫
- android camera yuv幀水平翻轉(zhuǎn)實例
- android實現(xiàn)撲克卡片翻轉(zhuǎn)
相關(guān)文章
Android開發(fā)實現(xiàn)判斷通知欄是否打開及前往設(shè)置頁面的方法
這篇文章主要介紹了Android開發(fā)實現(xiàn)判斷通知欄是否打開及前往設(shè)置頁面的方法,涉及Android通知欄的打開、判斷、設(shè)置等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01Android攔截并獲取WebView內(nèi)部POST請求參數(shù)的實現(xiàn)方法
這篇文章主要介紹了Android攔截并獲取WebView內(nèi)部POST請求參數(shù) 的實現(xiàn)方法,本文通過兩種方案給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-04-04Android入門教程之ListView的應(yīng)用示例
這篇文章主要介紹了Android入門教程之ListView的應(yīng)用,結(jié)合簡單實例形式分析了Android中l(wèi)istview的簡單創(chuàng)建與使用步驟,需要的朋友可以參考下2016-10-10Android UI設(shè)計與開發(fā)之ViewPager介紹和簡單實現(xiàn)引導界面
這篇文章主要為大家詳細介紹了Android UI設(shè)計與開發(fā)之ViewPager介紹和簡單實現(xiàn)引導界面,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08Android應(yīng)用程序中讀寫txt文本文件的基本方法講解
這篇文章主要介紹了Android應(yīng)用程序中讀寫txt文本文件的基本方法講解,基本上依靠context.openFileInput()和context.openFileOutput()兩個方法為主,需要的朋友可以參考下2016-04-04