Android實(shí)現(xiàn)一個比相冊更高大上的左右滑動特效(附源碼)
在Android里面,想要實(shí)現(xiàn)一個類似相冊的左右滑動效果,我們除了可以用Gallery、HorizontalScrollView、ViewPager等控件,還可以用一個叫做 ViewFlipper 的類來代替實(shí)現(xiàn),它繼承于 ViewAnimator。如見其名,這個類是跟動畫有關(guān),會將添加到它里面的兩個或者多個View做一個動畫,然后每次只顯示一個子View,通過在 View 之間切換時執(zhí)行動畫,最終達(dá)到一個類似相冊能左右滑動的效果。
本次功能要實(shí)現(xiàn)的兩個基本效果
- 最基本的左右滑動效果
- 從屏幕的45度方向進(jìn)入和退出的效果
實(shí)現(xiàn)思路
- 按照 ViewFlipper 的源碼說明,它是將兩個或多個View用動畫展示出來。那么我就在 ViewFlipper 內(nèi)放入兩個布局,每個布局都包含一個 TextView 和 ImageView,分別用于顯示文字和圖片
- 既然要有動畫效果,我準(zhǔn)備使用Android的位移動畫類 TranslateAnimation,設(shè)置起始的橫縱坐標(biāo)值
- 為了讓效果明顯,我會設(shè)置 ViewFlipper 的進(jìn)入和退出屏幕的動畫,并且在左滑時呈現(xiàn)一個動畫、右滑時呈現(xiàn)另一個動畫(需要判斷是左滑還是右滑:重寫 onTouchEvent 方法,比較橫坐標(biāo)X的值的變化)
源碼如下:
1、主Activity
// import語句省略 public class ViewFlipperDemo extends Activity { private static final String TAG = "ViewFlipperDemo"; private ViewFlipper mViewFlipper; private float mOldTouchValue; @Override protected void onCreate(Bundle onSavedInstance) { super.onCreate(onSavedInstance); // 設(shè)置為全屏 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.view_flipper_demo); mViewFlipper = findViewById(R.id.viewFlipper1); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: mOldTouchValue = event.getX(); break; case MotionEvent.ACTION_UP: float currentX = event.getX(); // 手指向右滑動: 手指向右滑動時橫坐標(biāo) X 的值會變大,因此 currentX 的值更大 if (mOldTouchValue < currentX) { // 進(jìn)入屏幕的動效 mViewFlipper.setInAnimation(AnimationHelper.inFromLeftAnimation()); // 退出屏幕的動效 mViewFlipper.setOutAnimation(AnimationHelper.outToRightAnimation()); mViewFlipper.showNext(); } // 橫坐標(biāo)的值變小,說明是左滑 if (mOldTouchValue > currentX) { // 進(jìn)入屏幕的動效 mViewFlipper.setInAnimation(AnimationHelper.inFromRightAnimation()); // 退出屏幕的動效 mViewFlipper.setOutAnimation(AnimationHelper.outToLeftAnimation()); mViewFlipper.showPrevious(); } break; default: break; } return super.onTouchEvent(event); } }
2、對應(yīng)的布局文件 view_flipper_demo.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/colorBlack" android:gravity="center" android:text="這是一個ViewFlipper樣例" android:paddingTop="20dp"/> <ViewFlipper android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/viewFlipper1"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/colorBlue" android:gravity="center" android:text="這是第一個ViewFlipper頁面"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/avasterdr"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/colorBlue" android:gravity="center" android:text="這是第二個ViewFlipper頁面"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/avastertony"/> </LinearLayout> </ViewFlipper> </LinearLayout>
3、動畫輔助類 AnimationHelper.java
public class AnimationHelper { // 左滑的進(jìn)入動畫 public static Animation inFromRightAnimation() { Animation inFromRight = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); inFromRight.setDuration(500); inFromRight.setInterpolator(new AccelerateInterpolator()); return inFromRight; } // 左滑的退出動畫 public static Animation outToLeftAnimation() { Animation outToLeft = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); outToLeft.setDuration(500); outToLeft.setInterpolator(new AccelerateInterpolator()); return outToLeft; } // 右滑的進(jìn)入動畫 public static Animation inFromLeftAnimation() { Animation inFromLeft = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); inFromLeft.setDuration(500); inFromLeft.setInterpolator(new AccelerateInterpolator()); return inFromLeft; } // 右滑的退出動畫 public static Animation outToRightAnimation() { Animation outToRight = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f); outToRight.setDuration(500); outToRight.setInterpolator(new AccelerateInterpolator()); return outToRight; } }
4、對應(yīng)的效果圖如下
可以看到,這個左右滑動效果沒有任何酷炫的地方。我們不妨先來看看跟動畫相關(guān)的幾個重點(diǎn)地方:
(1)函數(shù) setInAnimation:是指 View 進(jìn)入屏幕的動效
(2)函數(shù) setOutAnimation:是指 View 退出屏幕的動效
(3)TranslateAnimation的構(gòu)造函數(shù)的參數(shù)解釋:
1、fromXType/toXType/fromYType/toYType,取值共有三個:
Animation.ABSOLUTEAnimation.RELATIVE_TO_SELFAnimation.RELATIVE_TO_PARENT
我這里用的是 Animation.RELATIVE_TO_PARENT,當(dāng)傳入該參數(shù)時,其余幾個坐標(biāo)值需要傳入百分比參數(shù)(1.0表示100%);如果傳入 Animation.ABSOLUTE,坐標(biāo)值需要傳入屏幕上的絕對位置(比如1000,1000)
2、fromXValue:起點(diǎn)的橫坐標(biāo)值
3、toXValue:終點(diǎn)的橫坐標(biāo)值
4、fromYValue:起點(diǎn)的縱坐標(biāo)值
5、toYValue:終點(diǎn)的縱坐標(biāo)值
如果我們想讓這個效果變成45度從屏幕的四個角進(jìn)入和退出,那代碼就應(yīng)該這么寫(注意代碼中傳入的 4 個橫縱坐標(biāo)值):
// 左滑的進(jìn)入動畫 public static Animation inFromRightAnimation() { Animation inFromRight = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f); inFromRight.setDuration(500); inFromRight.setInterpolator(new AccelerateInterpolator()); return inFromRight; } // 左滑的退出動畫 public static Animation outToLeftAnimation() { Animation outToLeft = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f); outToLeft.setDuration(500); outToLeft.setInterpolator(new AccelerateInterpolator()); return outToLeft; } // 右滑的進(jìn)入動畫 public static Animation inFromLeftAnimation() { Animation inFromLeft = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f); inFromLeft.setDuration(500); inFromLeft.setInterpolator(new AccelerateInterpolator()); return inFromLeft; } // 右滑的退出動畫 public static Animation outToRightAnimation() { Animation outToRight = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f, Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 1.0f); outToRight.setDuration(500); outToRight.setInterpolator(new AccelerateInterpolator()); return outToRight; }
對應(yīng)的效果如下:
之所以有 -1.0f 這個值,是因?yàn)槠聊簧系臋M縱坐標(biāo)值的分布可以用如下象限來表示:
ViewFlipper中的 View 就位于象限的中心位置。因此,如果動畫從左上角進(jìn)入,那么它的起始橫縱坐標(biāo)就是(-1,-1)。大家可以按照這個思路去實(shí)現(xiàn)自己想要的動效。
到此這篇關(guān)于Android實(shí)現(xiàn)一個比相冊更高大上的左右滑動特效(附源碼)的文章就介紹到這了,更多相關(guān)android 實(shí)現(xiàn)左右滑動特效內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Android自定義view實(shí)現(xiàn)滑動解鎖效果
- Android實(shí)現(xiàn)小米相機(jī)底部滑動指示器
- Android實(shí)現(xiàn)View滑動效果的6種方法
- Android自定義SeekBar實(shí)現(xiàn)滑動驗(yàn)證且不可點(diǎn)擊
- Android SeekBar實(shí)現(xiàn)禁止滑動
- Android 滑動Scrollview標(biāo)題欄漸變效果(仿京東toolbar)
- Android RecycleView滑動停止后自動吸附效果的實(shí)現(xiàn)代碼(滑動定位)
- Android實(shí)現(xiàn)上下菜單雙向滑動
- Android實(shí)現(xiàn)滑動效果
- Android實(shí)現(xiàn)手勢滑動(左滑和右滑)
- Android RecyclerView實(shí)現(xiàn)滑動刪除
- 詳解Android使用CoordinatorLayout+AppBarLayout+CollapsingToolbarLayou實(shí)現(xiàn)手指滑動效果
- Android實(shí)現(xiàn)三段式滑動效果
相關(guān)文章
android二級listview列表實(shí)現(xiàn)代碼
今天來實(shí)現(xiàn)以下大眾點(diǎn)評客戶端的橫向listview二級列表,感興趣的朋友可以研究下2013-01-01Android 懸浮窗權(quán)限各機(jī)型各系統(tǒng)適配大全(總結(jié))
這篇文章主要介紹了Android 懸浮窗權(quán)限各機(jī)型各系統(tǒng)適配大全(總結(jié)),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08Android:Field can be converted to a local varible.的解決辦法
這篇文章主要介紹了Android:Field can be converted to a local varible.的解決辦法的相關(guān)資料,希望通過本文能幫助到大家,讓大家遇到這樣的問題輕松解決,需要的朋友可以參考下2017-10-10Android如何利用RecyclerView實(shí)現(xiàn)列表倒計(jì)時效果實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于Android如何利用RecyclerView實(shí)現(xiàn)列表倒計(jì)時效果的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-02-02Android自定義LinearLayout實(shí)現(xiàn)淘寶詳情頁
這篇文章主要為大家詳細(xì)介紹了Android自定義LinearLayout實(shí)現(xiàn)淘寶詳情頁的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09Android開發(fā)之無痕過渡下拉刷新控件的實(shí)現(xiàn)思路詳解
下拉刷新效果功能在程序開發(fā)中經(jīng)常會見到,今天小編抽時間給大家分享Android開發(fā)之無痕過渡下拉刷新控件的實(shí)現(xiàn)思路詳解,需要的朋友參考下吧2016-11-11