您好,登錄后才能下訂單哦!
這篇文章主要介紹“Android如何實現(xiàn)滑動屏幕切換圖片的方法”,在日常操作中,相信很多人在Android如何實現(xiàn)滑動屏幕切換圖片的方法問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Android如何實現(xiàn)滑動屏幕切換圖片的方法”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
具體內(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被觸摸時回調(diào) public boolean onTouchEvent(MotionEvent event){ return gestureDetector.onTouchEvent(event); } //自定義GestureDetector的手勢識別監(jiān)聽器 private GestureDetector.OnGestureListener onGestureListener = new GestureDetector.SimpleOnGestureListener(){ //當(dāng)識別的手勢是滑動手勢時回調(diào)onFinger方法 public boolean onFling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY){ //得到手觸碰位置的起始點和結(jié)束點坐標(biāo) x , y ,并進行計算 float x = e2.getX()-e1.getX(); float y = e2.getY()-e1.getY(); //通過計算判斷是向左還是向右滑動 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è)置效果:
這個功能的代碼里有很多沒見過的單詞,本人英語學(xué)的不好,需要查查意思然后找這些方法的功能。
可以用這個加上切換動畫做一個圖片查看器。
由于沒用模擬器,用的是真機調(diào)試,給不了滑動的實物圖,抱歉抱歉。
到此,關(guān)于“Android如何實現(xiàn)滑動屏幕切換圖片的方法”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。