欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2020年04月08日 11:03:37   作者:Code_MoMo  
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)滑動(dòng)屏幕切換圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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;
 //定義手勢(shì)監(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è)置手勢(shì)監(jiān)聽由onGestureListener處理
 
 }
 
 //當(dāng)Activity被觸摸時(shí)回調(diào)
 public boolean onTouchEvent(MotionEvent event){
 return gestureDetector.onTouchEvent(event);
 }
 //自定義GestureDetector的手勢(shì)識(shí)別監(jiān)聽器
 private GestureDetector.OnGestureListener onGestureListener
  = new GestureDetector.SimpleOnGestureListener(){
 //當(dāng)識(shí)別的手勢(shì)是滑動(dòng)手勢(shì)時(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ù)組長(zhǎng)度-1
  }else if(x < 0){
  count--;
  count=(count+(resId.length-1))%(resId.length-1);
  }
 
  iv.setImageResource(resId[count]); //切換imageView的圖片
  return true;
 }
 };
}

界面設(shè)置效果:

這個(gè)功能的代碼里有很多沒見過的單詞,本人英語(yǔ)學(xué)的不好,需要查查意思然后找這些方法的功能。

可以用這個(gè)加上切換動(dòng)畫做一個(gè)圖片查看器。

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論