Android編程實(shí)現(xiàn)3D滑動(dòng)旋轉(zhuǎn)效果的方法
本文實(shí)例講述了Android編程實(shí)現(xiàn)3D滑動(dòng)旋轉(zhuǎn)效果的方法。分享給大家供大家參考,具體如下:
這里我們通過(guò)代碼實(shí)現(xiàn)一些滑動(dòng)翻頁(yè)的動(dòng)畫(huà)效果。
Animation實(shí)現(xiàn)動(dòng)畫(huà)有兩個(gè)方式:幀動(dòng)畫(huà)(frame-by-frame animation)和補(bǔ)間動(dòng)畫(huà)(tweened animation)
本示例通過(guò)繼承Animation自定義Rotate3D,實(shí)現(xiàn)3D翻頁(yè)效果。效果圖如下:

1、Rotate3D(Animation)
首先,自定義Animation的3D動(dòng)畫(huà)類Rotate3D
public class Rotate3D extends Animation {
private float fromDegree; // 旋轉(zhuǎn)起始角度
private float toDegree; // 旋轉(zhuǎn)終止角度
private float mCenterX; // 旋轉(zhuǎn)中心x
private float mCenterY; // 旋轉(zhuǎn)中心y
private Camera mCamera;
public Rotate3D(float fromDegree, float toDegree, float centerX, float centerY) {
this.fromDegree = fromDegree;
this.toDegree = toDegree;
this.mCenterX = centerX;
this.mCenterY = centerY;
}
@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 FromDegree = fromDegree;
float degrees = FromDegree + (toDegree - fromDegree) * interpolatedTime; // 旋轉(zhuǎn)角度(angle)
final float centerX = mCenterX;
final float centerY = mCenterY;
final Matrix matrix = t.getMatrix();
if (degrees <= -76.0f) {
degrees = -90.0f;
mCamera.save();
mCamera.rotateY(degrees); // 旋轉(zhuǎn)
mCamera.getMatrix(matrix);
mCamera.restore();
} else if (degrees >= 76.0f) {
degrees = 90.0f;
mCamera.save();
mCamera.rotateY(degrees);
mCamera.getMatrix(matrix);
mCamera.restore();
} else {
mCamera.save();
mCamera.translate(0, 0, centerX); // 位移x
mCamera.rotateY(degrees);
mCamera.translate(0, 0, -centerX);
mCamera.getMatrix(matrix);
mCamera.restore();
}
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}
然后,實(shí)例化Rotate3D的旋轉(zhuǎn)方向
public void initAnimation() {
// 獲取旋轉(zhuǎn)中心
DisplayMetrics dm = new DisplayMetrics();
dm = getResources().getDisplayMetrics();
mCenterX = dm.widthPixels / 2;
mCenterY = dm.heightPixels / 2;
// 定義旋轉(zhuǎn)方向
int duration = 1000;
lQuest1Animation = new Rotate3D(0, -90, mCenterX, mCenterY); // 下一頁(yè)的【question1】旋轉(zhuǎn)方向(從0度轉(zhuǎn)到-90,參考系為水平方向?yàn)?度)
lQuest1Animation.setFillAfter(true);
lQuest1Animation.setDuration(duration);
lQuest2Animation = new Rotate3D(90, 0, mCenterX, mCenterY); // 下一頁(yè)的【question2】旋轉(zhuǎn)方向(從90度轉(zhuǎn)到0,參考系為水平方向?yàn)?度)(起始第一題)
lQuest2Animation.setFillAfter(true);
lQuest2Animation.setDuration(duration);
rQuest1Animation = new Rotate3D(0, 90, mCenterX, mCenterY); // 上一頁(yè)的【question1】旋轉(zhuǎn)方向(從0度轉(zhuǎn)到90,參考系為水平方向?yàn)?度)
rQuest1Animation.setFillAfter(true);
rQuest1Animation.setDuration(duration);
rQuest2Animation = new Rotate3D(-90, 0, mCenterX, mCenterY); // 上一頁(yè)的【question2】旋轉(zhuǎn)方向(從-90度轉(zhuǎn)到0,參考系為水平方向?yàn)?度)
rQuest2Animation.setFillAfter(true);
rQuest2Animation.setDuration(duration);
}
2、Activity
首先,定義兩個(gè)布局文件,用于旋轉(zhuǎn)的畫(huà)面切換
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout_main" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> ... </LinearLayout>
next.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/layout_next" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical"> ... </LinearLayout>
限于篇幅,完整布局文件請(qǐng)?jiān)斠?jiàn)源碼 ^_^
然后,初始化兩個(gè)旋轉(zhuǎn)的布局文件資源
private void initMain(){
setContentView(R.layout.main);
layoutmain = (LinearLayout)findViewById(R.id.layout_main);
btn_MainLast = (Button)findViewById(R.id.main_last);
btn_MainNext = (Button)findViewById(R.id.main_next);
btn_MainLast.setOnClickListener(listener);
btn_MainNext.setOnClickListener(listener);
}
private void initNext(){
setContentView(R.layout.next);
layoutnext = (LinearLayout)findViewById(R.id.layout_next);
btn_NextLast = (Button)findViewById(R.id.next_last);
btn_NextNext = (Button)findViewById(R.id.next_next);
btn_NextLast.setOnClickListener(listener);
btn_NextNext.setOnClickListener(listener);
}
最后,設(shè)置布局文件中的按鈕監(jiān)聽(tīng)事件,響應(yīng)3D旋轉(zhuǎn)動(dòng)畫(huà)和方向
private View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.main_last: // 上一頁(yè)
layoutmain.startAnimation(lQuest1Animation); // 當(dāng)前頁(yè)向左旋轉(zhuǎn)(0,-90)
initNext();
layoutnext.startAnimation(lQuest2Animation); // 下一頁(yè)向左旋轉(zhuǎn)(90, 0)
break;
case R.id.main_next: // 下一頁(yè)
layoutmain.startAnimation(rQuest1Animation); // 當(dāng)前頁(yè)向右旋轉(zhuǎn)(0,90)
initNext();
layoutnext.startAnimation(rQuest2Animation); // 下一頁(yè)向右旋轉(zhuǎn)(-90, 0)
break;
case R.id.next_last:
layoutnext.startAnimation(lQuest1Animation);
initMain();
layoutmain.startAnimation(lQuest2Animation);
break;
case R.id.next_next:
layoutnext.startAnimation(rQuest1Animation);
initMain();
layoutmain.startAnimation(rQuest2Animation);
break;
}
}
};
完整實(shí)例代碼代碼點(diǎn)擊此處本站下載。
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android實(shí)現(xiàn)3D翻轉(zhuǎn)動(dòng)畫(huà)效果
- Android實(shí)現(xiàn)3D推拉門(mén)式滑動(dòng)菜單源碼解析
- Android高級(jí)圖片滾動(dòng)控件實(shí)現(xiàn)3D版圖片輪播器
- Android酷炫動(dòng)畫(huà)效果之3D星體旋轉(zhuǎn)效果
- Android使用Rotate3dAnimation實(shí)現(xiàn)3D旋轉(zhuǎn)動(dòng)畫(huà)效果的實(shí)例代碼
- Android編程實(shí)現(xiàn)3D立體旋轉(zhuǎn)效果的實(shí)例代碼
- Android動(dòng)畫(huà)之3D翻轉(zhuǎn)效果實(shí)現(xiàn)函數(shù)分析
- Android 3D旋轉(zhuǎn)動(dòng)畫(huà)效果實(shí)現(xiàn)分解
- Android 使用Gallery實(shí)現(xiàn)3D相冊(cè)(附效果圖+Demo源碼)
- Android TV開(kāi)發(fā):實(shí)現(xiàn)3D仿Gallery效果的實(shí)例代碼
相關(guān)文章
利用Android中的TextView實(shí)現(xiàn)逐字顯示動(dòng)畫(huà)
在安卓程序啟動(dòng)的時(shí)候,想逐字顯示一段話,每個(gè)字都有一個(gè)從透明到不透明的漸變動(dòng)畫(huà)。那如何顯示這個(gè)效果,下面一起來(lái)看看。2016-08-08
Android 架構(gòu)之?dāng)?shù)據(jù)庫(kù)框架搭建
這篇文章主要給大家介紹的是Android 架構(gòu)之?dāng)?shù)據(jù)庫(kù)框架搭建,在本篇中,將會(huì)讓你一點(diǎn)一滴從無(wú)到有創(chuàng)建一個(gè)不再為數(shù)據(jù)庫(kù)而煩惱的框架。需要的朋友可以參考下面文章的具體內(nèi)容2021-09-09
Android中的Selector的用法詳解及實(shí)例
這篇文章主要介紹了Android中的Selector的用法詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-05-05
Android仿微信Viewpager-Fragment惰性加載(lazy-loading)
這篇文章主要為大家詳細(xì)介紹了Android仿微信Viewpager-Fragment惰性加載lazy-loading,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android中使用Gson解析JSON數(shù)據(jù)的兩種方法
Json是一種類似于XML的通用數(shù)據(jù)交換格式,具有比XML更高的傳輸效率;本文將介紹兩種方法解析JSON數(shù)據(jù),需要的朋友可以參考下2012-12-12
Android實(shí)現(xiàn)將已發(fā)送的短信寫(xiě)入短信數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Android實(shí)現(xiàn)將已發(fā)送的短信寫(xiě)入短信數(shù)據(jù)庫(kù)的方法,是Android手機(jī)開(kāi)發(fā)常見(jiàn)的技巧,需要的朋友可以參考下2014-09-09
Android破解微信獲取聊天記錄和通訊錄信息(靜態(tài)方式)
這篇文章主要介紹了Android以靜態(tài)方式破解微信獲取聊天記錄和通訊錄信息,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Android APK使用Debug簽名重新打包 Eclipse更改默認(rèn)Debug簽名
這篇文章主要介紹了Android APK使用Debug簽名重新打包 Eclipse更改默認(rèn)Debug簽名等內(nèi)容,需要的朋友可以參考下2015-04-04
Android實(shí)現(xiàn)城市選擇三級(jí)聯(lián)動(dòng)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)城市選擇三級(jí)聯(lián)動(dòng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-12-12
Android基礎(chǔ)教程數(shù)據(jù)存儲(chǔ)之文件存儲(chǔ)
這篇文章主要介紹了Android基礎(chǔ)教程數(shù)據(jù)存儲(chǔ)之文件存儲(chǔ)的相關(guān)資料,數(shù)據(jù)存儲(chǔ)是Android開(kāi)發(fā)的重要的知識(shí),這里提供了實(shí)例,需要的朋友可以參考下2017-07-07

