Android實(shí)現(xiàn)移動(dòng)小球和CircularReveal頁(yè)面切換動(dòng)畫實(shí)例代碼
前言
本文主要給大家介紹了關(guān)于Android如何實(shí)現(xiàn)移動(dòng)小球和CircularReveal頁(yè)面切換動(dòng)畫的相關(guān)內(nèi)容,分享出來(lái)供大家參考學(xué)習(xí),下面話不多說(shuō)了,來(lái)一起看看詳細(xì)的介紹吧。
效果圖如下
是在fragment中跳轉(zhuǎn)activity實(shí)現(xiàn)的效果,fragment跳fragment,activity跳activity類似~~
實(shí)現(xiàn)過(guò)程
- 重寫FloatingActionButton的onTouchListener()方法,使小球可以移動(dòng),并判斷邊界
- 點(diǎn)擊fab時(shí)記錄坐標(biāo)傳到下一個(gè)頁(yè)面,在下一個(gè)頁(yè)面展示動(dòng)畫。
- 點(diǎn)擊后退或者重寫onBackPressed()方法,執(zhí)行動(dòng)畫
重寫Fab的onTouchListener()
floatingActionButton.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: downX = ev.getX(); downY = ev.getY(); isClick = true; break; case MotionEvent.ACTION_MOVE: isClick = false; moveX = ev.getX(); moveY = ev.getY(); int offsetX = (int) (moveX - downX); int offsetY = (int) (moveY - downY); //這里使用了setTranslation來(lái)移動(dòng)view。。。嘗試過(guò)layout。不知道為什么fragment切換回來(lái)的時(shí)候會(huì)恢復(fù)原位 floatingActionButton.setTranslationX(floatingActionButton.getTranslationX() + offsetX); floatingActionButton.setTranslationY(floatingActionButton.getTranslationY() + offsetY); break; case MotionEvent.ACTION_UP: //用來(lái)觸發(fā)點(diǎn)擊事件 if (isClick) { startAct(); return false; } //用來(lái)判斷移動(dòng)邊界 if (floatingActionButton.getX() < 0) { floatingActionButton.setX(0); } if (floatingActionButton.getX() + floatingActionButton.getWidth() > ScreenUtil.getScreenWidth(getContext())) { floatingActionButton.setX(ScreenUtil.getScreenWidth(getContext()) - floatingActionButton.getWidth()); } if (floatingActionButton.getY() < titleHeight) { floatingActionButton.setY(0); } if (floatingActionButton.getY() + floatingActionButton.getHeight() + titleHeight > getActivity().findViewById(R.id.activity_main_mainLl).getHeight() - getActivity().findViewById(R.id.fc_rg).getHeight()) { floatingActionButton.setY(getBottomY()); } break; } return true; } private void startAct() { //跳轉(zhuǎn)Activity,傳遞動(dòng)畫參數(shù) Intent intent = new Intent(getActivity(), CheckWorkActivity.class); intent.putExtra("x", (int) floatingActionButton.getX() + floatingActionButton.getWidth() / 2); intent.putExtra("y", (int) floatingActionButton.getY() + floatingActionButton.getHeight() / 2); intent.putExtra("start_radius", floatingActionButton.getWidth() / 2); intent.putExtra("end_radius", DialogFragment.this.view.getHeight()); startActivity(intent); } });
在下一個(gè)頁(yè)面中實(shí)現(xiàn)CircleRevel動(dòng)畫
onCrete中調(diào)用
private void initAnimation() { //ll為根布局 final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll); linearLayout.post(new Runnable() { @Override public void run() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Animator animator = ViewAnimationUtils.createCircularReveal( linearLayout,// 操作的視圖 getIntent().getIntExtra("x", 0), // 動(dòng)畫的中心點(diǎn)X getIntent().getIntExtra("y", 0) + findViewById(R.id.title).getHeight(), // 動(dòng)畫的中心點(diǎn)Y getIntent().getIntExtra("start_radius", 0), // 動(dòng)畫半徑 getIntent().getIntExtra("end_radius", 0) // 動(dòng)畫結(jié)束半徑 ); animator.setInterpolator(new AccelerateInterpolator()); animator.setDuration(500); animator.start(); } } }); }
點(diǎn)擊后退或者觸發(fā)onBackPressed時(shí)候調(diào)用
private void endAnim() { final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { Animator animator = ViewAnimationUtils.createCircularReveal( linearLayout,// 操作的視圖 getIntent().getIntExtra("x", 0), getIntent().getIntExtra("y", 0) + findViewById(R.id.title).getHeight(), getIntent().getIntExtra("end_radius", 0), getIntent().getIntExtra("start_radius", 0) ); animator.setInterpolator(new AccelerateInterpolator()); animator.setDuration(500); animator.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { super.onAnimationEnd(animation); finish(); } }); animator.start(); } }
還有一個(gè)重要的地方是修改兩個(gè)activity的theme
<style name="AppThemeCircleRevel" parent="Theme.AppCompat.Light.NoActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/blue</item> <item name="android:windowAnimationStyle">@null</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowIsTranslucent">true</item> <item name="android:colorBackgroundCacheHint">@null</item> </style>
總結(jié)
以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問(wèn)大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
相關(guān)文章
Android 實(shí)現(xiàn)手機(jī)接通電話后振動(dòng)提示的功能
本文主要介紹Android 實(shí)現(xiàn)手機(jī)接通電話后振動(dòng)提示的功能,這里整理了詳細(xì)的相關(guān)資料,并附有示例代碼,有需要的朋友可以參考下2016-08-08Android程序啟動(dòng)時(shí)出現(xiàn)黑屏問(wèn)題的解決方法
這篇文章主要介紹了Android程序啟動(dòng)時(shí)出現(xiàn)黑屏問(wèn)題的解決方法,分析了黑屏出現(xiàn)的原因及相應(yīng)的解決方法,需要的朋友可以參考下2016-08-08Android手勢(shì)操作簡(jiǎn)單實(shí)例講解
這篇文章主要為大家詳細(xì)介紹了Android手勢(shì)操作簡(jiǎn)單實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09android 開發(fā)教程之日歷項(xiàng)目實(shí)踐(一)
決定開始學(xué)習(xí) Android 平臺(tái)下的軟件開發(fā),以日歷作為實(shí)踐項(xiàng)目,進(jìn)行一周后,基本完成,有需要的朋友可以參考下2013-01-01Android studio創(chuàng)建第一個(gè)app
這篇文章主要為大家詳細(xì)介紹了如何使用Android studio創(chuàng)建你的第一個(gè)項(xiàng)目Hello World,感興趣的小伙伴們可以參考一下2016-05-05Kotlin中標(biāo)準(zhǔn)函數(shù)run、with、let、also與apply的使用和區(qū)別詳解
相比Java, Kotlin提供了不少高級(jí)語(yǔ)法特性。對(duì)于一個(gè)Kotlin的初學(xué)者來(lái)說(shuō)經(jīng)常會(huì)寫出一些不夠優(yōu)雅的代碼,下面這篇文章主要給大家介紹了關(guān)于Kotlin中標(biāo)準(zhǔn)函數(shù)run、with、let、also與apply的使用和區(qū)別的相關(guān)資料,需要的朋友可以參考下。2018-03-03Android實(shí)現(xiàn)五子棋游戲(局域網(wǎng)版)
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)局域網(wǎng)版的五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-05-05android實(shí)現(xiàn)底部導(dǎo)航欄
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)底部導(dǎo)航欄,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06