網(wǎng)站首頁 編程語言 正文
今天給大家實(shí)現(xiàn)的功能是類似于ViewFlipper的圖片滑動的效果,供大家參考,具體內(nèi)容如下
現(xiàn)在就直接上代碼吧!
代碼實(shí)例:
1、xml布局文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout ? ? xmlns:android="http://schemas.android.com/apk/res/android" ? ? xmlns:app="http://schemas.android.com/apk/res-auto" ? ? xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" ? ? android:layout_height="match_parent" tools:context="com.zking.laci.android20_shou.MainActivity"> ? ? <ViewFlipper ? ? ? ? android:layout_width="match_parent" ? ? ? ? android:layout_height="wrap_content" ? ? ? ? android:id="@+id/vf_main_image" ? ? ? ? ></ViewFlipper> </LinearLayout>
2、activity類
package com.zking.laci.android20_shou;
?
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.ViewFlipper;
?
public class MainActivity extends AppCompatActivity{
?
? ? private ViewFlipper vf_main_image;
? ? private int images[]={R.drawable.s10,R.drawable.s1,R.drawable.s7};
? ? private GestureDetector ges;
?
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? //得到控件
? ? ? ? vf_main_image = (ViewFlipper) findViewById(R.id.vf_main_image);
? ? ? ? for (int i = 0; i < images.length; i++) {
? ? ? ? ? ? ImageView iv=new ImageView(this);
? ? ? ? ? ? iv.setImageResource(images[i]);
? ? ? ? ? ? //將圖片防區(qū)ViewFlipper中
? ? ? ? ? ? vf_main_image.addView(iv);
? ? ? ? }
?
? ? ? ? //實(shí)例化一個手勢檢測器的類
? ? ? ? ges = new GestureDetector(this, new GestureDetector.OnGestureListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? /**
? ? ? ? ? ? ?* 按下
? ? ? ? ? ? ?*/
? ? ? ? ? ? public boolean onDown(MotionEvent e) {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
?
? ? ? ? ? ? /**
? ? ? ? ? ? ?* 按下但是沒有抬起
? ? ? ? ? ? ?* @param e
? ? ? ? ? ? ?*/
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onShowPress(MotionEvent e) {
?
? ? ? ? ? ? }
?
? ? ? ? ? ? /**
? ? ? ? ? ? ?* 按一下 短按
? ? ? ? ? ? ?* @param e
? ? ? ? ? ? ?* @return
? ? ? ? ? ? ?*/
? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean onSingleTapUp(MotionEvent e) {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
?
? ? ? ? ? ? /**
? ? ? ? ? ? ?* 滑動
? ? ? ? ? ? ?* @param e1
? ? ? ? ? ? ?* @param e2
? ? ? ? ? ? ?* @param distanceX
? ? ? ? ? ? ?* @param distanceY
? ? ? ? ? ? ?* @return
? ? ? ? ? ? ?*/
? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
?
? ? ? ? ? ? /**
? ? ? ? ? ? ?* 長按
? ? ? ? ? ? ?* @param e
? ? ? ? ? ? ?*/
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onLongPress(MotionEvent e) {
?
? ? ? ? ? ? }
?
? ? ? ? ? ? /**
? ? ? ? ? ? ?* 拖動
? ? ? ? ? ? ?* @param e1 ? ?起點(diǎn)
? ? ? ? ? ? ?* @param e2 ? ?終點(diǎn)
? ? ? ? ? ? ?* @param velocityX x值
? ? ? ? ? ? ?* @param velocityY ? ?Y值
? ? ? ? ? ? ?* @return
? ? ? ? ? ? ?*/
? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
? ? ? ? ? ? ? ? if(e1.getX()-e2.getX()>200){
? ? ? ? ? ? ? ? ? ? //顯示下一張
? ? ? ? ? ? ? ? ? ? vf_main_image.showNext();
? ? ? ? ? ? ? ? ? ? //設(shè)置出去的動畫
? ? ? ? ? ? ? ? ? ? vf_main_image.setOutAnimation(MainActivity.this,R.anim.left_out);
? ? ? ? ? ? ? ? ? ? //設(shè)置進(jìn)來的動畫
? ? ? ? ? ? ? ? ? ? vf_main_image.setInAnimation(MainActivity.this,R.anim.right_in);
? ? ? ? ? ? ? ? }else if(e2.getX()-e1.getX()>200){
? ? ? ? ? ? ? ? ? ? //顯示上一張
? ? ? ? ? ? ? ? ? ? vf_main_image.showPrevious();
? ? ? ? ? ? ? ? ? ? //設(shè)置出去的動畫
? ? ? ? ? ? ? ? ? ? vf_main_image.setOutAnimation(MainActivity.this,R.anim.right_out);
? ? ? ? ? ? ? ? ? ? //設(shè)置進(jìn)來的動畫
? ? ? ? ? ? ? ? ? ? vf_main_image.setInAnimation(MainActivity.this,R.anim.left_in);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? return false;
? ? ? ? ? ? }
? ? ? ? });
?
? ? }
?
? ? @Override
? ? public boolean onTouchEvent(MotionEvent event) {
? ? ? ? //讓觸摸繼續(xù)傳遞下去
? ? ? ? return ges.onTouchEvent(event);
? ? }
}
3、進(jìn)入和出去的四個動畫
left_out:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:duration="1000" ? ? > ? ? <translate ? ? ? ? android:fromXDelta="0" ? ? ? ? android:toXDelta="-100%p" ? ? ? ? android:fromYDelta="0" ? ? ? ? android:toYDelta="-100%p" ? ? ? ? ></translate> ? </set>
right_in:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:duration="1000" ? ? > ? ? <translate ? ? ? ? android:fromXDelta="100%p" ? ? ? ? android:toXDelta="0" ? ? ? ? android:fromYDelta="-100%p" ? ? ? ? android:toYDelta="0" ? ? ? ? ></translate> ? </set>
right_out:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:duration="1000" ? ? > ? ? <translate ? ? ? ? android:fromXDelta="0" ? ? ? ? android:toXDelta="100%p" ? ? ? ? android:fromYDelta="0" ? ? ? ? android:toYDelta="-100%p" ? ? ? ? ></translate> ? </set>
left_in:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:duration="1000" ? ? > ? ? <translate ? ? ? ? android:fromXDelta="-100%p" ? ? ? ? android:toXDelta="0" ? ? ? ? android:fromYDelta="-100%p" ? ? ? ? android:toYDelta="0" ? ? ? ? ></translate> ? </set>
原文鏈接:https://blog.csdn.net/pang_ping/article/details/74936533
相關(guān)推薦
- 2022-07-15 SQL?Server中執(zhí)行動態(tài)SQL_MsSql
- 2022-05-31 詳解Docker?Compose配置文件參數(shù)_docker
- 2023-12-10 記錄一次多數(shù)據(jù)源配置失效的情況
- 2022-03-20 Android國際化之中英文語言切換_Android
- 2022-06-12 python實(shí)現(xiàn)微信小程序的多種支付方式_python
- 2022-06-10 Python?Pytorch學(xué)習(xí)之圖像檢索實(shí)踐_python
- 2022-05-08 Python與C語言分別完成排序流程_python
- 2022-08-05 C語言簡明介紹常見關(guān)鍵字的用法_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支