Android中利用ViewFliper實現螢幕轉場效果

來源:互聯網
上載者:User

本樣本是用ViewFliper實現螢幕切換圖片的效果,手指滑動就實現圖片的切換,主要實現效果如下:


程式結構圖:


ViewFliper中的代碼

package com.scnu.mc.myviewfliper;import android.app.Activity;import android.os.Bundle;import android.view.MotionEvent;import android.widget.ViewFlipper;public class ViewFliper extends Activity {ViewFlipper viewFlipper = null;float startX;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);init();}private void init() {viewFlipper = (ViewFlipper) this.findViewById(R.id.viewFlipper);}@Overridepublic boolean onTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:startX = event.getX();break;case MotionEvent.ACTION_UP:if (event.getX() > startX) { // 向右滑動viewFlipper.setInAnimation(this, R.anim.in_left2right);viewFlipper.setOutAnimation(this, R.anim.out_left2right);viewFlipper.showNext();} else if (event.getX() < startX) { // 向左滑動viewFlipper.setInAnimation(this, R.anim.in_right2left);viewFlipper.setOutAnimation(this, R.anim.out_right2left);viewFlipper.showPrevious();}break;}return super.onTouchEvent(event);}}

main.xml代碼

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent" android:layout_height="fill_parent"android:orientation="vertical"><ViewFlipper android:id="@+id/viewFlipper"android:layout_width="fill_parent" android:layout_height="fill_parent"><!-- 第一個頁面 --><LinearLayout android:layout_width="fill_parent"android:layout_height="fill_parent" android:gravity="center"><ImageView android:layout_width="wrap_content"android:layout_height="wrap_content" android:src="@drawable/a1" /></LinearLayout><!-- 第二個頁面 --><LinearLayout android:layout_width="fill_parent"android:layout_height="fill_parent" android:gravity="center"><ImageView android:layout_width="wrap_content"android:layout_height="wrap_content" android:src="@drawable/a2"android:gravity="center" /></LinearLayout><!-- 第三個頁面 --><LinearLayout android:layout_width="fill_parent"android:layout_height="fill_parent" android:gravity="center"><ImageView android:layout_width="wrap_content"android:layout_height="wrap_content" android:src="@drawable/a3"android:gravity="center" /></LinearLayout><!-- 第四個頁面 --><LinearLayout android:layout_width="fill_parent"android:layout_height="fill_parent" android:gravity="center"><ImageView android:layout_width="wrap_content"android:layout_height="wrap_content" android:src="@drawable/a4"android:gravity="center" /></LinearLayout></ViewFlipper></LinearLayout>

4個動畫效果的xml檔案

in_left2right----從左至右進入螢幕

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="30"        android:fromXDelta="-100%p"        android:toXDelta="0" /></set>

in_right2left----從右至左進入螢幕

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="30"        android:fromXDelta="100%p"        android:toXDelta="0" /></set>

out_left2right----從左至右退出螢幕

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="30"        android:fromXDelta="0"        android:toXDelta="100%p" /></set>

out_right2left----從右至左退出螢幕

<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" >    <translate        android:duration="30"        android:fromXDelta="0"        android:toXDelta="-100%p" /></set>

註:fromXDelta和toXDelta代表控制項相對應parent的位移距離,個人理解是"100%p"代表控制項向右跑到螢幕的外面,"-100%p"代表控制項向左跑到螢幕的外部。下面附上個人理解的圖。



相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.