Android中并沒(méi)有提供直接做3D翻轉(zhuǎn)的動(dòng)畫(huà),所以關(guān)于3D翻轉(zhuǎn)的動(dòng)畫(huà)效果需要我們自己實(shí)現(xiàn),那么我們首先來(lái)分析一下Animation 和 Transformation。

創(chuàng)新互聯(lián)建站專(zhuān)注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計(jì)、網(wǎng)站制作、于洪網(wǎng)絡(luò)推廣、小程序定制開(kāi)發(fā)、于洪網(wǎng)絡(luò)營(yíng)銷(xiāo)、于洪企業(yè)策劃、于洪品牌公關(guān)、搜索引擎seo、人物專(zhuān)訪、企業(yè)宣傳片、企業(yè)代運(yùn)營(yíng)等,從售前售中售后,我們都將竭誠(chéng)為您服務(wù),您的肯定,是我們最大的嘉獎(jiǎng);創(chuàng)新互聯(lián)建站為所有大學(xué)生創(chuàng)業(yè)者提供于洪建站搭建服務(wù),24小時(shí)服務(wù)熱線:18980820575,官方網(wǎng)址:www.chinadenli.net
Animation動(dòng)畫(huà)的主要接口,其中主要定義了動(dòng)畫(huà)的一些屬性比如開(kāi)始時(shí)間,持續(xù)時(shí)間,是否重復(fù)播放等等。而Transformation中則包含一個(gè)矩陣和alpha值,矩陣是用來(lái)做平移,旋轉(zhuǎn)和縮放動(dòng)畫(huà)的,而alpha值是用來(lái)做alpha動(dòng)畫(huà)的,要實(shí)現(xiàn)3D旋轉(zhuǎn)動(dòng)畫(huà)我們需要繼承自Animation類(lèi)來(lái)實(shí)現(xiàn),我們需要重載getTransformation和applyTransformation,在getTransformation中Animation會(huì)根據(jù)動(dòng)畫(huà)的屬性來(lái)產(chǎn)生一系列的差值點(diǎn),然后將這些差值點(diǎn)傳給applyTransformation,這個(gè)函數(shù)將根據(jù)這些點(diǎn)來(lái)生成不同的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 {
// 開(kāi)始角度
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)的開(kāi)始和結(jié)束角度,中心點(diǎn)、是否扭曲、和一個(gè)Camera,這里我們主要分析applyTransformation函數(shù),其中第一個(gè)參數(shù)就是通過(guò)getTransformation函數(shù)傳遞的差指點(diǎn),然后我們根據(jù)這個(gè)差值通過(guò)線性差值算法計(jì)算出一個(gè)中間角度degrees,Camera類(lèi)是用來(lái)實(shí)現(xiàn)繞Y軸旋轉(zhuǎn)后透視投影的,因此我們首先通過(guò)t.getMatrix()取得當(dāng)前的矩陣,然后通過(guò)camera.translate來(lái)對(duì)矩陣進(jìn)行平移變換操作,camera.rotateY進(jìn)行旋轉(zhuǎn)。這樣我們就可以很輕松的實(shí)現(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="下一個(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的代碼如下:
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)聽(tīng)
rotation.setAnimationListener(new DisplayNextView());
tv.startAnimation(rotation);
}
private final class DisplayNextView implements Animation.AnimationListener {
public void onAnimationStart(Animation animation) {
}
// 動(dòng)畫(huà)結(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());
// 開(kāi)始動(dòng)畫(huà)
tv.startAnimation(rotation);
tv.setText(String.valueOf(count++));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
} 以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
當(dāng)前文章:Android實(shí)現(xiàn)3D翻轉(zhuǎn)動(dòng)畫(huà)效果
網(wǎng)站地址:http://www.chinadenli.net/article4/jdheie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站建設(shè)、網(wǎng)站收錄、軟件開(kāi)發(fā)、網(wǎng)站維護(hù)、自適應(yīng)網(wǎng)站、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)