您好,登錄后才能下訂單哦!
用ViewFlipper與GestureDetector來實現圖片的左右滑動效果。
首先在布局xml中添加ViewFlipper控件:
<ViewFlipper android:id="@+id/flipper" android:layout_width="match_parent" android:layout_height="match_parent" > </ViewFlipper>
在Activity中實現GestureDetector的OnGestureListener監聽:
mGesture = new GestureDetector(this, this);
public boolean onDown(MotionEvent e) { return false; } public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { return false; } public void onLongPress(MotionEvent e) { } public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } public void onShowPress(MotionEvent e) { } public boolean onSingleTapUp(MotionEvent e) { return false; }
為ViewFilpper加入View,如圖片、文字等:
mFilpper = (ViewFlipper) findViewById(R.id.flipper); mFilpper.addView(addImageView(R.drawable.chrysanthemum)); mFilpper.addView(addImageView(R.drawable.desert)); mFilpper.addView(addImageView(R.drawable.ic_launcher)); mFilpper.addView(addImageView(R.drawable.koala)); mFilpper.addView(addImageView(R.drawable.penguins)); mFilpper.addView(addImageView(R.drawable.lighthouse)); mFilpper.addView(addText("你好"));
public ImageView addImageView(int resId) { ImageView p_w_picpath = new ImageView(this); p_w_picpath.setImageResource(resId); return p_w_picpath; } private View addText(String string) { TextView textView = new TextView(this); textView.setText(string); return textView; }
在onFling()中編寫滑動效果:
@Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Log.e("onFling", "onFling"); if (e1.getX() - e2.getX() > 120) { mFilpper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.left_in)); mFilpper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.left_out)); mFilpper.showPrevious(); } else if (e2.getX() - e1.getX() > 120) { mFilpper.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.rigth_in)); mFilpper.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.right_out)); mFilpper.showNext(); } return false; }
其中的left_in.xml如下:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha android:duration="1000" android:fromAlpha="0" android:toAlpha="1" /> <translate android:duration="1000" android:fromXDelta="100%p" android:toXDelta="0" /> </set>
left_out.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha android:duration="1000" android:fromAlpha="1" android:toAlpha="0" /> <translate android:duration="1000" android:fromXDelta="0" android:toXDelta="-100%p" /> </set>
right_in.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha android:duration="1000" android:fromAlpha="0" android:toAlpha="1" /> <translate android:duration="1000" android:fromXDelta="-100%p" android:toXDelta="0" /> </set>
right_out.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > <alpha android:duration="1000" android:fromAlpha="1" android:toAlpha="0" /> <translate android:duration="1000" android:fromXDelta="0" android:toXDelta="100%p" /> </set>
最后還要重寫onTouchEvent()方法在其中調用GestureDetector的onTouchEvent()方法:
@Override public boolean onTouchEvent(MotionEvent event) { Log.e("onTouchEvent", "onTouchEvent"); return mGesture.onTouchEvent(event); }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。