国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統(tǒng) > Android > 正文

Android實(shí)現(xiàn)滑動(dòng)屏幕切換圖片

2019-12-12 00:32:51
字體:
供稿:網(wǎng)友

本文實(shí)例為大家分享了Android實(shí)現(xiàn)滑動(dòng)屏幕切換圖片的具體代碼,供大家參考,具體內(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)序號(hào)  private int count = 0;  //定義手勢監(jiān)聽對(duì)象  private GestureDetector gestureDetector;  //定義ImageView對(duì)象  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的手勢識(shí)別監(jiān)聽器  private GestureDetector.OnGestureListener onGestureListener      = new GestureDetector.SimpleOnGestureListener(){    //當(dāng)識(shí)別的手勢是滑動(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ì)算判斷是向左還是向右滑動(dòng)      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è)加上切換動(dòng)畫做一個(gè)圖片查看器。

由于沒用模擬器,用的是真機(jī)調(diào)試,給不了滑動(dòng)的實(shí)物圖,抱歉抱歉。

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 塔河县| 恩施市| 平江县| 章丘市| 贞丰县| 潜江市| 年辖:市辖区| 汕头市| 米易县| 赞皇县| 贡山| 鄂托克旗| 炉霍县| 城市| 岳普湖县| 厦门市| 河源市| 剑川县| 临颍县| 克什克腾旗| 西平县| 双辽市| 连平县| 梁山县| 达州市| 赤峰市| 肥西县| 建阳市| 襄城县| 桐庐县| 如皋市| 调兵山市| 韶山市| 逊克县| 长子县| 大竹县| 小金县| 湘阴县| 金寨县| 老河口市| 登封市|