Android利用Camera實(shí)現(xiàn)中軸3D卡牌翻轉(zhuǎn)效果
在Android系統(tǒng)API中,有兩個(gè)Camera類:
- android.graphics.Camera
- android.hardware.Camera
第二個(gè)應(yīng)用于手機(jī)硬件中的相機(jī)相關(guān)的操作,本文講述的是利用第一個(gè)Camera類實(shí)現(xiàn)中軸3D轉(zhuǎn)換的卡牌翻轉(zhuǎn)效果,開始之前,先看一下Android系統(tǒng)中的坐標(biāo)系:
對(duì)應(yīng)于三維坐標(biāo)系中的三個(gè)方向,Camera提供了三種旋轉(zhuǎn)方法:
- rotateX()
- rotateY()
- rotateX()
調(diào)用這三種方法,傳入旋轉(zhuǎn)角度參數(shù),即可實(shí)現(xiàn)視圖沿著坐標(biāo)軸旋轉(zhuǎn)的功能。本文的中軸3D旋轉(zhuǎn)效果就是讓視圖沿著Y軸旋轉(zhuǎn)的。
系統(tǒng)API Demos中已經(jīng)為我們提供了一個(gè)非常好用的3D旋轉(zhuǎn)動(dòng)畫的工具類:
Rotate3dAnimation.java:
package com.feng.androidtest; import android.graphics.Camera; import android.graphics.Matrix; import android.util.Log; import android.view.animation.Animation; import android.view.animation.Transformation; /** * An animation that rotates the view on the Y axis between two specified angles. * This animation also adds a translation on the Z axis (depth) to improve the effect. */ public class Rotate3dAnimation extends Animation { private final float mFromDegrees; private final float mToDegrees; private final float mCenterX; private final float mCenterY; private final float mDepthZ; private final boolean mReverse; private Camera mCamera; /** * Creates a new 3D rotation on the Y axis. The rotation is defined by its * start angle and its end angle. Both angles are in degrees. The rotation * is performed around a center point on the 2D space, definied by a pair * of X and Y coordinates, called centerX and centerY. When the animation * starts, a translation on the Z axis (depth) is performed. The length * of the translation can be specified, as well as whether the translation * should be reversed in time. * * @param fromDegrees the start angle of the 3D rotation * @param toDegrees the end angle of the 3D rotation * @param centerX the X center of the 3D rotation * @param centerY the Y center of the 3D rotation * @param reverse true if the translation should be reversed, false otherwise */ 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(); } @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(); Log.i("interpolatedTime", interpolatedTime+""); 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); } }
可以看出, Rotate3dAnimation 總共做了兩件事:在構(gòu)造函數(shù)中賦值了旋轉(zhuǎn)動(dòng)畫所需要的參數(shù),以及重寫(override)父類Animation中的applyTransformation()方法,下面分類闡述一下:
- fromDegrees與toDegrees
- 視圖旋轉(zhuǎn)的開始角度和結(jié)束角度,當(dāng)toDegree處于90倍數(shù)時(shí),視圖將變得不可見。
- centerX與centerY
- 視圖旋轉(zhuǎn)的中心點(diǎn)。
- depthZ
- Z軸移動(dòng)基數(shù),用于計(jì)算Camera在Z軸移動(dòng)距離
- reverse
- boolean類型,控制Z軸移動(dòng)方向,達(dá)到視覺遠(yuǎn)近移動(dòng)導(dǎo)致的視圖放大縮小效果。
- applyTransformation()
- 根據(jù)動(dòng)畫播放的時(shí)間 interpolatedTime (動(dòng)畫start到end的過(guò)程,interpolatedTime從0.0變化到1.0),讓Camera在Z軸方向上進(jìn)行相應(yīng)距離的移動(dòng),實(shí)現(xiàn)視覺上遠(yuǎn)近移動(dòng)的效果。然后調(diào)用 rotateX()方法,讓視圖圍繞Y軸進(jìn)行旋轉(zhuǎn),產(chǎn)生3D立體旋轉(zhuǎn)效果。最后再通過(guò)Matrix來(lái)確定旋轉(zhuǎn)的中心點(diǎn)的位置。
activity_main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@android:color/white" > <Button android:id="@+id/btn_open" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="16dp" android:onClick="onClickView" android:text="打開" android:textColor="@android:color/black" android:textSize="16sp" /> <RelativeLayout android:id="@+id/rl_content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/btn_open" android:layout_marginTop="16dp" android:background="@android:color/black"> <ImageView android:id="@+id/iv_logo" android:layout_width="match_parent" android:layout_height="match_parent" android:contentDescription="@null" android:src="@drawable/ic_qrcode" android:scaleType="centerInside"/> <TextView android:id="@+id/tv_desc" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="16dp" android:text="腳本之家。" android:textColor="@android:color/white" android:textSize="18sp" android:visibility="gone"/> </RelativeLayout> </RelativeLayout>
布局中配置了卡牌正面的圖片控件,卡牌背面的文本控件,以及他們的parent容器,也就是本文中的旋轉(zhuǎn)動(dòng)畫的執(zhí)行對(duì)象。
MainActivity.java文件:
package com.feng.androidtest; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.animation.AccelerateInterpolator; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.DecelerateInterpolator; import android.widget.Button; import android.widget.ImageView; import android.widget.RelativeLayout; import android.widget.TextView; import com.example.androidtest.R; public class MainActivity extends Activity { private RelativeLayout mContentRl; private ImageView mLogoIv; private TextView mDescTv; private Button mOpenBtn; private int centerX; private int centerY; private int depthZ = 400; private int duration = 600; private Rotate3dAnimation openAnimation; private Rotate3dAnimation closeAnimation; private boolean isOpen = false; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContentRl = (RelativeLayout) findViewById(R.id.rl_content); mLogoIv = (ImageView) findViewById(R.id.iv_logo); mDescTv = (TextView) findViewById(R.id.tv_desc); mOpenBtn = (Button) findViewById(R.id.btn_open); } /** * 卡牌文本介紹打開效果:注意旋轉(zhuǎn)角度 */ private void initOpenAnim() { //從0到90度,順時(shí)針旋轉(zhuǎn)視圖,此時(shí)reverse參數(shù)為true,達(dá)到90度時(shí)動(dòng)畫結(jié)束時(shí)視圖變得不可見, openAnimation = new Rotate3dAnimation(0, 90, centerX, centerY, depthZ, true); openAnimation.setDuration(duration); openAnimation.setFillAfter(true); openAnimation.setInterpolator(new AccelerateInterpolator()); openAnimation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { mLogoIv.setVisibility(View.GONE); mDescTv.setVisibility(View.VISIBLE); //從270到360度,順時(shí)針旋轉(zhuǎn)視圖,此時(shí)reverse參數(shù)為false,達(dá)到360度動(dòng)畫結(jié)束時(shí)視圖變得可見 Rotate3dAnimation rotateAnimation = new Rotate3dAnimation(270, 360, centerX, centerY, depthZ, false); rotateAnimation.setDuration(duration); rotateAnimation.setFillAfter(true); rotateAnimation.setInterpolator(new DecelerateInterpolator()); mContentRl.startAnimation(rotateAnimation); } }); } /** * 卡牌文本介紹關(guān)閉效果:旋轉(zhuǎn)角度與打開時(shí)逆行即可 */ private void initCloseAnim() { closeAnimation = new Rotate3dAnimation(360, 270, centerX, centerY, depthZ, true); closeAnimation.setDuration(duration); closeAnimation.setFillAfter(true); closeAnimation.setInterpolator(new AccelerateInterpolator()); closeAnimation.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { } @Override public void onAnimationRepeat(Animation animation) { } @Override public void onAnimationEnd(Animation animation) { mLogoIv.setVisibility(View.VISIBLE); mDescTv.setVisibility(View.GONE); Rotate3dAnimation rotateAnimation = new Rotate3dAnimation(90, 0, centerX, centerY, depthZ, false); rotateAnimation.setDuration(duration); rotateAnimation.setFillAfter(true); rotateAnimation.setInterpolator(new DecelerateInterpolator()); mContentRl.startAnimation(rotateAnimation); } }); } public void onClickView(View v) { //以旋轉(zhuǎn)對(duì)象的中心點(diǎn)為旋轉(zhuǎn)中心點(diǎn),這里主要不要再onCreate方法中獲取,因?yàn)橐晥D初始繪制時(shí),獲取的寬高為0 centerX = mContentRl.getWidth()/2; centerY = mContentRl.getHeight()/2; if (openAnimation == null) { initOpenAnim(); initCloseAnim(); } //用作判斷當(dāng)前點(diǎn)擊事件發(fā)生時(shí)動(dòng)畫是否正在執(zhí)行 if (openAnimation.hasStarted() && !openAnimation.hasEnded()) { return; } if (closeAnimation.hasStarted() && !closeAnimation.hasEnded()) { return; } //判斷動(dòng)畫執(zhí)行 if (isOpen) { mContentRl.startAnimation(closeAnimation); }else { mContentRl.startAnimation(openAnimation); } isOpen = !isOpen; mOpenBtn.setText(isOpen ? "關(guān)閉" : "打開"); } }
代碼中已對(duì)核心的地方做了注釋解釋,主要弄清楚 rotate3dAnimation構(gòu)造參數(shù)中的 fromDegrees和toDegrees、depthZ、reverse參數(shù),同時(shí)在動(dòng)畫中設(shè)置了速度插播器,如動(dòng)畫的前半程使用加速器 AccelerateInterpolator,后半程使用減速器 DecelerateInterpolator,使動(dòng)畫體驗(yàn)更加人性化。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)Android軟件編程有所幫助。
- Android實(shí)現(xiàn)圖片反轉(zhuǎn)、翻轉(zhuǎn)、旋轉(zhuǎn)、放大和縮小
- Android動(dòng)畫之3D翻轉(zhuǎn)效果實(shí)現(xiàn)函數(shù)分析
- Android圖片翻轉(zhuǎn)動(dòng)畫簡(jiǎn)易實(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實(shí)現(xiàn)卡片翻轉(zhuǎn)動(dòng)畫
- android camera yuv幀水平翻轉(zhuǎn)實(shí)例
- android實(shí)現(xiàn)撲克卡片翻轉(zhuǎn)
相關(guān)文章
Android ListView數(shù)據(jù)的分批顯示功能
本文通過(guò)實(shí)例代碼給大家分享了Android ListView數(shù)據(jù)的分批顯示功能,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友參考下吧2017-04-04Android實(shí)現(xiàn)微信聊天語(yǔ)言點(diǎn)擊喇叭動(dòng)畫效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)微信聊天語(yǔ)言點(diǎn)擊喇叭動(dòng)畫效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android仿考拉全局滑動(dòng)返回及聯(lián)動(dòng)效果的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于Android仿考拉全局滑動(dòng)返回及聯(lián)動(dòng)效果的實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08iOS UIButton 點(diǎn)擊無(wú)響應(yīng)的解決辦法
在開發(fā)中按鈕我們經(jīng)常會(huì)遇到,但是有時(shí)候會(huì)碰到一些難以處理的問(wèn)題,就是按鈕點(diǎn)擊無(wú)響應(yīng),其實(shí)解決方法也不難。下面小編之家小編抽空給大家介紹iOS UIButton 點(diǎn)擊無(wú)響應(yīng)的解決辦法,需要的朋友參考下吧2017-12-12Android?Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器App?(Java語(yǔ)言版)
這篇文章主要為大家詳細(xì)介紹了Android?Studio實(shí)現(xiàn)簡(jiǎn)易計(jì)算器App,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05android app跳轉(zhuǎn)應(yīng)用商店實(shí)現(xiàn)步驟
這篇文章主要為大家介紹了android app跳轉(zhuǎn)應(yīng)用商店實(shí)現(xiàn)步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11Android APP檢測(cè)實(shí)體按鍵事件詳解
這篇文章主要為大家詳細(xì)介紹了Android APP檢測(cè)實(shí)體按鍵事件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08詳解Flutter中網(wǎng)絡(luò)框架dio的二次封裝
其實(shí)dio框架已經(jīng)封裝的很好了,但是在實(shí)戰(zhàn)項(xiàng)目中,為了項(xiàng)目可以統(tǒng)一管理,還是需要對(duì)dio框架進(jìn)行二次封裝。本文將詳細(xì)講解一下dio如何二次封裝,需要的可以參考一下2022-04-04