本文实例为大家分享了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; } }); } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。