網站首頁 編程語言 正文
本文實例為大家分享了android viewflipper實現左右滑動切換顯示圖片的具體代碼,供大家參考,具體內容如下
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);
? ? }
}
原文鏈接:https://blog.csdn.net/a107494639/article/details/7548662
相關推薦
- 2022-07-09 使用cgroup控制cpu、內存、IO資源實踐
- 2023-01-19 利用Python構建Flutter應用的教程詳解_python
- 2022-06-01 Python+Opencv實現計算閉合區域面積_python
- 2022-04-08 python如何去除異常值和缺失值的插值_python
- 2022-08-01 grpool?goroutine池協程管理_Golang
- 2023-10-12 手寫導航欄遇到的問題,opacity占位,display沒動畫效果,已全部解決
- 2022-08-23 C++詳解使用floor&ceil&round實現保留小數點后兩位_C 語言
- 2022-09-18 C#?中的partial?關鍵字詳解_C#教程
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支