ViewFlipper的使用
看一下類的繼承結(jié)構(gòu),我們可以明白很多。
它不僅擁有從父類繼承的字段、屬性以及xml屬性,他還有自己的兩個(gè)xml屬性:
1.android:autoStart? 是否開啟自動(dòng)開始動(dòng)畫,值為true或false
2.android:flipInterval? 滑動(dòng)的時(shí)間間隔
四、公共方法
public bool isAutoStart ()
如果視圖顯示到窗口上時(shí)會(huì)自動(dòng)調(diào)用startFlipping()方法,則返回true
public bool isFlipping()
如果子視圖正在切換,則返回true
public bool setAutoStart (bool autoStart)
設(shè)置視圖顯示到窗口上時(shí)是否會(huì)自動(dòng)調(diào)用startFlipping()方法
public bool setFlipInterval (int milliseconds)
視圖間切換的時(shí)間間隔
參數(shù)
milliseconds ???毫秒數(shù)
public bool startFlipping ()
開始在子視圖間定時(shí)循環(huán)切換
public bool stopFlipping ()
停止切換
通過查看 API文檔可以發(fā)現(xiàn),android.widget.ViewAnimator類繼承至FrameLayout,ViewAnimator類的作用是為FrameLayout里面的View切換提供動(dòng)畫效果。該類有如下幾個(gè)和動(dòng)畫相關(guān)的函數(shù):
setInAnimation:設(shè)置 View進(jìn)入屏幕時(shí)候使用的動(dòng)畫,該函數(shù)有兩個(gè)版本,一個(gè)接受單個(gè)參數(shù),類型為android.view.animation.Animation;一個(gè)接受兩個(gè)參數(shù),類型為Context和int,分別為Context對(duì)象和定義Animation的resourceID。
setOutAnimation: 設(shè)置View退出屏幕時(shí)候使用的動(dòng)畫,參數(shù)setInAnimation函數(shù)一樣。
showNext: 調(diào)用該函數(shù)來顯示FrameLayout里面的下一個(gè)View。
showPrevious: 調(diào)用該函數(shù)來顯示FrameLayout里面的上一個(gè)View。
一般不直接使用 ViewAnimator而是使用它的兩個(gè)子類ViewFlipper和ViewSwitcher。
ViewFlipper可以用來指定FrameLayout內(nèi)多個(gè)View之間的切換效果,可以一次指定也可以每次切換的時(shí)候都指定單獨(dú)的效果。該類額外提供了上面所示的幾個(gè)函數(shù)。ViewFlipper是繼承至FrameLayout的,所以它是一個(gè)Layout里面可以放置多個(gè)View。在示例中定義一個(gè)ViewFlipper,里面包含三View作為示例的三個(gè)屏幕,每個(gè)View中包含一張圖片。
xml布局如下:
android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > android:id="@+id/details" android:layout_width="fill_parent" android:layout_height="fill_parent" android:flipInterval="1000" android:inAnimation="@anim/push_left_in" android:outAnimation="@anim/push_left_out" android:persistentDrawingCache="animation" > android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > android:id="@+id/image1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:src="http://m.bai1xia.com/news/zb_users/upload/2022/05/20220530200002_38170." android:toXDelta="0" /> android:duration="500" android:fromAlpha="0.0" android:toAlpha="1.0" /> android:duration="500" android:fromXDelta="0" android:toXDelta="-100%p" /> android:duration="500" android:fromAlpha="1.0" android:toAlpha="0.0" /> 這里注意一下,我們這用的是left,所以動(dòng)畫是從右向左的,當(dāng)然你也可以設(shè)置從左向右。 主Activity代碼: public class ViewFlipperDemo extends Activity { private ViewFlipper flipper; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view_flipper_demo); flipper=(ViewFlipper) findViewById(R.id.details); flipper.setAutoStart(true);//自動(dòng)開始,不然不會(huì)開始。 } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_view_flipper_demo, menu); return true; } } GestureDetector.OnDoubleTapListener:用來通知 DoubleTap事件,類似于鼠標(biāo)的雙擊事件,該接口有如下三個(gè)回調(diào)函數(shù): 1.???onDoubleTap(MotionEvent e):通知 DoubleTap手勢(shì), 2.???onDoubleTapEvent(MotionEvent e):通知 DoubleTap手勢(shì)中的事件,包含down、up和move事件(這里指的是在雙擊之間發(fā)生的事件,例如在同一個(gè)地方雙擊會(huì)產(chǎn)生DoubleTap手勢(shì),而在DoubleTap手勢(shì)里面還會(huì)發(fā)生down和up事件,這兩個(gè)事件由該函數(shù)通知); 3.???onSingleTapConfirmed(MotionEvent e):用來判定該次點(diǎn)擊是 SingleTap而不是DoubleTap,如果連續(xù)點(diǎn)擊兩次就是DoubleTap手勢(shì),如果只點(diǎn)擊一次,OPhone系統(tǒng)等待一段時(shí)間后沒有收到第二次點(diǎn)擊則判定該次點(diǎn)擊為SingleTap而不是DoubleTap,然后觸發(fā)SingleTapConfirmed事件。 GestureDetector.OnGestureListener:用來通知普通的手勢(shì)事件,該接口有如下六個(gè)回調(diào)函數(shù): 1.???onDown(MotionEvent e): down事件; 2.???onSingleTapUp(MotionEvent e):一次點(diǎn)擊 up事件; 3.???onShowPress(MotionEvent e): down事件發(fā)生而move或則up還沒發(fā)生前觸發(fā)該事件; 4.???onLongPress(MotionEvent e):長(zhǎng)按事件; 5.???onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY):滑動(dòng)手勢(shì)事件; 6.???onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY):在屏幕上拖動(dòng)事件。 在上述事件中,如果在程序中處理的該事件就返回 true否則返回false,在GestureDetector中也定義了一個(gè)SimpleOnGestureListener類,這是個(gè)助手類,實(shí)現(xiàn)了上述的所有函數(shù)并且都返回false。如果在項(xiàng)目中只需要監(jiān)聽某個(gè)事件繼承這個(gè)類可以少些幾個(gè)空回調(diào)函數(shù)。 要走上面的程序中添加滑動(dòng)手勢(shì)來實(shí)現(xiàn)屏幕切換的話,首先需要定義一個(gè)GestureDetector: private GestureDetector mGestureDetector; 并在onCreate函數(shù)初始化: mGestureDetector = new GestureDetector(this); 參數(shù)是OnGestureListener,然后讓Activity實(shí)現(xiàn) OnGestureListener 和OnDoubleTapListener接口以及OnTouchListener: public class ViewFlipperDemo extends Activity implements OnGestureListener, OnDoubleTapListener,OnTouchListener { private ViewFlipper flipper; private GestureDetector gestureDetector; private Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_view_flipper_demo); flipper = (ViewFlipper) findViewById(R.id.details); gestureDetector = new GestureDetector(this);//注冊(cè)一個(gè)用于手勢(shì)識(shí)別的類 // flipper.setAutoStart(true); flipper.setOnTouchListener(this); flipper.setLongClickable(true); //允許長(zhǎng)按住ViewFlipper,這樣才能識(shí)別拖動(dòng)等手勢(shì) button=(Button) findViewById(R.id.button1); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { //flipper.showNext(); Intent intent=new Intent(ViewFlipperDemo.this,TestActivity.class); startActivity(intent);//進(jìn)入其他Activity,沒有問題 } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_view_flipper_demo, menu); return true; } public boolean onSingleTapConfirmed(MotionEvent e) { // TODO Auto-generated method stub return false; } public boolean onDoubleTap(MotionEvent e) { // TODO Auto-generated method stub return false; } /* * 然后在onDoubleTap中實(shí)現(xiàn)雙擊自動(dòng)切換的效果,再次雙擊則停止: * * @see * android.view.GestureDetector.OnDoubleTapListener#onDoubleTapEvent(android * .view.MotionEvent) */ public boolean onDoubleTapEvent(MotionEvent e) { if (flipper.isFlipping()) { flipper.stopFlipping(); } else { flipper.startFlipping(); } return true; } public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub return false; } public void onShowPress(MotionEvent e) { // TODO Auto-generated method stub } public boolean onSingleTapUp(MotionEvent e) { // TODO Auto-generated method stub return false; } public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub return false; } public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub } /* * 這里實(shí)現(xiàn)的功能是從右往左滑動(dòng)則切換到上一個(gè)View,從左往右滑動(dòng)則切換到下一個(gè)View,并且使用不同的in、out 動(dòng)畫使切換效果看起來統(tǒng)一一些。 * * @see android.view.GestureDetector.OnGestureListener#onFling(android.view. * MotionEvent, android.view.MotionEvent, float, float) */ public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (e1.getX()>e2.getX()) { flipper.showNext(); } else if (e1.getX() flipper.setInAnimation(getApplicationContext(), R.anim.push_right_in); flipper.setOutAnimation(getApplicationContext(), R.anim.push_right_out); flipper.showPrevious(); flipper.setInAnimation(getApplicationContext(), R.anim.push_left_in); flipper.setOutAnimation(getApplicationContext(), R.anim.push_left_out); } else { return false; } return true; } public boolean onTouch(View v, MotionEvent event) { return this.gestureDetector.onTouchEvent(event); } } 。 Android
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。