日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達(dá)者為師

網(wǎng)站首頁 編程語言 正文

Android?studio實(shí)現(xiàn)左右滑動(dòng)切換圖片_Android

作者:wj778 ? 更新時(shí)間: 2022-07-11 編程語言

本文實(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

欄目分類
最近更新