国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android使用animator實現fragment的3D翻轉效果

2019-12-12 01:21:59
字體:
來源:轉載
供稿:網友

今天老師留的作業,使用倆個Fragment來實現3D翻轉效果,遇到了一點點的問題,于是在網上進行了查找,但是發現有些博主的代碼不正確,對其他人進行了誤導,在網上使用屬性動畫實現3D效果非常少,所以經過我自己的實驗摸索,我將自己的代碼和遇到的問題給他講解一下提供一點點借鑒,并且希望可以幫助到大家。
首先講解一下主要實現動畫的函數:

getFragmentManager().beginTransaction()    .setCustomAnimations(R.animator.fragment_second_3d_reversal_enter,R.animator.fragment_second_3d_reversal_exit)    .replace(R.id.container, new MainFragment()).commit();

我想信這個函數大家在實現動畫時都會使用到,先獲得FragmentManager然后進行transaction,主要添加動畫的函數是setCustomAnimations(),在網上可以查到的解釋,對這個方法有些錯誤,描述的是當前的Fragment對象的進入和退出時的動畫效果,是這個對象的一種屬性,但是這個方法真正的解釋應該是在當前Activity在切換Fragment時所執行的動畫方式,也就是說當前Fragment退出時用的是方法中的退出動畫,新的Fragment進入時執行的是進入的動畫效果,可以理解為這一次動畫效果完全是利用這一個語句來完成,有些博客的記載對我們產生了一些誤導。

官方的注釋如下:

/** * 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翻轉效果代碼如下:

第二個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);  }}

第一個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);  }}

逆時針翻轉動畫進入時配置文件。

<?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>

退出時動畫配置文件,

<?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>

順時針翻轉動畫進入時配置文件,

<?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>

退出時動畫配置文件,

<?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>

至此,兩個Fragment的3D翻轉切換已經完成,希望我的經驗可以幫助到你們。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 那坡县| 蕲春县| 淮滨县| 夹江县| 准格尔旗| 万荣县| 沙雅县| 蒙城县| 贡觉县| 五常市| 龙南县| 龙州县| 江安县| 全州县| 简阳市| 张家港市| 霍邱县| 双辽市| 丰原市| 建水县| 柘城县| 宜君县| 铁岭县| 凭祥市| 景德镇市| 宁夏| 聂荣县| 太仆寺旗| 玉环县| 浦城县| 雷山县| 布拖县| 磐石市| 沙雅县| 许昌县| 英超| 锡林郭勒盟| 建湖县| 开平市| 弥渡县| 白玉县|