本文執行個體講述了Android中ViewFlipper的使用及設定動畫效果。分享給大家供大家參考,具體如下:
說到左右滑動,其實實現左右滑動的方式很多,有ViewPaer,自訂實現Viewgroup,gallery等都可以達到這種效果。這裡做下ViewFliper實現左右滑動的效果。
會用到以下的技術:
1、ViewFlipper
2、GestureDetector
3、Animation
主要是這三個類在起作用。
ViewFlipper,不妨把它看做一個容器吧,你可以把許多的View放在這個容器中,讓它展示給使用者,雖然它每次只展示一個view,我感覺它的用途更好是作為廣告展示,比如類似購物網站那樣的廣告滾動展示。比如可以設定自動播放功能,就可以滾動廣告了!
原理:向左向右滑動主要是依賴手勢來控制,手勢向右滑動就調用 viewFlipper.showNext()方法,同理,向左滑動就會去調用viewFlipper.showPrevious()方法。
效果圖如下:
接下來直接上代碼:
首先在布局檔案中添加ViewFlipper的標籤:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/pattern1" > <Button android:id="@+id/treasure_chest_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:background="@drawable/treasure" /> <ViewFlipper android:id="@+id/viewflipper" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="@id/treasure_chest_btn" > </ViewFlipper> <Button android:id="@+id/garbage_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@id/viewflipper" android:background="@drawable/garbage" /> </RelativeLayout>
其次是在anim中定義動畫:
slide_left_in.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 畫面轉換位置移動動畫效果 開始向左動畫 fromXDelta : 動畫開始時 X座標位置 toXDelta :動畫結束時 X座標位置 duration :動畫期間 --> <translate android:duration="800" android:fromXDelta="100%p" android:toXDelta="0" /> </set>
slide_left_out.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="800" android:fromXDelta="0" android:toXDelta="-100%p" /> </set>
slide_right_in.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="800" android:fromXDelta="-100%p" android:toXDelta="0" /> </set>
slide_right_out.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <translate android:duration="800" android:fromXDelta="0" android:toXDelta="100%p" /> </set>
最後是Activity中的實現:
package net.loonggg.demo.slider; import android.app.Activity; import android.os.Bundle; import android.view.GestureDetector; import android.view.GestureDetector.OnGestureListener; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.view.Window; import android.view.animation.Animation; import android.view.animation.Animation.AnimationListener; import android.view.animation.AnimationSet; import android.view.animation.AnimationUtils; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.ViewFlipper; public class MainActivity extends Activity implements OnGestureListener, OnTouchListener { private GestureDetector detector; private ViewFlipper myViewFlipper; private int[] imgs = { R.drawable.img0, R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5 }; private int displayedChildIndex = 0; private Button treasure_btn; private Button garbage_btn; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main); myViewFlipper = (ViewFlipper) findViewById(R.id.viewflipper); treasure_btn = (Button) findViewById(R.id.treasure_chest_btn); garbage_btn = (Button) findViewById(R.id.garbage_btn); myViewFlipper.setOnTouchListener(this); myViewFlipper.setLongClickable(true);// 設定長按事件 // myViewFlipper.setAutoStart(true);// 設定是否自動播放,預設不自動播放 detector = new GestureDetector(this); addFlipperView(); } /** * 向FlipperView中動態添加View */ private void addFlipperView() { for (int i = 0; i < imgs.length; i++) { View view = LayoutInflater.from(this).inflate( R.layout.myviewflipper, null); TextView title = (TextView) view.findViewById(R.id.view_title); title.setText("頭像"); ImageView iv = (ImageView) view.findViewById(R.id.view_image); iv.setBackgroundResource(imgs[i]); myViewFlipper.addView(view); } } @Override public boolean onDown(MotionEvent e) { return false; } @Override public void onShowPress(MotionEvent e) { } @Override public boolean onSingleTapUp(MotionEvent e) { return false; } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } @Override public void onLongPress(MotionEvent e) { } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { // 返回當前正在顯示的子視圖的索引 displayedChildIndex = myViewFlipper.getDisplayedChild(); final ImageView iv = (ImageView) myViewFlipper.getChildAt( displayedChildIndex).findViewById(R.id.view_image); if (e2.getX() - e1.getX() >= 100 && e1.getY() - e2.getY() >= 100) { AnimationSet outAnim = (AnimationSet) AnimationUtils.loadAnimation( this, R.anim.collect_treasure); iv.startAnimation(outAnim); outAnim.setAnimationListener(new AnimationListener() { public void onAnimationStart(Animation animation) { } public void onAnimationRepeat(Animation animation) { } public void onAnimationEnd(Animation animation) { showNextView(); AnimationSet downAnim = (AnimationSet) AnimationUtils .loadAnimation(MainActivity.this, R.anim.up_or_down); treasure_btn.setAnimation(downAnim); iv.clearAnimation(); } }); } else if (e1.getX() - e2.getX() >= 100 && e2.getY() - e1.getY() >= 100) { AnimationSet outAnim = (AnimationSet) AnimationUtils.loadAnimation( this, R.anim.throw_garbage); iv.startAnimation(outAnim); outAnim.setAnimationListener(new AnimationListener() { public void onAnimationStart(Animation animation) { } public void onAnimationRepeat(Animation animation) { } public void onAnimationEnd(Animation animation) { showNextView(); AnimationSet leftAnim = (AnimationSet) AnimationUtils .loadAnimation(MainActivity.this, R.anim.left_or_right); garbage_btn.setAnimation(leftAnim); iv.clearAnimation(); } }); } else if (e1.getX() - e2.getX() >= 100) { showNextView(); } else if (e2.getX() - e1.getX() >= 100) { showPreviousView(); } return false; } private void showPreviousView() { myViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_right_in)); myViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_right_out)); myViewFlipper.showPrevious(); } private void showNextView() { myViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_left_in)); myViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.slide_left_out)); myViewFlipper.showNext(); } @Override public boolean onTouch(View v, MotionEvent event) { return detector.onTouchEvent(event); } }
希望本文所述對大家Android程式設計有所協助。