android?viewflipper實現(xiàn)左右滑動切換顯示圖片
本文實例為大家分享了android viewflipper實現(xiàn)左右滑動切換顯示圖片的具體代碼,供大家參考,具體內容如下
1.首先定義四個動畫文件,表示當view切換的時候的顯示效果
in_leftright.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > ? ? ? <translate ? ? ? ? android:duration="500" ? ? ? ? android:fromXDelta="0" ? ? ? ? android:toXDelta="-100%p" /> ? </set>
in_rightleft.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > ? ? ? <translate ? ? ? ? android:duration="500" ? ? ? ? android:fromXDelta="100%p" ? ? ? ? android:toXDelta="0" /> ? </set>
out_leftright.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > ? ? ? <translate ? ? ? ? android:duration="500" ? ? ? ? android:fromXDelta="0" ? ? ? ? android:toXDelta="100%p" /> ? </set>
out_rightleft.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" > ? ? ? <translate ? ? ? ? android:duration="500" ? ? ? ? android:fromXDelta="0" ? ? ? ? android:toXDelta="-100%p" /> ? </set>
2.在main.xml中添加ViewFlipper控件,里面放三個LinearLayout,表示3個view
<?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="match_parent" ? ? ? ? android:layout_height="match_parent" > ? ? ? ? ? <!-- first page --> ? ? ? ? ? <LinearLayout ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? android:gravity="center" > ? ? ? ? ? ? ? <ImageView ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:layout_gravity="center" ? ? ? ? ? ? ? ? android:src="@drawable/a001" /> ? ? ? ? </LinearLayout> ? ? ? ? ? <!-- second page --> ? ? ? ? ? <LinearLayout ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? android:gravity="center" > ? ? ? ? ? ? ? <ImageView ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:layout_gravity="center" ? ? ? ? ? ? ? ? android:src="@drawable/a002" /> ? ? ? ? </LinearLayout> ? ? ? ? ? <!-- third page --> ? ? ? ? ? <LinearLayout ? ? ? ? ? ? android:layout_width="match_parent" ? ? ? ? ? ? android:layout_height="match_parent" ? ? ? ? ? ? android:gravity="center" > ? ? ? ? ? ? ? <ImageView ? ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? ? android:layout_gravity="center" ? ? ? ? ? ? ? ? android:src="@drawable/a003" /> ? ? ? ? </LinearLayout> ? ? ? </ViewFlipper> ? </LinearLayout>
3.最后在activity里的onTouchEvent事件中,來判斷是往哪個方向移動
package org.example.viewflipper;
?
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ViewFlipper;
?
public class ViewFlipperActivity extends Activity {
?
? ? private ViewFlipper viewFlipper = null;
?
? ? float startX;
?
? ? @Override
? ? public void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.main);
? ? ? ? // init widget
? ? ? ? viewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper);
? ? }
?
? ? @Override
? ? public boolean onTouchEvent(MotionEvent event) {
? ? ? ? switch (event.getAction()) {
? ? ? ? case MotionEvent.ACTION_DOWN:
? ? ? ? ? ? startX = event.getX();
? ? ? ? ? ? break;
? ? ? ? case MotionEvent.ACTION_UP:
? ? ? ? ? ? if (event.getX() > startX) {// flip to right
? ? ? ? ? ? ? ? viewFlipper.setInAnimation(this, R.anim.in_leftright);
? ? ? ? ? ? ? ? viewFlipper.setOutAnimation(this, R.anim.out_leftright);
? ? ? ? ? ? ? ? viewFlipper.showNext();
? ? ? ? ? ? } else {// flip to left
? ? ? ? ? ? ? ? viewFlipper.setInAnimation(this, R.anim.in_rightleft);
? ? ? ? ? ? ? ? viewFlipper.setOutAnimation(this, R.anim.out_rightleft);
? ? ? ? ? ? ? ? viewFlipper.showPrevious();
? ? ? ? ? ? }
?
? ? ? ? }
? ? ? ? return super.onTouchEvent(event);
? ? }
}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
- Android實現(xiàn)界面左右滑動切換功能
- Android App中使用ViewPager+Fragment實現(xiàn)滑動切換效果
- Android開發(fā)之使用ViewPager實現(xiàn)圖片左右滑動切換效果
- Android實現(xiàn)簡單底部導航欄 Android仿微信滑動切換效果
- Android實現(xiàn)滑動屏幕切換圖片
- Android編程實現(xiàn)ViewPager多頁面滑動切換及動畫效果的方法
- Android實現(xiàn)微信首頁左右滑動切換效果
- android實現(xiàn)切換日期左右無限滑動效果
- Android使用TabLayou+fragment+viewpager實現(xiàn)滑動切換頁面效果
- Android Listview上下拉動刷新tab滑動切換功能
相關文章
Android多功能視頻播放器GSYVideoPlayer開發(fā)流程
怎么在Android中實現(xiàn)GSYVideoPlayer視頻播放器?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲2022-11-11
Android WebView使用方法詳解 附js交互調用方法
這篇文章主要為大家詳細介紹了Android WebView使用方法詳解,文中附js交互調用方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-05-05

