您好,登錄后才能下訂單哦!
用ViewFlipper與GestureDetector來(lái)實(shí)現(xiàn)圖片的左右滑動(dòng)效果。
首先在布局xml中添加ViewFlipper控件:
<ViewFlipper android:id="@+id/flipper" android:layout_width="match_parent" android:layout_height="match_parent" > </ViewFlipper>
在Activity中實(shí)現(xiàn)GestureDetector的OnGestureListener監(jiān)聽(tīng):
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()中編寫(xiě)滑動(dòng)效果:
@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>
最后還要重寫(xiě)onTouchEvent()方法在其中調(diào)用GestureDetector的onTouchEvent()方法:
@Override public boolean onTouchEvent(MotionEvent event) { Log.e("onTouchEvent", "onTouchEvent"); return mGesture.onTouchEvent(event); }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。