網站首頁 編程語言 正文
本文實例為大家分享了Android studio實現左右滑動切換圖片的具體代碼,供大家參考,具體內容如下
切換圖片首先要使用到圖片切換器ImageSwitcher
先了解一下ImageSwitcher
1.ImageSwitcher的重要屬性:
android:inAnimation:切入圖片時的效果。
android:outAnimation:切出圖片時的效果。
以上兩個屬性在XML中設定,可以通過XML資源文件自定義動畫效果,如果只是想使用Android自帶的一些簡單的效果,調用Android內置的資源即可,也可以在代碼中設定,可以直接使用setInAnimation()和setOutAnimation()方法。它們都傳遞一個Animation的抽象對象,Animation用于描述一個動畫效果,一般使用一個AnimationUtils的工具類獲得。
常用的動畫效果有:
- fede_in:淡進
- fade_out:淡出
- slide_in_left:從左滑進
- slide_out_right: 從右滑出
2.java文件中ImageSwitcher的重要重要方法:
setImageURL(URL) setImageResource(int) setImageDrawable(Drawable)
3.視圖工廠 setFactory()
ImageSwitcher通過setFactory()方法為它設置一個ViewSwitcher.ViewFactory接口。設置這個ViewFactory接口時需要實現makeView()方法,該方法通常會返回一個ImageView。makeView()為ImageSwitcher生成ImageView。
接下來代碼實現左右滑動切換圖片
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);
? ? ? ? //設置全屏顯示
? ? ? ? getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
? ? ? ? ? ? ? ? WindowManager.LayoutParams.FLAG_FULLSCREEN);
? ? ? ? imageSwitcher=findViewById(R.id.imageswitch);
? ? ? ? //設置視圖工廠
? ? ? ? imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public View makeView() {
? ? ? ? ? ? ? ? ImageView ?imageView=new ImageView(ImageSwitcha_Activity.this);
? ? ? ? ? ? ? ? imageView.setImageResource(arrayPicture[index]);//設置顯示圖片(利用下標)
? ? ? ? ? ? ? ? return imageView;//返回圖像視圖
? ? ? ? ? ? }
? ? ? ? });
? ? ? ? //設置觸摸監聽器
? ? ? ? imageSwitcher.setOnTouchListener(new View.OnTouchListener() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public boolean onTouch(View v, MotionEvent event) {
? ? ? ? ? ? ? ? //判斷動作是不是按下 ?獲得按下時的X坐標
? ? ? ? ? ? ? ? if(event.getAction()==MotionEvent.ACTION_DOWN) {
? ? ? ? ? ? ? ? ? ? touchDownX=event.getX();
? ? ? ? ? ? ? ? ? ? return true;
? ? ? ? ? ? ? ? } else if(event.getAction()==MotionEvent.ACTION_UP) {
? ? ? ? ? ? ? ? ? ? touchUpX=event.getX();
? ? ? ? ? ? ? ? ? ? //判斷是左滑動還是右滑動
? ? ? ? ? ? ? ? ? ? if(touchUpX-touchDownX>100){
? ? ? ? ? ? ? ? ? ? ? ? //判斷是不是第一張圖片 是就將索引變成最后一張圖片索引,
? ? ? ? ? ? ? ? ? ? ? ? // 不是則當前索引減一
? ? ? ? ? ? ? ? ? ? ? ? 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;//注意這里下標是從0開始的,所以應該是長度減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
相關推薦
- 2022-09-19 C/C++淺析鄰接表拓撲排序算法的實現_C 語言
- 2022-11-11 react中代碼塊輸出,代碼高亮顯示,帶行號,能復制的問題_React
- 2022-07-17 C++深入講解namespace與string關鍵字的使用_C 語言
- 2022-10-05 C++淺析數據在內存中如何存儲_C 語言
- 2023-07-07 根據文件后綴名稱獲取contentType,其中Minio上傳文件會用到contentType
- 2022-04-18 Python基礎中的的if-else語句詳解_python
- 2022-07-19 Linux——磁盤管理與文件系統
- 2023-05-31 python升級pip及失敗處理方式_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支