Android studio實現(xiàn)左右滑動切換圖片
本文實例為大家分享了Android studio實現(xiàn)左右滑動切換圖片的具體代碼,供大家參考,具體內(nèi)容如下
切換圖片首先要使用到圖片切換器ImageSwitcher
先了解一下ImageSwitcher
1.ImageSwitcher的重要屬性:
android:inAnimation:切入圖片時的效果。
android:outAnimation:切出圖片時的效果。
以上兩個屬性在XML中設(shè)定,可以通過XML資源文件自定義動畫效果,如果只是想使用Android自帶的一些簡單的效果,調(diào)用Android內(nèi)置的資源即可,也可以在代碼中設(shè)定,可以直接使用setInAnimation()和setOutAnimation()方法。它們都傳遞一個Animation的抽象對象,Animation用于描述一個動畫效果,一般使用一個AnimationUtils的工具類獲得。
常用的動畫效果有:
- 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è)置一個ViewSwitcher.ViewFactory接口。設(shè)置這個ViewFactory接口時需要實現(xiàn)makeView()方法,該方法通常會返回一個ImageView。makeView()為ImageSwitcher生成ImageView。
接下來代碼實現(xiàn)左右滑動切換圖片
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) {
? ? ? ? ? ? ? ? //判斷動作是不是按下 ?獲得按下時的X坐標(biāo)
? ? ? ? ? ? ? ? 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){
? ? ? ? ? ? ? ? ? ? ? ? //判斷是不是第一張圖片 是就將索引變成最后一張圖片索引,
? ? ? ? ? ? ? ? ? ? ? ? // 不是則當(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)該是長度減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;
? ? ? ? ? ? }
? ? ? ? });
? ? }
}以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Flutter?彈性布局基石flex算法flexible示例詳解
這篇文章主要為大家介紹了Flutter?彈性布局基石flex算法flexible示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android中判斷手機(jī)是否聯(lián)網(wǎng)實例
這篇文章主要介紹了Android中判斷手機(jī)是否聯(lián)網(wǎng)實例,包括xml配置文件及功能代碼的實現(xiàn),需要的朋友可以參考下2014-10-10
Android中Listview下拉刷新和上拉加載更多的多種實現(xiàn)方案
本文大概通過三種方案給大家介紹了Android中Listview下拉刷新和上拉加載更多知識,非常不錯,具有參考借鑒價值,需要的朋友參考下2016-12-12
Android?IntentFilter的匹配規(guī)則示例詳解
這篇文章主要為大家介紹了Android?IntentFilter的匹配規(guī)則示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android Studio實現(xiàn)登錄功能案例講解
這篇文章主要介紹了Android Studio實現(xiàn)登錄功能案例講解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-08-08
Android Studio實現(xiàn)簡易進(jìn)制轉(zhuǎn)換計算器
這篇文章主要為大家詳細(xì)介紹了Android Studio實現(xiàn)簡易進(jìn)制轉(zhuǎn)換計算器,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05

