利用ViewFlipper來實現頁面左右滑動切換的效果很簡單,
只要使用ViewFlipper將View裝在一起,再用GestureDetector做手勢辨別就可以了。
新增四個anim檔案,分別為左進、左出、右進、右出
左進 (push_left_in.xml):手勢往左滑的進場動作
左出(push_left_out.xml):手勢往左滑的出場動作
右進(push_right_in.xml):手勢往右滑的進場動作
右出(push_right_out.xml):手勢往右滑的出場動作
translate標籤(位置移動)屬性介紹:
fromXDelta:X軸方向的起始位置
toXDelta:X軸方向的結束位置
fromYDelta:Y軸方向的起始位置
toYDelta:Y軸方向的結束位置
以上屬性的值可以填入
(1) 數值 → 如果數值為50,代表在目前View的左上角加上50px後做為起點
(2) % → 如果為50%,代表在目前View的左上角加上自己寬度的50%後做為起點
(3) %p → 如果為50%p,代表在目前View的左上角加上父元件寬度的50%後做為起點
手勢的判斷要繼承OnGestureListener方法。
(出現錯誤按下Alt+Enter,並匯入Method)
接著在onCreate中宣告GestureDetector方法,並在onFling的方法中加上判斷手勢方向的方法
最後宣告onTouchEvent,如果沒宣告的話,會無法滑動
完整MainActivity.java程式碼:
參考資料:启舰的博客
只要使用ViewFlipper將View裝在一起,再用GestureDetector做手勢辨別就可以了。
Step1 在layout加上ViewFlipper元件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ViewFlipper | |
android:id="@+id/vf" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
</ViewFlipper> |
Step2 在anim資料夾中新增左右滑動動畫的XML
新增anim資料夾,並且將Resource Type 選擇 anim新增四個anim檔案,分別為左進、左出、右進、右出
左進 (push_left_in.xml):手勢往左滑的進場動作
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<set xmlns:android="http://schemas.android.com/apk/res/android"> | |
<translate | |
android:duration="500" | |
android:fromXDelta="100%p" | |
android:toXDelta="0" /> | |
<alpha | |
android:duration="500" | |
android:fromAlpha="0.1" | |
android:toAlpha="1.0" /> | |
</set> |
左出(push_left_out.xml):手勢往左滑的出場動作
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<set xmlns:android="http://schemas.android.com/apk/res/android"> | |
<translate | |
android:duration="500" | |
android:fromXDelta="0" | |
android:toXDelta="-100%p" /> | |
<alpha | |
android:duration="500" | |
android:fromAlpha="1.0" | |
android:toAlpha="0.1" /> | |
</set> |
右進(push_right_in.xml):手勢往右滑的進場動作
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<set xmlns:android="http://schemas.android.com/apk/res/android"> | |
<translate | |
android:duration="500" | |
android:fromXDelta="-100%p" | |
android:toXDelta="0" /> | |
<alpha | |
android:duration="500" | |
android:fromAlpha="0.1" | |
android:toAlpha="1.0" /> | |
</set> |
右出(push_right_out.xml):手勢往右滑的出場動作
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<set xmlns:android="http://schemas.android.com/apk/res/android"> | |
<translate | |
android:duration="500" | |
android:fromXDelta="0" | |
android:toXDelta="100%p" /> | |
<alpha | |
android:duration="500" | |
android:fromAlpha="1.0" | |
android:toAlpha="0.1" /> | |
</set> |
fromXDelta:X軸方向的起始位置
toXDelta:X軸方向的結束位置
fromYDelta:Y軸方向的起始位置
toYDelta:Y軸方向的結束位置
以上屬性的值可以填入
(1) 數值 → 如果數值為50,代表在目前View的左上角加上50px後做為起點
(2) % → 如果為50%,代表在目前View的左上角加上自己寬度的50%後做為起點
(3) %p → 如果為50%p,代表在目前View的左上角加上父元件寬度的50%後做為起點
Step3 設定MainActivity.java檔
宣告ViewFlipper並新增addImgView、addMyView的方法,再將要可切換的view新增到ViewFlipper中
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity { | |
ViewFlipper vf; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
vf = (ViewFlipper) findViewById(R.id.vf); | |
//增加view | |
vf.addView(addImgView(R.drawable.img3)); | |
vf.addView(addImgView(R.drawable.img4)); | |
vf.addView(addImgView(R.drawable.img5)); | |
vf.addView(addMyView(R.layout.layout1)); | |
vf.addView(addMyView(R.layout.layout2)); | |
} | |
//addImgView方法(切換圖片) | |
private View addImgView(int id){ | |
ImageView iv = new ImageView(this); | |
iv.setImageResource(id); | |
return iv; | |
} | |
//addMyView方法(切換layout) | |
private View addMyView(int layout) { | |
LayoutInflater loi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
View vi = loi.inflate(layout,null); | |
return vi; | |
} | |
} |
手勢的判斷要繼承OnGestureListener方法。
(出現錯誤按下Alt+Enter,並匯入Method)
接著在onCreate中宣告GestureDetector方法,並在onFling的方法中加上判斷手勢方向的方法
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gd = new GestureDetector(this); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) { | |
//手勢往左滑 | |
if (motionEvent.getX() - motionEvent1.getX() > 120){ | |
vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in)); | |
vf.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_out)); | |
vf.showNext(); | |
return true; | |
} | |
//手勢往右滑 | |
else if(motionEvent.getX() - motionEvent1.getX() < -120){ | |
vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in)); | |
vf.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out)); | |
vf.showPrevious(); | |
return true; | |
} | |
return false; | |
} |
最後宣告onTouchEvent,如果沒宣告的話,會無法滑動
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public boolean onTouchEvent(MotionEvent event) { | |
return this.gd.onTouchEvent(event); | |
} |
完整MainActivity.java程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener{ | |
ViewFlipper vf; | |
GestureDetector gd; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
gd = new GestureDetector(this); | |
vf = (ViewFlipper) findViewById(R.id.vf); | |
//增加view | |
vf.addView(addImgView(R.drawable.img3)); | |
vf.addView(addImgView(R.drawable.img4)); | |
vf.addView(addImgView(R.drawable.img5)); | |
vf.addView(addMyView(R.layout.layout1)); | |
vf.addView(addMyView(R.layout.layout2)); | |
} | |
//addImgView方法(切換圖片) | |
private View addImgView(int id){ | |
ImageView iv = new ImageView(this); | |
iv.setImageResource(id); | |
return iv; | |
} | |
//addMyView方法(切換layout) | |
private View addMyView(int layout) { | |
LayoutInflater loi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); | |
View vi = loi.inflate(layout,null); | |
return vi; | |
} | |
@Override | |
public boolean onDown(MotionEvent motionEvent) { | |
return false; | |
} | |
@Override | |
public void onShowPress(MotionEvent motionEvent) { | |
} | |
@Override | |
public boolean onSingleTapUp(MotionEvent motionEvent) { | |
return false; | |
} | |
@Override | |
public boolean onScroll(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) { | |
return false; | |
} | |
@Override | |
public void onLongPress(MotionEvent motionEvent) { | |
} | |
@Override | |
public boolean onFling(MotionEvent motionEvent, MotionEvent motionEvent1, float v, float v1) { | |
//手勢往左滑 | |
if (motionEvent.getX() - motionEvent1.getX() > 120){ | |
vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_left_in)); | |
vf.setOutAnimation(AnimationUtils.loadAnimation(this,R.anim.push_left_out)); | |
vf.showNext(); | |
return true; | |
} | |
//手勢往右滑 | |
else if(motionEvent.getX() - motionEvent1.getX() < -120){ | |
vf.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_in)); | |
vf.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.push_right_out)); | |
vf.showPrevious(); | |
return true; | |
} | |
return false; | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent event) { | |
return this.gd.onTouchEvent(event); | |
} | |
} |
參考資料:启舰的博客
留言
張貼留言