安卓开发——实现手机相册的查看功能


布局文件!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context=".MainActivity">
<ImageSwitcher
android:id="@+id/imageswitcher"
android:layout_width="match_parent"
android:layout_height="match_parent">

</ImageSwitcher>

</RelativeLayout>

Java文件!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
package com.example.a8imageswitch;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher;

public class MainActivity extends AppCompatActivity {
//初始化显示保存id的数组
private int[] arrayPicture=new int[]{R.drawable.test1,R.drawable.test2,R.drawable.test3,R.drawable.test4,R.drawable.test5,R.drawable.test6,R.drawable.test7,R.drawable.test8,R.drawable.test9};
private ImageSwitcher imageswithcher;
//要显示的图片在图片数组中的索引
private int index;
//定义手指按下和抬起时X的坐标
private float touchDownX;
private float touchUpX;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//设置全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAGS_CHANGED,WindowManager.LayoutParams.FLAG_FULLSCREEN);
//获取ImageSwitcher对象并创建工厂
imageswithcher=findViewById(R.id.imageswitcher);
imageswithcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
//在重写的方法中创建ImageView用于显示图片
ImageView imageView=new ImageView(MainActivity.this);
//给ImageView设定一个要显示的默认图片
imageView.setImageResource(arrayPicture[index]);
return imageView;
}
});
//为ImageView添加触发时间监听器
imageswithcher.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
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;
//定义一下图片的切换方式
imageswithcher.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this,android.R.anim.fade_out));
imageswithcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this,android.R.anim.fade_in));
imageswithcher.setImageResource(arrayPicture[index]);
}else if(touchDownX-touchUpX>100){
//如果当前的索引值等于数组的长度 那个index设置为0.否则的话index+1
index=index==arrayPicture.length-1?0:index+1;
//定义一下图片的切换方式
imageswithcher.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this,android.R.anim.fade_in));
imageswithcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this,android.R.anim.fade_out));
imageswithcher.setImageResource(arrayPicture[index]);
}
return true;
}
return false;
}
});
}
}


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM