您好,登錄后才能下訂單哦!
本文章向大家介紹怎么在Android中利用animator實(shí)現(xiàn)一個(gè)3D翻轉(zhuǎn)效果的基本知識(shí)點(diǎn)總結(jié)和需要注意事項(xiàng),具有一定的參考價(jià)值,需要的朋友可以參考一下。
Android是一種基于Linux內(nèi)核的自由及開放源代碼的操作系統(tǒng),主要使用于移動(dòng)設(shè)備,如智能手機(jī)和平板電腦,由美國(guó)Google公司和開放手機(jī)聯(lián)盟領(lǐng)導(dǎo)及開發(fā)。
首先講解一下主要實(shí)現(xiàn)動(dòng)畫的函數(shù):
getFragmentManager().beginTransaction() .setCustomAnimations(R.animator.fragment_second_3d_reversal_enter,R.animator.fragment_second_3d_reversal_exit) .replace(R.id.container, new MainFragment()).commit();
我想信這個(gè)函數(shù)大家在實(shí)現(xiàn)動(dòng)畫時(shí)都會(huì)使用到,先獲得FragmentManager然后進(jìn)行transaction,主要添加動(dòng)畫的函數(shù)是setCustomAnimations(),在網(wǎng)上可以查到的解釋,對(duì)這個(gè)方法有些錯(cuò)誤,描述的是當(dāng)前的Fragment對(duì)象的進(jìn)入和退出時(shí)的動(dòng)畫效果,是這個(gè)對(duì)象的一種屬性,但是這個(gè)方法真正的解釋應(yīng)該是在當(dāng)前Activity在切換Fragment時(shí)所執(zhí)行的動(dòng)畫方式,也就是說當(dāng)前Fragment退出時(shí)用的是方法中的退出動(dòng)畫,新的Fragment進(jìn)入時(shí)執(zhí)行的是進(jìn)入的動(dòng)畫效果,可以理解為這一次動(dòng)畫效果完全是利用這一個(gè)語句來完成,有些博客的記載對(duì)我們產(chǎn)生了一些誤導(dǎo)。
官方的注釋如下:
/** * Set specific animation resources to run for the fragments that are * entering and exiting in this transaction. These animations will not be * played when popping the back stack. */ public abstract FragmentTransaction setCustomAnimations(int enter, int exit);
整體的3D翻轉(zhuǎn)效果代碼如下:
第二個(gè)Fragment。
/** * Created by Liurs on 2016/6/14. **/ public class SecondFragment extends Fragment { private LinearLayout root; private Button mButton; public SecondFragment() { } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { root = (LinearLayout) inflater.inflate(R.layout.fragment_second, container, false); //Set listener; setListener(); return root; } /** * set listener */ private void setListener() { mButton = (Button) root.findViewById(R.id.button); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getFragmentManager().beginTransaction() .setCustomAnimations(R.animator.fragment_second_3d_reversal_enter,R.animator.fragment_second_3d_reversal_exit) .replace(R.id.container, new MainFragment()).commit(); } }); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); } }
第一個(gè)Fragment。
/** * Created by Liurs on 2016/6/14. **/ public class MainFragment extends Fragment { private LinearLayout root; private Button mButton; public MainFragment() { } @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { root = (LinearLayout) inflater.inflate(R.layout.content_main, container, false); //Set listener; setListener(); return root; } /** * set listener */ private void setListener() { mButton = (Button) root.findViewById(R.id.button); mButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getFragmentManager() .beginTransaction() .addToBackStack(null) .setCustomAnimations(R.animator.fragment_3d_reversal_enter,R.animator.fragment_3d_reversal_exit,R.animator.fragment_second_3d_reversal_enter,R.animator.fragment_second_3d_reversal_exit) .replace(R.id.container, new SecondFragment()) .commit(); } }); } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); } }
逆時(shí)針翻轉(zhuǎn)動(dòng)畫進(jìn)入時(shí)配置文件。
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially"> <objectAnimator android:duration="0" android:propertyName="rotationY" android:valueFrom="0f" android:valueTo="270f" /> <objectAnimator android:duration="500" android:propertyName="rotationY" android:startOffset="500" android:valueFrom="270f" android:valueTo="360f" /> </set>
退出時(shí)動(dòng)畫配置文件,
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially"> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500" android:propertyName="rotationY" android:valueFrom="0f" android:valueTo="90f"> </objectAnimator> </set>
順時(shí)針翻轉(zhuǎn)動(dòng)畫進(jìn)入時(shí)配置文件,
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially"> <objectAnimator android:duration="0" android:propertyName="rotationY" android:valueFrom="180f" android:valueTo="90f" /> <objectAnimator android:duration="500" android:propertyName="rotationY" android:startOffset="500" android:valueFrom="90f" android:valueTo="0f" /> </set>
退出時(shí)動(dòng)畫配置文件,
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially"> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500" android:propertyName="rotationY" android:valueFrom="0f" android:valueTo="-90f"> </objectAnimator> </set>
以上就是小編為大家?guī)淼脑趺丛贏ndroid中利用animator實(shí)現(xiàn)一個(gè)3D翻轉(zhuǎn)效果的全部?jī)?nèi)容了,希望大家多多支持億速云!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。