Android動(dòng)畫之3D翻轉(zhuǎn)效果實(shí)現(xiàn)函數(shù)分析
Android中的翻轉(zhuǎn)動(dòng)畫效果的實(shí)現(xiàn),首先看一下運(yùn)行效果如上圖所示.
Android中并沒有提供直接做3D翻轉(zhuǎn)的動(dòng)畫,所以關(guān)于3D翻轉(zhuǎn)的動(dòng)畫效果需要我們自己實(shí)現(xiàn),那么我們首先來分析一下Animation 和 Transformation。
Animation動(dòng)畫的主要接口,其中主要定義了動(dòng)畫的一些屬性比如開始時(shí)間,持續(xù)時(shí)間,是否重復(fù)播放等等。而Transformation中則包含一個(gè)矩陣和alpha值,矩陣是用來做平移,旋轉(zhuǎn)和縮放動(dòng)畫的,而alpha值是用來做alpha動(dòng)畫的,要實(shí)現(xiàn)3D旋轉(zhuǎn)動(dòng)畫我們需要繼承自Animation類來實(shí)現(xiàn),我們需要重載getTransformation和applyTransformation,在getTransformation中Animation會(huì)根據(jù)動(dòng)畫的屬性來產(chǎn)生一系列的差值點(diǎn),然后將這些差值點(diǎn)傳給applyTransformation,這個(gè)函數(shù)將根據(jù)這些點(diǎn)來生成不同的Transformation。下面是具體實(shí)現(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;
// 中心點(diǎn)
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é)束角度,中心點(diǎn)、是否扭曲、和一個(gè)Camera,這里我們主要分析applyTransformation函數(shù),其中第一個(gè)參數(shù)就是通過getTransformation函數(shù)傳遞的差指點(diǎn),然后我們根據(jù)這個(gè)差值通過線性差值算法計(jì)算出一個(gè)中間角度degrees,Camera類是用來實(shí)現(xiàn)繞Y軸旋轉(zhuǎn)后透視投影的,因此我們首先通過t.getMatrix()取得當(dāng)前的矩陣,然后通過camera.translate來對(duì)矩陣進(jìn)行平移變換操作,camera.rotateY進(jìn)行旋轉(zhuǎn)。這樣我們就可以很輕松的實(shí)現(xiàn)3D旋轉(zhuǎn)效果了。
下面是布局文件main.xml:
View Code
<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="下一個(gè)" />
<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的代碼如下:
View Code
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) {
// 計(jì)算中心點(diǎn)
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) {
}
// 動(dòng)畫結(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());
// 開始動(dòng)畫
tv.startAnimation(rotation);
tv.setText(String.valueOf(count++));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
看懂了嗎?呵呵。
- Android實(shí)現(xiàn)圖片反轉(zhuǎn)、翻轉(zhuǎn)、旋轉(zhuǎn)、放大和縮小
- Android圖片翻轉(zhuǎn)動(dòng)畫簡易實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)Flip翻轉(zhuǎn)動(dòng)畫效果
- Android實(shí)現(xiàn)文字翻轉(zhuǎn)動(dòng)畫的效果
- android使用FlipAnimation實(shí)現(xiàn)3D垂直翻轉(zhuǎn)動(dòng)畫
- Android實(shí)現(xiàn)3D翻轉(zhuǎn)動(dòng)畫效果
- Android利用Camera實(shí)現(xiàn)中軸3D卡牌翻轉(zhuǎn)效果
- Android實(shí)現(xiàn)卡片翻轉(zhuǎn)動(dòng)畫
- android camera yuv幀水平翻轉(zhuǎn)實(shí)例
- android實(shí)現(xiàn)撲克卡片翻轉(zhuǎn)
相關(guān)文章
Android轉(zhuǎn)場動(dòng)畫深入分析探究
對(duì)于一個(gè)動(dòng)畫而言,它是由多個(gè)分鏡頭組成的,而轉(zhuǎn)場就是分鏡之間銜接方式。轉(zhuǎn)場的主要目的,就是為了讓鏡頭與鏡頭之間過渡的更加自然,讓動(dòng)畫更加連貫,例如兩個(gè)頁面切換之間的銜接動(dòng)畫2022-10-10Android 判斷是開發(fā)debug模式,還是發(fā)布release模式的方法
下面小編就為大家?guī)硪黄狝ndroid 判斷是開發(fā)debug模式,還是發(fā)布release模式的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-12-12基于VSTS的Xamarin.Android持續(xù)集成步驟詳解
這篇文章主要介紹了基于VSTS的Xamarin.Android持續(xù)集成步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04基于android studio的layout的xml文件的創(chuàng)建方式
這篇文章主要介紹了基于android studio的layout的xml文件的創(chuàng)建方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-03-03Android studio 如何刪除項(xiàng)目 module
本篇文章主要介紹了Android studio 如何刪除項(xiàng)目module的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來看下吧2017-05-05