安卓開發——實現手機相冊的查看功能


布局文件!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
<?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