Android studio實(shí)現(xiàn)左右滑動(dòng)切換圖片
本文實(shí)例為大家分享了Android studio實(shí)現(xiàn)左右滑動(dòng)切換圖片的具體代碼,供大家參考,具體內(nèi)容如下
切換圖片首先要使用到圖片切換器ImageSwitcher
先了解一下ImageSwitcher
1.ImageSwitcher的重要屬性:
android:inAnimation:切入圖片時(shí)的效果。
android:outAnimation:切出圖片時(shí)的效果。
以上兩個(gè)屬性在XML中設(shè)定,可以通過(guò)XML資源文件自定義動(dòng)畫效果,如果只是想使用Android自帶的一些簡(jiǎn)單的效果,調(diào)用Android內(nèi)置的資源即可,也可以在代碼中設(shè)定,可以直接使用setInAnimation()和setOutAnimation()方法。它們都傳遞一個(gè)Animation的抽象對(duì)象,Animation用于描述一個(gè)動(dòng)畫效果,一般使用一個(gè)AnimationUtils的工具類獲得。
常用的動(dòng)畫效果有:
- fede_in:淡進(jìn)
- fade_out:淡出
- slide_in_left:從左滑進(jìn)
- slide_out_right: 從右滑出
2.java文件中ImageSwitcher的重要重要方法:
setImageURL(URL) setImageResource(int) setImageDrawable(Drawable)
3.視圖工廠 setFactory()
ImageSwitcher通過(guò)setFactory()方法為它設(shè)置一個(gè)ViewSwitcher.ViewFactory接口。設(shè)置這個(gè)ViewFactory接口時(shí)需要實(shí)現(xiàn)makeView()方法,該方法通常會(huì)返回一個(gè)ImageView。makeView()為ImageSwitcher生成ImageView。
接下來(lái)代碼實(shí)現(xiàn)左右滑動(dòng)切換圖片
XML
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" ? ? android:orientation="vertical" ? ? android:layout_width="match_parent" ? ? android:layout_height="match_parent"> ? ? <ImageSwitcher ? ? ? ? android:id="@+id/imageswitch" ? ? ? ? android:layout_width="wrap_content" ? ? ? ? android:layout_height="wrap_content"/> </LinearLayout>
java代碼如下:
package com.example.tablelayout; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import android.view.WindowManager; import android.view.animation.AnimationUtils; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.ViewSwitcher; import androidx.appcompat.app.AppCompatActivity; public class ImageSwitcha_Activity extends AppCompatActivity { ? ? private ?int[] ?arrayPicture=new int[]{ ? ? ? ? ? ? R.drawable.pa,R.drawable.pb}; ? ? private ImageSwitcher imageSwitcher; ? ? private int ?index; ? ? private ?float touchDownX; ? ? private ?float touchUpX; ? ? @Override ? ? protected void onCreate(Bundle savedInstanceState) { ? ? ? ? super.onCreate(savedInstanceState); ? ? ? ? setContentView(R.layout.imageswitch_main); ? ? ? ? //設(shè)置全屏顯示 ? ? ? ? getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, ? ? ? ? ? ? ? ? WindowManager.LayoutParams.FLAG_FULLSCREEN); ? ? ? ? imageSwitcher=findViewById(R.id.imageswitch); ? ? ? ? //設(shè)置視圖工廠 ? ? ? ? imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public View makeView() { ? ? ? ? ? ? ? ? ImageView ?imageView=new ImageView(ImageSwitcha_Activity.this); ? ? ? ? ? ? ? ? imageView.setImageResource(arrayPicture[index]);//設(shè)置顯示圖片(利用下標(biāo)) ? ? ? ? ? ? ? ? return imageView;//返回圖像視圖 ? ? ? ? ? ? } ? ? ? ? }); ? ? ? ? //設(shè)置觸摸監(jiān)聽(tīng)器 ? ? ? ? imageSwitcher.setOnTouchListener(new View.OnTouchListener() { ? ? ? ? ? ? @Override ? ? ? ? ? ? public boolean onTouch(View v, MotionEvent event) { ? ? ? ? ? ? ? ? //判斷動(dòng)作是不是按下 ?獲得按下時(shí)的X坐標(biāo) ? ? ? ? ? ? ? ? if(event.getAction()==MotionEvent.ACTION_DOWN) { ? ? ? ? ? ? ? ? ? ? touchDownX=event.getX(); ? ? ? ? ? ? ? ? ? ? return true; ? ? ? ? ? ? ? ? } else if(event.getAction()==MotionEvent.ACTION_UP) { ? ? ? ? ? ? ? ? ? ? touchUpX=event.getX(); ? ? ? ? ? ? ? ? ? ? //判斷是左滑動(dòng)還是右滑動(dòng) ? ? ? ? ? ? ? ? ? ? if(touchUpX-touchDownX>100){ ? ? ? ? ? ? ? ? ? ? ? ? //判斷是不是第一張圖片 是就將索引變成最后一張圖片索引, ? ? ? ? ? ? ? ? ? ? ? ? // 不是則當(dāng)前索引減一 ? ? ? ? ? ? ? ? ? ? ? ? index=index==0?arrayPicture.length-1:index-1; ? ? ? ? ? ? ? ? ? ? ? ? //使用自帶的淡入淡出 ? ? ? ? ? ? ? ? ? ? ? ? imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(ImageSwitcha_Activity.this,android.R.anim.fade_in)); ? ? ? ? ? ? ? ? ? ? ? ? imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(ImageSwitcha_Activity.this,android.R.anim.fade_out)); ? ? ? ? ? ? ? ? ? ? ? ? imageSwitcher.setImageResource(arrayPicture[index]); ? ? ? ? ? ? ? ? ? ? }else if(touchDownX-touchUpX>100){ ? ? ? ? ? ? ? ? ? ? ? ? index=index==arrayPicture.length-1?0:index+1;//注意這里下標(biāo)是從0開(kāi)始的,所以應(yīng)該是長(zhǎng)度減1 ? ? ? ? ? ? ? ? ? ? ? ? imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(ImageSwitcha_Activity.this,android.R.anim.fade_in)); ? ? ? ? ? ? ? ? ? ? ? ? imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(ImageSwitcha_Activity.this,android.R.anim.fade_out)); ? ? ? ? ? ? ? ? ? ? ? ? imageSwitcher.setImageResource(arrayPicture[index]); ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? return true; ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? return false; ? ? ? ? ? ? } ? ? ? ? }); ? ? } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)圖片九宮格
- Android不壓縮圖片實(shí)現(xiàn)高清加載巨圖實(shí)例
- Android實(shí)現(xiàn)手勢(shì)劃定區(qū)域裁剪圖片
- AndroidStudio實(shí)現(xiàn)能在圖片上涂鴉程序
- Android實(shí)現(xiàn)左右滑動(dòng)切換圖片
- Android開(kāi)發(fā)實(shí)現(xiàn)圖片大小與質(zhì)量壓縮及保存
- Android性能優(yōu)化之圖片大小,尺寸壓縮綜合解決方案
- Android實(shí)現(xiàn)圖片預(yù)覽與保存功能
- Android自定義視圖中圖片的處理
相關(guān)文章
Android Retrofit 中文亂碼問(wèn)題的解決辦法
這篇文章主要介紹了Android Retrofit 中文亂碼問(wèn)題的解決辦法的相關(guān)資料,希望通過(guò)本文能幫助到大家,讓大家遇到這種問(wèn)題及時(shí)的解決,需要的朋友可以參考下2017-10-10Flutter學(xué)習(xí)LogUtil封裝與實(shí)現(xiàn)實(shí)例詳解
這篇文章主要為大家介紹了Flutter學(xué)習(xí)LogUtil封裝與實(shí)現(xiàn)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09一行代碼教你解決Scrollview和TextInput焦點(diǎn)獲取問(wèn)題
這篇文章主要為大家介紹了一行代碼教你解決Scrollview和TextInput焦點(diǎn)獲取問(wèn)題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12解決android Listview的item中最外層Margin失效的問(wèn)題
下面小編就為大家?guī)?lái)一篇解決android Listview的item中最外層Margin失效的問(wèn)題。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04Android App支付系列(一):微信支付接入詳細(xì)指南(附官方支付demo)
這篇文章主要介紹了Android App支付系列(一):微信支付接入詳細(xì)指南(附官方支付demo) ,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-11-11Android中手機(jī)錄屏并轉(zhuǎn)換GIF的兩種方式
本文主要介紹了android中手機(jī)錄屏并轉(zhuǎn)換GIF的兩種方式,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01如何修改Android Studio創(chuàng)建module時(shí)默認(rèn)的compileSdkVersion
這篇文章主要給大家介紹了如何修改Android Studio創(chuàng)建module時(shí)默認(rèn)的compileSdkVersion的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-05-05flutter 實(shí)現(xiàn)多布局列表的示例代碼
這篇文章主要介紹了flutter 實(shí)現(xiàn)多布局列表的示例代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02