最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2022年05月17日 10:08:57   作者:wj778  
這篇文章主要為大家詳細(xì)介紹了Android studio實(shí)現(xiàn)左右滑動(dòng)切換圖片,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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;
? ? ? ? ? ? }
? ? ? ? });
? ? }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

新平| 大荔县| 临漳县| 石泉县| 绍兴县| 罗田县| 仪陇县| 成武县| 黄浦区| 丹凤县| 香河县| 达孜县| 西华县| 宿迁市| 墨竹工卡县| 宜宾市| 城固县| 本溪市| 瑞丽市| 清河县| 盱眙县| 巩义市| 汉川市| 浠水县| 双峰县| 怀集县| 土默特左旗| 日土县| 漯河市| 临武县| 慈利县| 淮南市| 伊宁市| 西藏| 临沭县| 文昌市| 甘孜| 永昌县| 伊吾县| 谢通门县| 平遥县|