AndroidStudio实现照片的左右滑动切换
1.activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.mingrisoft.showpictrue.MainActivity">
<!-- 图片切换器-->
<ImageSwitcher
android:id="@+id/imageswitcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
2.MainActivity.java
package com.example.app0901;
import android.support.v7.app.ActionBarActivity;
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;
public class MainActivity extends ActionBarActivity {
//保存图像Id
private int[] arrayPicture=new int[]{
R.drawable.t01,R.drawable.t02,R.drawable.t03,
R.drawable.t04,R.drawable.t05,R.drawable.t06,
};
private ImageSwitcher imageSwitcher;
//当前图片的下标
private int index;
//鼠标按下,拿起的坐标
private float touchDownX,touchUpX;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//使其全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//获取xml中的图片转换器
imageSwitcher= (ImageSwitcher) findViewById(R.id.imageswitcher);
//放置初始图片
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
//创建一个图片对象
ImageView imageView=new ImageView(MainActivity.this);
//给该对象设置图片
imageView.setImageResource(arrayPicture[index]);
return imageView;
}
});
//触摸事件监听器
imageSwitcher.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
//如果获得当前事件的活动等于鼠标点下,则给touchDownX赋值
if(motionEvent.getAction()==MotionEvent.ACTION_DOWN)
{
touchDownX=motionEvent.getX();
return true;
}
//如果鼠标抬起
else if(motionEvent.getAction()==MotionEvent.ACTION_UP)
{
//记录此时坐标
touchUpX=motionEvent.getX();
//判断移动方向
if(touchUpX-touchDownX>100)//从左向右
{
//改变图片下标值,
index=(index==0?arrayPicture.length-1:index-1);
//渐入渐出效果
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this,android.R.anim.fade_out));
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this,android.R.anim.fade_in));
//把图片资源给图片切换器
imageSwitcher.setImageResource(arrayPicture[index]);
}else if(touchDownX-touchUpX>100)//从右向左
{
index=(index==arrayPicture.length-1?0:index+1);
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this,android.R.anim.fade_out));
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this,android.R.anim.fade_in));
imageSwitcher.setImageResource(arrayPicture[index]);
}
return true;
}
return false;
}
});
}
}
3.drawable文件夹存放照片
可下载我的源代码,链接如下
链接:https://pan.baidu.com/s/1GQK6B1L6Qkk8G9eg_Dys6Q
提取码:1234