網(wǎng)站首頁 編程語言 正文
本文實(shí)例為大家分享了Android studio實(shí)現(xiàn)左右滑動(dòng)切換圖片的具體代碼,供大家參考,具體內(nèi)容如下
切換圖片首先要使用到圖片切換器ImageSwitcher
先了解一下ImageSwitcher
1.ImageSwitcher的重要屬性:
android:inAnimation:切入圖片時(shí)的效果。
android:outAnimation:切出圖片時(shí)的效果。
以上兩個(gè)屬性在XML中設(shè)定,可以通過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通過setFactory()方法為它設(shè)置一個(gè)ViewSwitcher.ViewFactory接口。設(shè)置這個(gè)ViewFactory接口時(shí)需要實(shí)現(xiàn)makeView()方法,該方法通常會(huì)返回一個(gè)ImageView。makeView()為ImageSwitcher生成ImageView。
接下來代碼實(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)聽器
? ? ? ? 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開始的,所以應(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;
? ? ? ? ? ? }
? ? ? ? });
? ? }
}
原文鏈接:https://blog.csdn.net/wj778/article/details/106176079
相關(guān)推薦
- 2022-11-12 CSS單標(biāo)簽實(shí)現(xiàn)復(fù)雜的棋盤布局_經(jīng)驗(yàn)交流
- 2024-07-18 Spring Security之認(rèn)證過濾器
- 2022-04-16 c語言?數(shù)據(jù)存儲(chǔ)與原碼?反碼?補(bǔ)碼詳細(xì)解析_C 語言
- 2022-05-13 C++ 減少臨時(shí)字符串對(duì)象的產(chǎn)生
- 2022-07-22 C語言實(shí)現(xiàn)在屏幕上打印特定的*星號(hào)圖案
- 2022-06-17 C#關(guān)鍵字之重載Overload介紹_C#教程
- 2022-04-08 MariaDB表表達(dá)式之公用表表達(dá)式(CTE)_mariadb
- 2022-07-01 Python中的字典合并與列表合并技巧_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支