網站首頁 編程語言 正文
本文實例為大家分享了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
相關推薦
- 2023-07-30 el-selete改變值后選中的內容不變
- 2022-08-06 Qt編寫顯示密碼強度的控件_C 語言
- 2023-05-07 numpy中數組的堆疊方法_python
- 2022-11-14 Go語言官方依賴注入工具Wire的使用教程_Golang
- 2022-08-31 C++淺析引用的定義與使用_C 語言
- 2022-01-21 什么是前端開發?什么是后端開發?
- 2022-03-10 Android?APP啟動時間優化介紹_Android
- 2022-09-14 Redis?緩存淘汰策略和事務實現樂觀鎖詳情_Redis
- 最近更新
-
- 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同步修改后的遠程分支