日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android中通过ImageSwitcher实现相册滑动查看照片功能(附代码下载)

發布時間:2025/3/19 Android 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android中通过ImageSwitcher实现相册滑动查看照片功能(附代码下载) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

場景

效果

?

注:

博客:
https://blog.csdn.net/badao_liumang_qizhi
關注公眾號
霸道的程序猿
獲取編程相關電子書、教程推送與免費下載。

實現

將需要滾動查看的照片復制到res/drawable下

這里只準備了兩張bg01.jpg和bg02.jpg

?

在滾動時需要用到左進右出和左出右進的動畫,所以在res下新建anim目錄,在目錄下新建四種動畫的xml文件

?

具體代碼參照示例代碼。

然后打開布局文件activity_image_switcher.xml

將布局修改為相對布局RelativeLayout,并添加一個ImageSwitcher,設置其ID屬性。

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout 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=".ImageSwitcherActivity"><ImageSwitcherandroid:id="@+id/imageSwitcher"android:layout_width="match_parent"android:layout_height="match_parent"/></RelativeLayout>

然后來到ImageSwitcherActivity

首先聲明一些私有變量,用來存儲照片資源數組、數組索引、鼠標放下和離開的X坐標等。??

?? //圖片資源數組private int[] arrayPicture = new int[]{R.drawable.bg01,R.drawable.bg02};private ImageSwitcher imageSwitcher;private? int index;private? float touchDowmX;private? float touchUpX;

然后通過id獲取ImageSwitcher并設置其視圖工廠

??????? //獲取imageSwitchimageSwitcher =(ImageSwitcher) findViewById(R.id.imageSwitcher);//設置視圖工廠imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {@Overridepublic View makeView() {ImageView imageView = new ImageView(ImageSwitcherActivity.this);imageView.setImageResource(arrayPicture[index]);return imageView;}});

然后設置ImageSwitcher的觸碰的監聽器

通過

event.getAction() == MotionEvent.ACTION_DOWN

判斷如果是鼠標按下,則記錄鼠標按下時的坐標。

否則通過

event.getAction() ==MotionEvent.ACTION_UP

判斷如果是鼠標抬起,則記錄抬起時的X坐標。

此時再通過

touchUpX-touchDowmX >100

即抬起時的X坐標減去落下時的X坐標大于100則認為是從左往右滑動。

此時圖片的索引通過三目表達式進行判斷。

index = index==0?arrayPicture.length-1:index-1;

如果當前索引為0,即為第一張照片時,則從左往右滑動后,應該是最后一張照片,即照片索引為圖片數組的長度減一。

然后通過

?imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(ImageSwitcherActivity.this, R.anim.slide_in_left));

設置左邊滑進的動畫

再通過

imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(ImageSwitcherActivity.this, R.anim.slide_out_right));

設置右邊滑出的動畫

最后通過

imageSwitcher.setImageResource(arrayPicture[index]);

設置圖片索引。

同理如果通過

touchDowmX - touchUpX >100

則認為是從右往左滑。

同樣通過三目表達式

index = index==arrayPicture.length -1?0:index+1;

如果是最后一張照片,即索引為數組的長度 -1 ,則再往左滑 該是第一張照片,即索引為0? 否則就索引+1。

然后通過

imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(ImageSwitcherActivity.this, R.anim.slide_in_right));

設置右邊滑進的動畫

再通過

imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(ImageSwitcherActivity.this, R.anim.slide_out_left));

設置左邊滑出的動畫

最后通過

imageSwitcher.setImageResource(arrayPicture[index]);

設置圖片

完整示例代碼

package com.badao.relativelayouttest;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.animation.AnimationUtils; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher;public class ImageSwitcherActivity extends AppCompatActivity {//圖片資源數組private int[] arrayPicture = new int[]{R.drawable.bg01,R.drawable.bg02};private ImageSwitcher imageSwitcher;private? int index;private? float touchDowmX;private? float touchUpX;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_image_switcher);//獲取imageSwitchimageSwitcher =(ImageSwitcher) findViewById(R.id.imageSwitcher);//設置視圖工廠imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {@Overridepublic View makeView() {ImageView imageView = new ImageView(ImageSwitcherActivity.this);imageView.setImageResource(arrayPicture[index]);return imageView;}});//設置imageSwitcher 觸碰監聽器imageSwitcher.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {//如果是鼠標按下if(event.getAction() == MotionEvent.ACTION_DOWN){//記錄按下時的X坐標touchDowmX = event.getX();return? true;}else if(event.getAction() ==MotionEvent.ACTION_UP) //如果是鼠標抬起{//記錄抬起時的X坐標touchUpX = event.getX();//如果是從左向右滑動if(touchUpX-touchDowmX >100){//如果是第一張圖片則從左向右滑后下標是數組的長度-1,即最后一張,如果不是則索引-1index = index==0?arrayPicture.length-1:index-1;//設置左邊滑進的動畫imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(ImageSwitcherActivity.this, R.anim.slide_in_left));//設置右邊滑出的動畫imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(ImageSwitcherActivity.this, R.anim.slide_out_right));//設置圖片索引imageSwitcher.setImageResource(arrayPicture[index]);}//否則認為是從右往左滑else if(touchDowmX - touchUpX >100){//如果是最后一張照片,即索引為數組的長度 -1 ,則再往左滑 該是第一張照片,即索引為0? 否則就索引+1index = index==arrayPicture.length -1?0:index+1;//設置右邊滑進的動畫imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(ImageSwitcherActivity.this, R.anim.slide_in_right));//設置左邊滑出的動畫imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(ImageSwitcherActivity.this, R.anim.slide_out_left));//設置圖片索引imageSwitcher.setImageResource(arrayPicture[index]);}}return false;}});} }

示例代碼下載

關注公眾號:

霸道的程序猿

回復:

Android相冊滑動代碼

?

?

?

?

?

?

?

?

?

總結

以上是生活随笔為你收集整理的Android中通过ImageSwitcher实现相册滑动查看照片功能(附代码下载)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。