Android中通过ImageSwitcher实现相册滑动查看照片功能(附代码下载)
場景
效果
?
注:
博客:
https://blog.csdn.net/badao_liumang_qizhi
關(guān)注公眾號
霸道的程序猿
獲取編程相關(guān)電子書、教程推送與免費(fèi)下載。
實(shí)現(xiàn)
將需要滾動查看的照片復(fù)制到res/drawable下
這里只準(zhǔn)備了兩張bg01.jpg和bg02.jpg
?
在滾動時(shí)需要用到左進(jìn)右出和左出右進(jìn)的動畫,所以在res下新建anim目錄,在目錄下新建四種動畫的xml文件
?
具體代碼參照示例代碼。
然后打開布局文件activity_image_switcher.xml
將布局修改為相對布局RelativeLayout,并添加一個(gè)ImageSwitcher,設(shè)置其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
首先聲明一些私有變量,用來存儲照片資源數(shù)組、數(shù)組索引、鼠標(biāo)放下和離開的X坐標(biāo)等。??
?? //圖片資源數(shù)組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并設(shè)置其視圖工廠
??????? //獲取imageSwitchimageSwitcher =(ImageSwitcher) findViewById(R.id.imageSwitcher);//設(shè)置視圖工廠imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {@Overridepublic View makeView() {ImageView imageView = new ImageView(ImageSwitcherActivity.this);imageView.setImageResource(arrayPicture[index]);return imageView;}});然后設(shè)置ImageSwitcher的觸碰的監(jiān)聽器
通過
event.getAction() == MotionEvent.ACTION_DOWN判斷如果是鼠標(biāo)按下,則記錄鼠標(biāo)按下時(shí)的坐標(biāo)。
否則通過
event.getAction() ==MotionEvent.ACTION_UP判斷如果是鼠標(biāo)抬起,則記錄抬起時(shí)的X坐標(biāo)。
此時(shí)再通過
touchUpX-touchDowmX >100即抬起時(shí)的X坐標(biāo)減去落下時(shí)的X坐標(biāo)大于100則認(rèn)為是從左往右滑動。
此時(shí)圖片的索引通過三目表達(dá)式進(jìn)行判斷。
index = index==0?arrayPicture.length-1:index-1;如果當(dāng)前索引為0,即為第一張照片時(shí),則從左往右滑動后,應(yīng)該是最后一張照片,即照片索引為圖片數(shù)組的長度減一。
然后通過
?imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(ImageSwitcherActivity.this, R.anim.slide_in_left));設(shè)置左邊滑進(jìn)的動畫
再通過
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(ImageSwitcherActivity.this, R.anim.slide_out_right));設(shè)置右邊滑出的動畫
最后通過
imageSwitcher.setImageResource(arrayPicture[index]);設(shè)置圖片索引。
同理如果通過
touchDowmX - touchUpX >100則認(rèn)為是從右往左滑。
同樣通過三目表達(dá)式
index = index==arrayPicture.length -1?0:index+1;如果是最后一張照片,即索引為數(shù)組的長度 -1 ,則再往左滑 該是第一張照片,即索引為0? 否則就索引+1。
然后通過
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(ImageSwitcherActivity.this, R.anim.slide_in_right));設(shè)置右邊滑進(jìn)的動畫
再通過
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(ImageSwitcherActivity.this, R.anim.slide_out_left));設(shè)置左邊滑出的動畫
最后通過
imageSwitcher.setImageResource(arrayPicture[index]);設(shè)置圖片
完整示例代碼
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 {//圖片資源數(shù)組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);//設(shè)置視圖工廠imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {@Overridepublic View makeView() {ImageView imageView = new ImageView(ImageSwitcherActivity.this);imageView.setImageResource(arrayPicture[index]);return imageView;}});//設(shè)置imageSwitcher 觸碰監(jiān)聽器imageSwitcher.setOnTouchListener(new View.OnTouchListener() {@Overridepublic boolean onTouch(View v, MotionEvent event) {//如果是鼠標(biāo)按下if(event.getAction() == MotionEvent.ACTION_DOWN){//記錄按下時(shí)的X坐標(biāo)touchDowmX = event.getX();return? true;}else if(event.getAction() ==MotionEvent.ACTION_UP) //如果是鼠標(biāo)抬起{//記錄抬起時(shí)的X坐標(biāo)touchUpX = event.getX();//如果是從左向右滑動if(touchUpX-touchDowmX >100){//如果是第一張圖片則從左向右滑后下標(biāo)是數(shù)組的長度-1,即最后一張,如果不是則索引-1index = index==0?arrayPicture.length-1:index-1;//設(shè)置左邊滑進(jìn)的動畫imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(ImageSwitcherActivity.this, R.anim.slide_in_left));//設(shè)置右邊滑出的動畫imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(ImageSwitcherActivity.this, R.anim.slide_out_right));//設(shè)置圖片索引imageSwitcher.setImageResource(arrayPicture[index]);}//否則認(rèn)為是從右往左滑else if(touchDowmX - touchUpX >100){//如果是最后一張照片,即索引為數(shù)組的長度 -1 ,則再往左滑 該是第一張照片,即索引為0? 否則就索引+1index = index==arrayPicture.length -1?0:index+1;//設(shè)置右邊滑進(jìn)的動畫imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(ImageSwitcherActivity.this, R.anim.slide_in_right));//設(shè)置左邊滑出的動畫imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(ImageSwitcherActivity.this, R.anim.slide_out_left));//設(shè)置圖片索引imageSwitcher.setImageResource(arrayPicture[index]);}}return false;}});} }示例代碼下載
關(guān)注公眾號:
霸道的程序猿
回復(fù):
Android相冊滑動代碼
?
?
?
?
?
?
?
?
?
總結(jié)
以上是生活随笔為你收集整理的Android中通过ImageSwitcher实现相册滑动查看照片功能(附代码下载)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android入门教程免费获取
- 下一篇: ZedGraph5.1.5源码分析去掉鼠