Android實(shí)現(xiàn)頁面滑動(dòng)切換動(dòng)畫
本文實(shí)例為大家分享了Android實(shí)現(xiàn)頁面滑動(dòng)切換動(dòng)畫的具體代碼,供大家參考,具體內(nèi)容如下
實(shí)現(xiàn)兩個(gè)頁面滑動(dòng)切換,一些相冊的效果也是如此
一個(gè)Activity的界面配置文件
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<!--ViewFlipper里面的子控件可以被看成一頁-->
<ViewFlipper
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/viewFlipper"
>
<!-- 第一頁 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#339900"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="第一頁"
/>
</LinearLayout>
<!-- 第二頁 -->
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="第二頁"
/>
</LinearLayout>
</ViewFlipper>
</LinearLayout>
MainActivity.java:
package com.example.activitymove;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ViewFlipper;
public class MainActivity extends Activity {
private ViewFlipper viewFlipper;
private float startX;
private float endX;
private Animation in_lefttoright;
private Animation out_lefttoright;
private Animation in_righttoleft;
private Animation out_righttoleft;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
in_lefttoright=AnimationUtils.loadAnimation(this, R.anim.enter_lefttoright);
out_lefttoright=AnimationUtils.loadAnimation(this, R.anim.out_lefttoright);
in_righttoleft=AnimationUtils.loadAnimation(this, R.anim.enter_righttoleft);
out_righttoleft=AnimationUtils.loadAnimation(this, R.anim.out_righttoleft);
viewFlipper=(ViewFlipper) this.findViewById(R.id.viewFlipper);
}
//處理觸屏?xí)r間的方法
//手在屏幕上向右滑動(dòng)然后松開翻下一頁,向左翻顯示前一頁
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN){
//記錄手放在屏幕上的點(diǎn)位置
startX=event.getX();
}else if(event.getAction()==MotionEvent.ACTION_UP){
//記錄手離開屏幕上的點(diǎn)位置
endX=event.getX();
if(endX>startX){
viewFlipper.setInAnimation(in_lefttoright);
viewFlipper.setOutAnimation(out_lefttoright);
viewFlipper.showNext();//顯示下一頁
}else if(endX<startX){
viewFlipper.setInAnimation(in_righttoleft);
viewFlipper.setOutAnimation(out_righttoleft);
viewFlipper.showPrevious();//顯示前一頁
}
return true;
}
return super.onTouchEvent(event);
}
}
在res/anim/文件夾下有
enter_lefttoright.xml和enter_righttoleft.xml
out_lefttoright.xml和out_righttoleft.xml
四個(gè)動(dòng)畫配置文件:
enter_lefttoright.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="-100%p"
android:toXDelta="0"
android:duration="1000"
/>
</set>
enter_righttoleft.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<!-- 這里用到了平移動(dòng)畫,這里只動(dòng)x軸坐標(biāo)就可以了
-100%p:這就是屏幕的寬度:這里的p代表parent,父元素的寬度,都是
手機(jī)屏幕寬度,第一頁要從-100%p移動(dòng)到0,持續(xù)5秒中.
-->
<translate
android:fromXDelta="100%p"
android:toXDelta="0"
android:duration="1000"
/>
</set>
out_lefttoright.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0"
android:toXDelta="100%p"
android:duration="1000"
/>
</set>
out_righttoleft.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="0"
android:toXDelta="-100%p"
android:duration="1000"
/>
</set>
效果:用手向右滑動(dòng),整個(gè)頁面向右慢慢滑動(dòng),切換頁面,用手向左滑動(dòng),整個(gè)頁面向左慢慢滑動(dòng),切換頁面。
(將配置文件換成其他的動(dòng)畫效果也可以,本例子使用的是移入移出的動(dòng)畫效果)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android仿京東頂部搜索框滑動(dòng)伸縮動(dòng)畫效果
- Android實(shí)現(xiàn)手勢滑動(dòng)和簡單動(dòng)畫效果
- Android程序開發(fā)之使用Design包實(shí)現(xiàn)QQ動(dòng)畫側(cè)滑效果和滑動(dòng)菜單導(dǎo)航
- Android編程實(shí)現(xiàn)ViewPager多頁面滑動(dòng)切換及動(dòng)畫效果的方法
- Android Tween動(dòng)畫之RotateAnimation實(shí)現(xiàn)圖片不停旋轉(zhuǎn)效果實(shí)例介紹
- android實(shí)現(xiàn)圖片閃爍動(dòng)畫效果的兩種實(shí)現(xiàn)方式(實(shí)用性高)
- Android Glide圖片加載(加載監(jiān)聽、加載動(dòng)畫)
- Android圖片翻轉(zhuǎn)動(dòng)畫簡易實(shí)現(xiàn)代碼
- Android實(shí)現(xiàn)圖片點(diǎn)擊預(yù)覽效果(zoom動(dòng)畫)
- Android實(shí)現(xiàn)ViewFlipper圖片動(dòng)畫滑動(dòng)
相關(guān)文章
Android簡單實(shí)現(xiàn)計(jì)算器功能
這篇文章主要為大家詳細(xì)介紹了Android簡單實(shí)現(xiàn)計(jì)算器功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08
Android基礎(chǔ)教程數(shù)據(jù)存儲之文件存儲
這篇文章主要介紹了Android基礎(chǔ)教程數(shù)據(jù)存儲之文件存儲的相關(guān)資料,數(shù)據(jù)存儲是Android開發(fā)的重要的知識,這里提供了實(shí)例,需要的朋友可以參考下2017-07-07
Android開發(fā)在RecyclerView上面實(shí)現(xiàn)"拖放"和"滑動(dòng)刪除"-2
這篇文章主要介紹了Android開發(fā)在RecyclerView上面實(shí)現(xiàn)"拖放"和"滑動(dòng)刪除"(二)功能,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-03-03
android通過自定義toast實(shí)現(xiàn)懸浮通知效果的示例代碼
這篇文章主要介紹了android通過自定義toast實(shí)現(xiàn)懸浮通知效果,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10
Android 中RecyclerView多種item布局的寫法(頭布局+腳布局)
這篇文章主要介紹了Android 中RecyclerView多種item布局的寫法(頭布局+腳布局)的相關(guān)資料,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-01-01
Android開發(fā)之ProgressDialog進(jìn)度對話框用法示例
這篇文章主要介紹了Android開發(fā)之ProgressDialog進(jìn)度對話框用法,簡單介紹了ProgressDialog進(jìn)度對話框常見函數(shù)功能,并結(jié)合實(shí)例形式分析了ProgressDialog組件創(chuàng)建及使用進(jìn)度對話框相關(guān)操作技巧,需要的朋友可以參考下2019-03-03

