Android实现滑动屏幕切换图片
作者:Code_MoMo 时间:2022-04-26 23:14:42
本文实例为大家分享了Android实现滑动屏幕切换图片的具体代码,供大家参考,具体内容如下
activity_main.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.example.administrator.hand_gliding.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:src="@drawable/a1"/>
</LinearLayout>
MainActivity.java 文件代码:
package com.example.administrator.hand_gliding;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
//定义图片
private int[] resId = new int[]{
R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,
R.drawable.a5,R.drawable.a6,R.drawable.a7
};
//图片下标序号
private int count = 0;
//定义手势监听对象
private GestureDetector gestureDetector;
//定义ImageView对象
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView)findViewById(R.id.imageView); //获取ImageView控件id
gestureDetector = new GestureDetector(onGestureListener); //设置手势监听由onGestureListener处理
}
//当Activity被触摸时回调
public boolean onTouchEvent(MotionEvent event){
return gestureDetector.onTouchEvent(event);
}
//自定义GestureDetector的手势识别 *
private GestureDetector.OnGestureListener onGestureListener
= new GestureDetector.SimpleOnGestureListener(){
//当识别的手势是滑动手势时回调onFinger方法
public boolean onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY){
//得到手触碰位置的起始点和结束点坐标 x , y ,并进行计算
float x = e2.getX()-e1.getX();
float y = e2.getY()-e1.getY();
//通过计算判断是向左还是向右滑动
if(x > 0){
count++;
count%=(resId.length-1); //想显示多少图片,就把定义图片的数组长度-1
}else if(x < 0){
count--;
count=(count+(resId.length-1))%(resId.length-1);
}
iv.setImageResource(resId[count]); //切换imageView的图片
return true;
}
};
}
界面设置效果:
这个功能的代码里有很多没见过的单词,本人英语学的不好,需要查查意思然后找这些方法的功能。
可以用这个加上切换动画做一个图片查看器。
由于没用模拟器,用的是真机调试,给不了滑动的实物图,抱歉抱歉。
来源:https://blog.csdn.net/Code_MoMo/article/details/78760862
标签:Android,滑动,切换图片
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java实现文件夹解压和压缩
2022-06-07 03:00:39
SpringBoot SSO轻松实现(附demo)
2022-04-05 02:24:33
Spring Boot2如何构建可部署的war包
2023-11-29 06:40:59
![](https://img.aspxhome.com/file/2023/5/60365_0s.png)
java中如何判断JSONObject是否存在某个Key
2022-06-10 15:07:24
![](https://img.aspxhome.com/file/2023/8/119148_0s.png)
c#使用xamarin编写拨打电话程序
2023-09-04 18:09:20
C#将布尔类型转换成字节数组的方法
2023-06-21 15:30:16
java 并发线程个数的如何确定
2022-01-01 21:52:13
Flutter如何轻松实现动态更新ListView浅析
2023-05-23 09:32:37
java读写ini文件、FileOutputStream问题
2023-11-29 08:50:13
![](https://img.aspxhome.com/file/2023/2/60482_0s.png)
执行java请求时导致在脚本执行结束时JVM无法退出
2023-11-25 05:57:13
关于Spring中的三级缓存解析
2022-08-20 15:31:06
![](https://img.aspxhome.com/file/2023/5/122965_0s.jpg)
Spring事务注解@Transactional失效的八种场景分析
2022-03-09 13:36:13
java.util.concurrent.ExecutionException 问题解决方法
2022-12-09 21:15:04
Android实现控制摄像头拍照
2022-06-03 02:14:06
![](https://img.aspxhome.com/file/2023/1/126781_0s.jpg)
C#使用base64对字符串进行编码和解码的测试
2022-09-21 23:08:59
![](https://img.aspxhome.com/file/2023/2/78112_0s.png)
C#编程实现四舍五入、向上及下取整的方法
2023-10-25 07:59:38
Java 8新特性方法引用详细介绍
2023-06-22 08:31:50
springboot连接不上redis的三种解决办法
2022-03-21 20:57:23
![](https://img.aspxhome.com/file/2023/3/67093_0s.png)
Java的RxJava库操作符的用法及实例讲解
2021-12-14 22:31:41
C#实现将一个字符串进行翻转显示的6种方法
2022-05-07 22:55:10
![](https://img.aspxhome.com/file/2023/5/102315_0s.png)