Android實(shí)現(xiàn)滑動屏幕切換圖片
本文實(shí)例為大家分享了Android實(shí)現(xiàn)滑動屏幕切換圖片的具體代碼,供大家參考,具體內(nèi)容如下
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
};
//圖片下標(biāo)序號
private int count = 0;
//定義手勢監(jiān)聽對象
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); //設(shè)置手勢監(jiān)聽由onGestureListener處理
}
//當(dāng)Activity被觸摸時(shí)回調(diào)
public boolean onTouchEvent(MotionEvent event){
return gestureDetector.onTouchEvent(event);
}
//自定義GestureDetector的手勢識別監(jiān)聽器
private GestureDetector.OnGestureListener onGestureListener
= new GestureDetector.SimpleOnGestureListener(){
//當(dāng)識別的手勢是滑動手勢時(shí)回調(diào)onFinger方法
public boolean onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY){
//得到手觸碰位置的起始點(diǎn)和結(jié)束點(diǎn)坐標(biāo) x , y ,并進(jìn)行計(jì)算
float x = e2.getX()-e1.getX();
float y = e2.getY()-e1.getY();
//通過計(jì)算判斷是向左還是向右滑動
if(x > 0){
count++;
count%=(resId.length-1); //想顯示多少圖片,就把定義圖片的數(shù)組長度-1
}else if(x < 0){
count--;
count=(count+(resId.length-1))%(resId.length-1);
}
iv.setImageResource(resId[count]); //切換imageView的圖片
return true;
}
};
}
界面設(shè)置效果:

這個(gè)功能的代碼里有很多沒見過的單詞,本人英語學(xué)的不好,需要查查意思然后找這些方法的功能。
可以用這個(gè)加上切換動畫做一個(gè)圖片查看器。
由于沒用模擬器,用的是真機(jī)調(diào)試,給不了滑動的實(shí)物圖,抱歉抱歉。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)界面左右滑動切換功能
- Android App中使用ViewPager+Fragment實(shí)現(xiàn)滑動切換效果
- Android開發(fā)之使用ViewPager實(shí)現(xiàn)圖片左右滑動切換效果
- Android實(shí)現(xiàn)簡單底部導(dǎo)航欄 Android仿微信滑動切換效果
- Android編程實(shí)現(xiàn)ViewPager多頁面滑動切換及動畫效果的方法
- Android實(shí)現(xiàn)微信首頁左右滑動切換效果
- android實(shí)現(xiàn)切換日期左右無限滑動效果
- Android使用TabLayou+fragment+viewpager實(shí)現(xiàn)滑動切換頁面效果
- Android Listview上下拉動刷新tab滑動切換功能
- android?viewflipper實(shí)現(xiàn)左右滑動切換顯示圖片
相關(guān)文章
Android中oncreate中獲得控件高度或?qū)挾鹊膶?shí)現(xiàn)方法
這篇文章主要介紹了Android中oncreate中獲得控件高度或?qū)挾鹊膶?shí)現(xiàn)方法的相關(guān)資料,希望通過本文大家能實(shí)現(xiàn)這樣的功能,需要的朋友可以參考下2017-09-09
解決Android popupWindow設(shè)置背景透明度無效的問題
這篇文章主要介紹了解決Android popupWindow設(shè)置背景透明度無效的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
Android編程實(shí)現(xiàn)長按彈出選項(xiàng)框View進(jìn)行操作的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)長按彈出選項(xiàng)框View進(jìn)行操作的方法,結(jié)合實(shí)例形式分析了Android事件響應(yīng)及彈窗的功能、布局相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
Android開發(fā)實(shí)現(xiàn)的自動換圖片、輪播圖效果示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)的自動換圖片、輪播圖效果,涉及Android ImageView及界面布局相關(guān)操作技巧,需要的朋友可以參考下2019-03-03
Android實(shí)現(xiàn)用代碼簡單安裝和卸載APK的方法
這篇文章主要介紹了Android實(shí)現(xiàn)用代碼簡單安裝和卸載APK的方法,涉及Android針對APK文件及package的相關(guān)操作技巧,需要的朋友可以參考下2016-08-08
Android 滑動小圓點(diǎn)ViewPager的兩種設(shè)置方法詳解流程
Viewpager,視圖翻頁工具,提供了多頁面切換的效果。Android 3.0后引入的一個(gè)UI控件,位于v4包中。低版本使用需要導(dǎo)入v4包,現(xiàn)在我們一般不再兼容3.0及以下版本,另外使用Android studio開發(fā),默認(rèn)導(dǎo)入v7包,v7包含了v4,所以不用導(dǎo)包,越來越方便了2021-11-11

